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;
相關文章
相關標籤/搜索