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;
|
请发表评论