您现在的位置:首页 >> 基础算法 >> window基础 >> 内容

Delphi中根据名称的字符串值去调用函数

时间:2011/9/3 15:31:50 点击:

  核心提示:type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject) ; procedure Cal...

type
   TForm1 = class(TForm)
     Button1: TButton;
     procedure Button1Click(Sender: TObject) ;
     procedure CallMeByName(Sender: TObject) ;
   private
     procedure ExecMethod(OnObject: TObject; MethodName: string) ;
   end;

var
   Form1: TForm1;

type
   TExec = procedure of object;

procedure TForm1.ExecMethod(OnObject: TObject; MethodName: string) ;
var
   Routine: TMethod;
   Exec: TExec;
begin
   Routine.Data := Pointer(OnObject) ;
   Routine.Code := OnObject.MethodAddress(MethodName) ;
   if NOT Assigned(Routine.Code) then Exit;
   Exec := TExec(Routine) ;
   Exec;
end;

procedure TForm1.CallMeByName(Sender: TObject) ;
begin
   ShowMessage('Hello Delphi!') ;
end;

procedure TForm1.Button1Click(Sender: TObject) ;
begin
   ExecMethod(Form1, 'CallMeByName') ;
end;

作者:站长 来源:转载
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
  • 盒子文章(www.2ccc.com) © 2024 版权所有 All Rights Reserved.
  • 沪ICP备05001939号