用的低级鼠标钩子,代码自己测试成功<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 hMouse:HHook=0;<br><br>function MouseProc(nCode:Integer;WParam:WPARAM;LParam:LPARAM):LRESULT;stdcall;<br>begin<br> Result:=0;<br> if (nCode=HC_ACTION) then<br> begin<br> case wParam of<br> WM_LBUTTONDOWN:<br> m:=m+1;<br> WM_LBUTTONUP:<br> FrmMain.tex_l.Text:=IntToStr(m);<br> WM_RBUTTONDOWN:<br> n:=n+1;<br> WM_RBUTTONUP:<br> FrmMain.tex_r.Text:=IntToStr
;<br> end;<br> Result:=CallNextHookEx(0, nCode, WParam, LParam);<br> end;<br>end;<br><br>function InstallHook: boolean;<br>begin<br> if hMouse=0 then<br> hMouse:=SetWindowsHookEx(WH_MOUSE_LL,<br> MouseProc,<br> HInstance,<br> 0);<br> Result:=hMouse<>0;<br>end;<br><br>function UnInstallHook:boolean;<br>begin<br> if hMouse <> 0 then<br> begin<br> UnHookWindowsHookEx(hMouse);<br> hMouse:=0;<br> end;<br> Result:=True;<br>end;<br>end.<br><br>窗体代码:<br>unit Main;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls;<br><br>type<br> TFrmMain = class(TForm)<br> Label1: TLabel;<br> tex_l: TEdit;<br> Label2: TLabel;<br> tex_r: TEdit;<br> bt_rec: TButton;<br> bt_stop: TButton;<br> procedure bt_recClick(Sender: TObject);<br> procedure bt_stopClick(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> FrmMain: TFrmMain;<br> m,n:integer;<br>implementation<br>uses Hook;<br>{$R *.dfm}<br><br>procedure TFrmMain.bt_recClick(Sender: TObject);<br>begin<br> InstallHook;<br>end;<br><br>procedure TFrmMain.bt_stopClick(Sender: TObject);<br>begin<br> UnInstallHook;<br>end;<br><br>end.