Delphi TButton.OnClick 匿名函数用法

type
  TNotifyEventRef = reference to procedure(Sender: TObject);

function AnonymousEvent(const Proc: TNotifyEventRef): TNotifyEvent;
type
  TVtable = array[0..3] of Pointer;
  PVtable = ^TVtable;
  PPVtable = ^PVtable;
begin
  TMethod(Result).Code := PPVtable((@Proc)^)^^[3];
  TMethod(Result).Data := Pointer((@Proc)^);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  //变态写法
  @Button1.OnClick := PPointer(Cardinal(PPointer(
    procedure(Sender: TObject)
    begin
      ShowMessage('你好,人民币!');
    end)^) + $0C)^;

  //正经写法
  Button2.OnClick := AnonymousEvent(
    procedure(Sender: TObject)
    begin
      ShowMessage('Hello,Money!');
    end);
end;

 

posted @ 2019-06-14 14:06  塞翁失身  阅读(1191)  评论(0编辑  收藏  举报