unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> StdCtrls;<br><br>type<br> TForm1 = class(TForm)<br> Button1: TButton;<br> Button2: TButton;<br> procedure FormCreate(Sender: TObject);<br> procedure Button1Click(Sender: TObject);<br> procedure Button2Click(Sender: TObject);<br> procedure FormClose(Sender: TObject; var Action: TCloseAction);<br> private<br> { Private declarations }<br> Recorder: TStringList;<br> HookHandle: THandle;<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br> S: string;<br><br>implementation<br><br>{$R *.DFM}<br>function MouseProc(Code: Integer; W: WPARAM; L: LPARAM): LRESULT; stdcall;<br>begin<br> if Code < 0 then<br> Result := CallNextHookEx(Form1.HookHandle, Code, W, L)<br> else<br> begin<br> if W = WM_MOUSEMOVE then<br> begin<br> S := Format('Mouse Move: X = %d, Y = %d',<br> [PMouseHookStruct(L).pt.x, PMouseHookStruct(L).pt.y]);<br> Form1.Recorder.Add(S);<br> end<br> else if W = WM_LBUTTONDOWN then Form1.Recorder.Add('Left Button down')<br> Result := 0;<br> end;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> Recorder := TStringList.Create;<br> HookHandle := 0;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> if HookHandle = 0 then<br> HookHandle := SetWindowsHookEx(WH_MOUSE, @MouseProc, hInstance, 0);<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br> if HookHandle <> 0 then<br> begin<br> UnhookWindowsHookEx(HookHandle);<br> HookHandle := 0;<br> end;<br>end;<br><br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br> Recorder.SaveToFile('d:/temp/a.txt');<br> Recorder.Free;<br>end;<br><br>end.<br><br>我很少用Hook的,刚才看到这个问题,觉得挺有趣,就自己参考API帮助文档中<br>的相关内容,试着写了这段代码。虽然可以运行,但不知是否能满足你的要求?<br>