先再Form1.OnMouseMove中,对一个全局变量(boolean型)赋值(如false)。然后,你可以加一个timer,不断设变量为true, if abc= false then abc:=true; 另加一个timer,判断abc,若累计一段时间后,abc始终为true,则说明鼠标到了窗体之外。(当然,timer2的时间设置不可太长,0.3秒-0.5秒差不多)成了的话,给我分,我有急用
可以试一下,但是仍不理想; private Mark: Boolean; procedure AppMessage(var Msg: tagMSG; var Handled: Boolean); { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject); begin Mark := False; Application.OnMessage := AppMessage; end;procedure TForm1.AppMessage(var Msg: tagMSG; var Handled: Boolean); var tme: TTRACKMOUSEEVENT; pt: TPoint; begin if Msg.message = WM_MOUSEMOVE then begin if not Mark then begin Mark := True; tme.cbSize := SizeOf(TTRACKMOUSEEVENT); tme.dwFlags := TME_LEAVE; tme.hwndTrack := Handle; TrackMouseEvent(tme); end; end; if Msg.message = WM_MOUSELEAVE then begin Mark := False; GetCursorPos(pt); if not PtInRect(BoundsRect, pt) then Caption := 'Mouse Leave: ' + IntToStr(GetTickCount); end; end;
上面的代码有点问题,因为TrackMouseEvent是Track Mouse是否离开Client, 改一下上面的代码, if not PtInRect(ClientRect, ScreenToClient(pt)) then 这样稍微有点误差,因为不是BoundsRect,而是ClientRect。
if abc= false then
abc:=true;
另加一个timer,判断abc,若累计一段时间后,abc始终为true,则说明鼠标到了窗体之外。(当然,timer2的时间设置不可太长,0.3秒-0.5秒差不多)成了的话,给我分,我有急用
Mark: Boolean;
procedure AppMessage(var Msg: tagMSG; var Handled: Boolean);
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
Mark := False;
Application.OnMessage := AppMessage;
end;procedure TForm1.AppMessage(var Msg: tagMSG;
var Handled: Boolean);
var
tme: TTRACKMOUSEEVENT;
pt: TPoint;
begin
if Msg.message = WM_MOUSEMOVE then
begin
if not Mark then
begin
Mark := True;
tme.cbSize := SizeOf(TTRACKMOUSEEVENT);
tme.dwFlags := TME_LEAVE;
tme.hwndTrack := Handle;
TrackMouseEvent(tme);
end;
end;
if Msg.message = WM_MOUSELEAVE then
begin
Mark := False;
GetCursorPos(pt);
if not PtInRect(BoundsRect, pt) then
Caption := 'Mouse Leave: ' + IntToStr(GetTickCount);
end;
end;
改一下上面的代码,
if not PtInRect(ClientRect, ScreenToClient(pt)) then
这样稍微有点误差,因为不是BoundsRect,而是ClientRect。
在OnMouseMove利用GetCursorPos来判断