统计点击次数(100分)

  • 主题发起人 主题发起人 cuihongwei
  • 开始时间 开始时间
C

cuihongwei

Unregistered / Unconfirmed
GUEST, unregistred user!
代码:
想统计一下鼠标左键、右键、左键双击的点击次数,如何实现?
 
下鼠标钩子
 
在一个app中? 还是单个form?<br><br>单个form的话,申明三个变量, 分别记录左键、右键、左键双击的点击次数, 每个对应的事件书写增1语句。。
 
想让一个app统计鼠标点击次数(统计所有的),我也知道应该使用鼠标钩子,但我初学delphi,谁能给我一个较完整的例子,谢谢
 
用的低级鼠标钩子,代码自己测试成功<br><br>钩子单元代码:<br>unit Hook;<br><br>interface<br><br>uses Windows, Messages, SysUtils;<br><br>function InstallHook:boolean;<br>function UnInstallHook:boolean;<br><br>implementation<br>uses Main;<br><br>const WH_MOUSE_LL=14;<br>var &nbsp; hMouse:HHook=0;<br><br>function MouseProc(nCode:Integer;WParam:WPARAM;LParam:LPARAM):LRESULT;stdcall;<br>begin<br> &nbsp;Result:=0;<br> &nbsp;if (nCode=HC_ACTION) then<br> &nbsp;begin<br> &nbsp; &nbsp;case wParam of<br> &nbsp; &nbsp; &nbsp;WM_LBUTTONDOWN:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;m:=m+1;<br> &nbsp; &nbsp; &nbsp;WM_LBUTTONUP:<br> &nbsp; &nbsp; &nbsp; &nbsp;FrmMain.tex_l.Text:=IntToStr(m);<br> &nbsp; &nbsp; &nbsp;WM_RBUTTONDOWN:<br> &nbsp; &nbsp; &nbsp; &nbsp;n:=n+1;<br> &nbsp; &nbsp; &nbsp;WM_RBUTTONUP:<br> &nbsp; &nbsp; &nbsp; &nbsp;FrmMain.tex_r.Text:=IntToStr(n);<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;Result:=CallNextHookEx(0, nCode, WParam, LParam);<br> &nbsp;end;<br>end;<br><br>function InstallHook: boolean;<br>begin<br> &nbsp;if hMouse=0 then<br> &nbsp; &nbsp; hMouse:=SetWindowsHookEx(WH_MOUSE_LL,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MouseProc,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HInstance,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0);<br> &nbsp;Result:=hMouse&lt;&gt;0;<br>end;<br><br>function UnInstallHook:boolean;<br>begin<br> &nbsp;if hMouse &lt;&gt; 0 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;UnHookWindowsHookEx(hMouse);<br> &nbsp; &nbsp; &nbsp;hMouse:=0;<br> &nbsp; &nbsp;end;<br> &nbsp;Result:=True;<br>end;<br>end.<br><br>窗体代码:<br>unit Main;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls;<br><br>type<br> &nbsp;TFrmMain = class(TForm)<br> &nbsp; &nbsp;Label1: TLabel;<br> &nbsp; &nbsp;tex_l: TEdit;<br> &nbsp; &nbsp;Label2: TLabel;<br> &nbsp; &nbsp;tex_r: TEdit;<br> &nbsp; &nbsp;bt_rec: TButton;<br> &nbsp; &nbsp;bt_stop: TButton;<br> &nbsp; &nbsp;procedure bt_recClick(Sender: TObject);<br> &nbsp; &nbsp;procedure bt_stopClick(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;FrmMain: TFrmMain;<br> &nbsp;m,n:integer;<br>implementation<br>uses Hook;<br>{$R *.dfm}<br><br>procedure TFrmMain.bt_recClick(Sender: TObject);<br>begin<br> &nbsp;InstallHook;<br>end;<br><br>procedure TFrmMain.bt_stopClick(Sender: TObject);<br>begin<br> &nbsp;UnInstallHook;<br>end;<br><br>end.
 
谢谢大家<br>我回去试试
 
后退
顶部