转自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 发布
本文介绍了一个使用Delphi实现动态绑定按钮点击事件的方法。通过`MethodAddress`获取方法地址,并利用`TNotifyEvent`类型来为未指定事件处理程序的按钮设置点击事件。示例展示了如何为一个按钮动态分配另一个按钮的点击事件处理程序。
1498

被折叠的 条评论
为什么被折叠?



