SetWindowsHookEx 是启动 Hook 功能,
SetWindowsHookEx(WM_MOUSEMOVE, ...对WM_MOUSEMOVE消息进行hook
CallNextHookEx() 将截获的消息往下一个消息接收地传送,
UnHookWindowsHookEx() 解除Hook功能 ,
例子: 这个只是在一般的application中的例子, 若要截获applicationForm之外的
消息, Hook必须放在dll程序中. dll 中要有启动和撤销功能.
var
Form1: TForm1;
YourHook: HHOOK;
function YourHookProc(Code: Integer; WParam: Longint; var Msg: TMsg): Longint; stdcall;
implementation
{$R *.DFM}
function YourHookProc(Code: Integer; WParam: Longint; var Msg: TMsg): Longint; stdcall;
begin
if Msg.Message = WM_MOUSEMOVE then begin
form1.Edit1.text := inttostr(msg.wParam);//做你要做的事
end
Result := CallNextHookEx(YourHook, Code, WParam, Longint(@Msg)); //放出消息给下一个.
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
YourHook := SetWindowsHookEx(WH_GETMESSAGE, @YourHookProc, 0, GetCurrentThreadID); //
end;