Some of the APIs involved can be found on the Internet for detailed explanations, which are no longer discussed here. SOURCE download
Because it is a global hook, it is injected with a DLL. The mouse message structure used is as follows:
[Delphi]View Plaincopy
- Pmousehookstruct = ^tmousehookstruct;
- {$EXTERNALSYM Tagmousehookstruct}
- Tagmousehookstruct = packed record
- Pt:tpoint;
- Hwnd:hwnd;
- Whittestcode:uint;
- Dwextrainfo:dword;
- End
- Tmousehookstruct = tagmousehookstruct;
DLL code, Mouse_hookdll
[Delphi]View Plaincopy
- Library Mouse_hookdll;
- {Important Note about DLL memory Management:sharemem must is the
- First unit in your library ' s USES clause and your project ' s (select
- Project-view Source) USES clause If your DLL exports any procedures or
- Functions that pass strings as parameters or function results. This
- Applies to all strings passed to and from your dll--even those
- is nested in records and classes. SHAREMEM is the interface unit to
- The BORLNDMM. DLL shared Memory manager, which must be deployed along
- With your DLL. To avoid using BORLNDMM. DLL, pass string information
- Using PChar or shortstring parameters. }
- Uses
- Sysutils,
- Windows
- Messages,
- Classes;
- {$R *.res}
- Var
- Nexthook:hhook;
- //caller's handle, used to send messages to it
- Callhandle:hwnd;
- //Notifies the caller of the message, which is passed in by the caller
- Messageid:word;
- Hook Sub-function, here only handle mouse movement, other mouse actions, the same reason
- function HookProc (code:integer;wparam:wparam;lparam:lparam): Lresult;stdcall;
- Begin
- Result: = 0;
- if code < 0 Then
- Result: = CallNextHookEx (Nexthook,code,wparam,lparam);
- Case WParam of
- Wm_ncmousemove,wm_mousemove:
- begin
- //Send a message to the caller
- SendMessage (Callhandle,messageid,wparam,integer (@pMouseHookStruct (lParam) ^));
- end;
- end;
- End
- Start Hook
- function Starthook (Msgid:word): Bool;stdcall;
- Begin
- Result: = False;
- if Nexthook <> 0 Then
- Exit;
- MessageID: = MsgID;
- //hooks, SetWindowsHookEx parameters dwthreadid=0, indicating the hanging global, do not know why, my system is 2003, with Wh_mouse can only be implemented in this process hook, WH_MOUSE_LL can achieve the global, In Delphi7, there is no wh_mouse_ll definition, you can define it yourself, the value is
- Nexthook: = SetWindowsHookEx (Wh_mouse_ll, @HookProc, hinstance,0);
- Result: = Nexthook <> 0;
- End
- Decoupling
- function Stophook:bool;stdcall;
- Begin
- if Nexthook <> 0 Then
- begin
- UnhookWindowsHookEx (Nexthook);
- Nexthook: = 0;
- end;
- Result: = Nexthook = 0;
- End
- Passing the caller handle
- Procedure Setcallhandle (Sender:hwnd); stdcall;
- Begin
- Callhandle: = sender;
- Nexthook: = 0;
- End
- Exports
- Starthook name ' Starthook ',
- Stophook name ' Stophook ',
- Setcallhandle name ' Setcallhandle ';
- Begin
- End.
Caller Code, Hooktest
[Delphi]View Plaincopy
- Unit hooktest;
- Interface
- Uses
- Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
- Dialogs, Stdctrls;
- Type
- Tfrmhooktest = Class (Tform)
- Label1:tlabel;
- procedure formcreate (sender:tobject);
- procedure Formclose (sender:tobject; var action:tcloseaction);
- Private
- {Private declarations}
- //Overloaded message processing
- procedure WndProc (var message:tmessage); override;
- Public
- {public declarations}
- end;
- Var
- Frmhooktest:tfrmhooktest;
- Const
- wm_testmsg = wm_user + 100;
- Implementation
- {$R *.DFM}
- function Starthook (msgid:word): bool;stdcall;external ' Mouse_hookdll.dll ';
- function stophook:bool;stdcall;external ' mouse_hookdll.dll ';
- Procedure Setcallhandle (Sender:hwnd); stdcall;external ' Mouse_hookdll.dll ';
- Procedure Tfrmhooktest. Formclose (Sender:tobject; var action:tcloseaction);
- Begin
- Stophook;
- End
- Procedure Tfrmhooktest. Formcreate (Sender:tobject);
- Begin
- Setcallhandle (self. Handle);
- If not starthook (wm_testmsg) Then
- begin
- ShowMessage (' hook failed! ‘);
- end;
- End
- Procedure Tfrmhooktest. WndProc (var message:tmessage);
- Var
- X, y:integer;
- Begin
- //Get the hook that matches the condition
- if Message. MSG = wm_testmsg Then
- begin
- x: = Pmousehookstruct (Message. LParam) ^.pt. X
- Y: = pmousehookstruct (Message. LParam) ^.pt. Y
- //display x, y coordinates
- Self. Label1. Caption: = ' mouse current position: x= ' +inttostr (x) +': y= ' +inttostr (y);
- end;
- inherited;
- End
- End.
Run results
http://blog.csdn.net/bdmh/article/details/5888287
Delphi implements global Mouse hooks