如何动态呼叫function or procedure

字体大小: 中小 标准 ->行高大小: 标准
type
  TMyPorcdure = procedure (Sender: TObject) of Object;

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    Procedure P1(Sender: TObject);
    Procedure P2(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TForm1 }

procedure TForm1.P1(Sender: TObject);
begin
  showmessage('In P1 Procedure');
end;

procedure TForm1.P2(Sender: TObject);
begin
  showmessage('In P2 Procedure');
end;

procedure TForm1.Button1Click(Sender: TObject);
var m : TMyPorcdure;
    s : string;
    p : Pointer;
begin
  s := 'P1';
  p := MethodAddress(s);
  if p<>nil then begin
    TMethod(m).Code := p;
    TMethod(m).Data := Self;
    m(Sender);
  end
  else raise Exception.CreateFmt('Procedure %s not exists',[s]);
  s := 'P2';
  p := MethodAddress(s);
  if p<>nil then begin
    TMethod(m).Code := p;
    TMethod(m).Data := Self;
    m(Sender);
  end
  else raise Exception.CreateFmt('Procedure %s not exists',[s]);
end;

此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/67735.html