判断鼠标是否移动(50分)

  • 主题发起人 主题发起人 youcan
  • 开始时间 开始时间
Y

youcan

Unregistered / Unconfirmed
GUEST, unregistred user!
我正在做一个程序,要求在一定的时间范围内如果没有人移动鼠标,则系统<br>自动调用另外的过程。不知如何判断鼠标是否在动,拜托各位高手!最好详细些!
 
自己定义一个WINDOWS消息处理涵数来接收WM_MOUSEMOVE的消息
 
能详细些吗?<br>我对这个不是很了解!
 
用一个APPLICATIONEVENT控件接受所有消息,看是否有鼠标移动的消息
 
unit udll;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;<br>var<br>&nbsp;myhook:Thandle;<br>procedure starthook;stdcall;export;<br>procedure unhook;stdcall;export;<br>function myHookProc(iCode: Integer; wParam: WPARAM; lParam: LPARAM):LRESULT;stdcall;<br>procedure log(ss:string);<br><br><br>implementation<br>procedure starthook;stdcall;<br>begin<br>myhook:=SetWindowsHookEx(WH_MOUSE,myHookProc,hInstance,0);<br>end;<br><br>function myHookProc(iCode: Integer; wParam: WPARAM; lParam: LPARAM):LRESULT;stdcall;<br>var<br>&nbsp; X, Y : Integer;<br>begin<br>result:=0;<br>if iCode&lt;0 then <br>&nbsp;begin<br>&nbsp; result := CallNextHookEx(myHook, iCode, wParam, lParam);<br>&nbsp; exit;<br>&nbsp;end;<br>if (wParam = WM_LBUTTONDOWN) or (wParam=WM_NCLBUTTONDOWN) then<br>begin<br>&nbsp; X := PMouseHookStruct(lParam).pt.X;<br>&nbsp; Y := PMouseHookStruct(lParam).pt.Y;<br>&nbsp; log(#13+#10+'leftbutton click at' +'['+ inttostr(X) + ';' + inttostr(Y)+']');<br>end;<br>if (wParam = WM_NCMouseMove) or (wParam=WM_MOUSEMOVE) then<br>begin<br>&nbsp; X := PMouseHookStruct(lParam).pt.X;<br>&nbsp; Y := PMouseHookStruct(lParam).pt.Y;<br>&nbsp; log(' &nbsp;['+inttostr(X) + ';' + inttostr(Y)+']');<br>end<br><br><br>result := CallNextHookEx(myHook, iCode, wParam, lParam);<br>end;<br>procedure unhook;stdcall;export;<br>begin<br>&nbsp;unhookwindowshookex(myhook);<br>end;<br>procedure log(ss:string);<br>var<br>&nbsp;file1:Tmemorystream;<br>begin<br>&nbsp;try<br>&nbsp; file1:=tmemorystream.Create;<br>&nbsp; try<br>&nbsp; file1.LoadFromFile('e:/delphi/log.txt');<br>&nbsp; except<br>&nbsp; end;<br>&nbsp; file1.Position:=file1.size;<br>&nbsp; file1.Write(ss[1],length(ss));<br>&nbsp; file1.SaveToFile('e:/delphi/log.txt');<br>&nbsp;finally<br>&nbsp; file1.free;<br>&nbsp;end;<br>end;<br>end.<br><br><br>library prjdll;<br><br>uses<br>&nbsp; udll in 'udll.pas';<br><br>{$R *.RES}<br><br>exports<br>&nbsp;starthook,unhook;<br>begin<br>end.<br>
 
最简单的就是先capture,再用定时器判断。
 
两种方式:<br>1. 用mousehook<br>2. 用定时器,定时调用getcursorpos判断鼠标坐标是否改变(一般设置25-50ms定时器周期对系统几乎没有影响)<br><br>这两种方法都不必理会鼠标是否在自己程序的窗口范围内
 
后退
顶部