type TShowFrm=Function(AHandle: THandle; aParent:TWinControl): boolean;stdcall; //是一个函数类 TForm1 = class(TForm) Button1: TButton; Button2: TButton; Panel1: TPanel; procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } CustomFunc:TShowFrm; //这里声明了一个实例 CustomHandle:THandle; public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject); begin if CustomHandle=0 then begin CustomHandle := LoadLibrary('project2.dll'); @CustomFunc:=GetProcAddress(CustomHandle,'ShowInheritFrm'); //这里用了 if @CustomFunc<>nil then begin CustomFunc(Application.Handle,panel1); end else showmessage('error'); end
end;procedure TForm1.FormCreate(Sender: TObject); begin Customhandle:=0; end;procedure TForm1.Button2Click(Sender: TObject); begin if not (CustomHandle = 0) then begin FreeLibrary(CustomHandle); CustomHandle := 0; @CustomFunc:=nil; end; end; 这个办法在动态调用动态链接库是经常使用,好好看看动态链接库的动态调用就知道了
声明了一个函数原型,名字是 TGetValueFunc,参数是 prop:TPropertyRecord,返回值是 boolean. of Object 表明是一个类实例的函数指针。如果不加表示普通的一个函数指针。 这种声明通常用于调用Dll里的函数。delphi 帮助里搜索 of object:The variables above are all procedure pointers--that is, pointers to the address of a procedure or function. If you want to reference a method of an instance object (see Classes and objects), you need to add the words of object to the procedural type name. For exampletype TMethod = procedure of object; TNotifyEvent = procedure(Sender: TObject) of object;These types represent method pointers. A method pointer is really a pair of pointers; the first stores the address of a method, and the second stores a reference to the object the method belongs to.
示例(delphi 帮助):有如下声明: type TNotifyEvent = procedure(Sender: TObject) of object; TMainForm = class(TForm) procedure ButtonClick(Sender: TObject); ... end; var MainForm: TMainForm; OnClick: TNotifyEvent可以像以下这样赋值: OnClick := MainForm.ButtonClick;
TGetValueFunc=function(prop:TPropertyRecord):boolean of Object; function Tform1.aa(prop:TPropertyRecord):boolean begin ........ end;var bb:TGetValueFunc; begin bb:=form1.aa;//aa必须是类的方法(of object) bb(prop); end;
TShowFrm=Function(AHandle: THandle; aParent:TWinControl): boolean;stdcall; //是一个函数类
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Panel1: TPanel;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
CustomFunc:TShowFrm; //这里声明了一个实例
CustomHandle:THandle;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
if CustomHandle=0 then
begin
CustomHandle := LoadLibrary('project2.dll');
@CustomFunc:=GetProcAddress(CustomHandle,'ShowInheritFrm'); //这里用了
if @CustomFunc<>nil then
begin
CustomFunc(Application.Handle,panel1);
end
else
showmessage('error');
end
end;procedure TForm1.FormCreate(Sender: TObject);
begin
Customhandle:=0;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
if not (CustomHandle = 0) then
begin
FreeLibrary(CustomHandle);
CustomHandle := 0;
@CustomFunc:=nil;
end;
end;
这个办法在动态调用动态链接库是经常使用,好好看看动态链接库的动态调用就知道了
of Object 表明是一个类实例的函数指针。如果不加表示普通的一个函数指针。
这种声明通常用于调用Dll里的函数。delphi 帮助里搜索 of object:The variables above are all procedure pointers--that is, pointers to the address of a procedure or function. If you want to reference a method of an instance object (see Classes and objects), you need to add the words of object to the procedural type name. For exampletype TMethod = procedure of object;
TNotifyEvent = procedure(Sender: TObject) of object;These types represent method pointers. A method pointer is really a pair of pointers; the first stores the address of a method, and the second stores a reference to the object the method belongs to.
type TNotifyEvent = procedure(Sender: TObject) of object;
TMainForm = class(TForm)
procedure ButtonClick(Sender: TObject);
...
end;
var
MainForm: TMainForm;
OnClick: TNotifyEvent可以像以下这样赋值:
OnClick := MainForm.ButtonClick;
begin
........
end;var
bb:TGetValueFunc;
begin
bb:=form1.aa;//aa必须是类的方法(of object)
bb(prop);
end;