本文實例講述了Delphi實作窗體感知滑鼠滑過並自動隱藏與顯示視窗的方法。分享給大家供大家參考。具體實作方法如下:
const WM_MouseEnter = $B013; WM_MouseLeave = $B014;type TfrmMain = class(TForm) . . Timer1: TTimer; procedure Timer1Timer(Sender: TObject); protected procedure WMMouseer(var): EnterTMage; 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 或 SWP_NOSIZE); //將窗體推到最前end;end;
希望本文所述對大家的Delphi程式設計有幫助。