重载它的
procedure WndProc(var Message: TMessage); override;
不过这样你要先派生button的子类;

解决方案 »

  1.   

    另外没有必要写
    button1.free;
    窗体摧毁的时候就自动销毁button了,不用你维护。
      

  2.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
        FButton: TButton;
        FOldWndProc: TWndMethod;
        procedure NewWndProc(var Msg: TMessage);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    begin
      FButton := TButton.Create(Self);
      FButton.Parent := Self;
      FButton.Left := 100;
      FButton.Top := 100;
      FButton.Caption := 'FButton';
      FOldWndProc := FButton.WindowProc;
      FButton.WindowProc := NewWndProc;
    end;procedure TForm1.NewWndProc(var Msg: TMessage);
    begin
      case Msg.Msg of
        CM_MOUSEENTER:
          Caption := 'FButtonMouseEnter';
        CM_MOUSELEAVE:
          Caption := 'FButtonMouseLeave';
      end;
      FOldWndProc(Msg);
    end;end.