本文實例講述了Delphi實現(xiàn)窗體感知鼠標(biāo)滑過并自動隱藏與顯示窗口的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
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; //為保證下拉窗體后呈現(xiàn)在最前面 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;//依賴定時器定時檢查鼠標(biāo)是否還在窗體范圍內(nèi),//這樣此能避免因為快速移動鼠標(biāo)而丟失MOUSELEAVE事件procedure TfrmMain.Timer1Timer(Sender: TObject);var rc:TRECT; pt:TPOINT;begin GetWindowRect(self.Handle,rc); //取窗體的矩形區(qū)域 GetCursorPos(pt); //取得當(dāng)前鼠標(biāo)所在位置 if(not PtInRect(rc,pt)) then //如果鼠標(biāo)不在窗體范圍內(nèi) begin if(Top = 0) then //如果目前窗體正吸附在屏幕上沿,則上移隱藏窗體 begin Top := 0-Height+2; end; Timer1.Enabled := False; //窗體隱藏后定時器關(guān)閉 SetWindowPos(Handle,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE or SWP_NOSIZE); //將窗體推到最前 end;end;
希望本文所述對大家的Delphi程序設(shè)計有所幫助。
新聞熱點
疑難解答
圖片精選