转自http://www.delphibbs.com/keylife/iblog_show.asp?xid=13388
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Menus;
type
TForm1 = class(TForm)
Button1: TButton; //指定一事件
Button2: TButton; //没有指定事件,点击button1后用“字符串事件”给它赋click事件
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
procedure SetMethodByName(AObject:TObject;AName: string);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button2Click(Sender: TObject);
begin
ShowMessage('执行成功');
end;
procedure TForm1.SetMethodByName(AObject:TObject;AName: string);
var
PAddr: pointer;
M: TMethod;
begin
PAddr := MethodAddress(AName); //找本窗口中的过程地址
if PAddr <> nil then
begin
M.Code := PAddr;
TButton(AObject).OnClick := TNotifyEvent(m);//指定click事件
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
SetMethodByName(Button2,'Button2Click');
end;
end.
delphi中执行字符串表示的方法名
最新推荐文章于 2021-09-24 14:34:24 发布