const
WM_MouseEnter = $B013;
WM_MouseLeave = $B014;
type
TfrmMain = class(TForm)
.
.
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
protected
procedure WMMouseEnter(var Msg: TMessage); message WM_MouseEnter;
end;
implementation
{$R *.dfm}
procedure TfrmMain.WMMouseEnter(var Msg: TMessage);
begin
if(Top<0) then
begin
Top := 0;
//為保證下拉窗體後呈現在最前面
SetWindowPos(Handle,HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE); //將窗體推到最前
SetWindowPos(Handle,HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE);//然後取消窗體最前
end;
Timer1.Enabled := True;
end;
//依賴定時器定時檢查鼠標是否還在窗體范圍內,這樣此能避免因為快速移動鼠標而丟失MOUSELEAVE事件
procedure TfrmMain.Timer1Timer(Sender: TObject);
var
rc:TRECT;
pt:TPOINT;
begin
GetWindowRect(self.Handle,rc); //取窗體的矩形區域
GetCursorPos(pt); //取得當前鼠標所在位置
if(not PtInRect(rc,pt)) then //如果鼠標不在窗體范圍內
begin
if(Top = 0) then //如果目前窗體正吸附在屏幕上沿,則上移隱藏窗體
begin
Top := 0-Height+2;
end;
Timer1.Enabled := False; //窗體隱藏後定時器關閉
SetWindowPos(Handle,HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE); //將窗體推到最前
end;
end;