下面是我做的一个改进TButton的控件,因该对你有所帮助,你可以用同样的方法改进TPanel.
unit Button1;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type TButton1 = class(TButton)
private FOnMouseLeave: TNotifyEvent;
procedure WZMouseLeave(var Msg:TMessage); message CM_MOUSELEAVE;
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
published
property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave;
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
//在系统中注册控件
RegisterComponents('Samples', [TButton1]);
end;
{ TButton1 }
procedure TButton1.WZMouseLeave(var Msg: TMessage);
begin
inherited;//继承父类
if csLButtonDown in ControlState then
begin
Self.MouseUp(mbLeft,[ssLeft],0,0);
end;
if Assigned (FonMouseLeave) then FOnMouseLeave(Self);
end;
end.
代码添加完后,编译后,一个名为Button1的控件即加入Simples项,新建一个项目,试试这段代码:
procedure TForm1.Button11MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
label1.Caption:='在';
end;
procedure TForm1.Button11MouseLeave(Sender: TObject);
begin
label1.Caption:='不在';
end;