TProcedure,TMethod,TNotifyEvent,TWndMethod的区别,并模拟点击按钮后发生的动作

忽然发现TProcedure和TNotifEvent的区别还挺大的:

procedure TForm1.Button2Click(Sender: TObject);
begin
    ShowMessage(IntToStr(SizeOf(TProcedure)));   // 4字节
    // ShowMessage(IntToStr(SizeOf(function)));   // 编译通不过
    ShowMessage(IntToStr(SizeOf(TNotifyEvent))); // 8字节
    ShowMessage(IntToStr(SizeOf(TMouseEvent)));  // 8字节
    ShowMessage(IntToStr(SizeOf(TWndMethod)));   // 8字节
end;

模拟点击按钮后发生的动作:

 

----------------------------------------------------------------------------------

其实各种TNotifyEvent,实际上就是TMethod,可随时做强制转换,然后测试TMethod:

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(IntToStr(SizeOf(TMethod))); // 8字节
end;

// 其声明位于System.pas中
TMethod = record
  Code, Data: Pointer;
end;

----------------------------------------------------------------------------------

TWndMethod是MakeObjectInstance所用的参数,该函数也是采用了前面类似的方法,不过不同的是,由于这些转换调用是长期的,所以那些动态生成的代码被放到了标识为可执行的动态空间中了,所以在 Win2003 的 DEP 下仍然可以正常工作:

function MakeObjectInstance(Method: TWndMethod): Pointer; 

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(IntToStr(SizeOf(TWndMethod))); // 8字节
end;

 

posted @ 2015-09-26 23:48  findumars  Views(807)  Comments(0)    收藏  举报