TmyButton=class  //用来创建一个我的按钮类。
  private
    Fhandle: THandle;
    FDefWndProc: Pointer;
    iWindowLog: Integer;    
  protected
    procedure WndProc(var msg: TMessage);
  public
    property handle: THandle read Fhandle write Fhandle;
    constructor Create(AOwner: TComponent);
    destructor Destroy();override;
  end;implementationprocedure TForm1.FormCreate(Sender: TObject);
begin
  myButton := TmyButton.Create(Self);
end;constructor TmyButton.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Fhandle := CreateWindow('button', 'button', WS_CHILD or BS_PUSHBUTTON or WS_VISIBLE, 20, 20, 175, 25, Form1.Handle, 0, HInstance, 0);
  FDefWndProc :=classes.MakeObjectInstance(WndProc);  //使回调函数能进入类方法中
  iWindowLog := SetWindowLong(Fhandle, GWL_WNDPROC, LongInt(FDefWndProc));
  ShowWindow(Fhandle, 5);
end;destructor TmyButton.Destroy;
begin
  inherited;
end;procedure TmyButton.WndProc(var msg: TMessage);
begin
    Form1.Memo1.Lines.Add(IntToStr(TMessage(msg).LParamHi));
    if TMessage(msg).LParamHi = WM_LBUTTONDOWN then  //为什么用TMessage(msg).msg不行
    begin
       ShowMessage('WM_LBUTTONDOWN');
       exit;
    end;
    CallWindowProc(Pointer(iWindowLog), Fhandle, TMessage(msg).msg, TMessage(msg).wParam, TMessage(msg).lParam);
end;
这是创建一个按钮的类,并设置了类的回调函数为类方法,但delphi中的TMessage(msg).msg是代表了消息的类别,和下面的类似,但我的却不行,不知为何?
  function MyFun(hwnd: Hwnd; Msg: UINT; wParam: WPARAM;lParam: LPARAM): LRESULT; stdcall;
  begin
    if Msg = WM_LBUTTONDOWN then  //msg 代表了消息的类别
    begin
       ShowMessage('WM_LBUTTONDOWN');
       exit;
    end;
    Result := CallWindowProc(Pointer(iWindowLog), hwnd, msg, wParam, lParam);
  end;

解决方案 »

  1.   

    补充,类声明为
    TmyButton=class(TComponent)  //用来创建一个我的按钮类。
      

  2.   

    还有,怎样把消息发到这函数中去呢?
      WM_MYNOTIFY = WM_USER + 101;//自定义消息
     procedure MyNotify(var message: TMessage); message WM_MYNOTIFY;  //消息函数声明
    ...//实现
    procedure TMyButton.MyNotify(var message: TMessage);
    var
      S: String;
    begin
      with TMessage(Message) do
         S := 'MyNotify :msg =' + IntToStr(msg) + ',WParam=' + IntToStr(WParam) + ',LParam=' + IntToStr(LParam);
      ShowMessage(S);
    end;我的想法是实现类似于VCL的消息分派,这有感兴趣的高手吗?