Delphi实现全局鼠标钩子

其中涉及到的一些API,网上均能查到详细的解释,这里不再熬述。源码下载

因为是全局钩子,所以要用dll注入。用到的鼠标消息结构如下:

[delphi] view plaincopy
 
  1. PMouseHookStruct = ^TMouseHookStruct;  
  2. {$EXTERNALSYM tagMOUSEHOOKSTRUCT}  
  3. tagMOUSEHOOKSTRUCT = packed record  
  4.   pt: TPoint;  
  5.   hwnd: HWND;  
  6.   wHitTestCode: UINT;  
  7.   dwExtraInfo: DWORD;  
  8. end;  
  9. TMouseHookStruct = tagMOUSEHOOKSTRUCT;  

DLL代码,Mouse_HookDLL

[delphi] view plaincopy
 
  1. library Mouse_HookDLL;  
  2.   
  3. { Important note about DLL memory management: ShareMem must be the 
  4.   first unit in your library's USES clause AND your project's (select 
  5.   Project-View Source) USES clause if your DLL exports any procedures or 
  6.   functions that pass strings as parameters or function results. This 
  7.   applies to all strings passed to and from your DLL--even those that 
  8.   are nested in records and classes. ShareMem is the interface unit to 
  9.   the BORLNDMM.DLL shared memory manager, which must be deployed along 
  10.   with your DLL. To avoid using BORLNDMM.DLL, pass string information 
  11.   using PChar or ShortString parameters. }  
  12.   
  13. uses  
  14.   SysUtils,  
  15.   Windows,  
  16.   Messages,  
  17.   Classes;  
  18.   
  19. {$R *.res}  
  20.   
  21. var  
  22.   NextHook : HHook;  
  23.   //调用者的Handle,用来给其发消息  
  24.   CallHandle : HWND;  
  25.   //通知调用者的消息,由调用者传进来  
  26.   MessageID : Word;  
  27.   
  28. //挂钩子函数 ,这里只处理鼠标移动,其他的鼠标动作,道理一样  
  29. function HookProc(code:Integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;  
  30. begin  
  31.   Result := 0;  
  32.   if code < then  
  33.     Result := CallNextHookEx(NextHook,code,wParam,lParam);  
  34.   case wParam of  
  35.     WM_NCMOUSEMOVE,WM_MOUSEMOVE:  
  36.     begin  
  37.       //给调用者发消息  
  38.       SendMessage(CallHandle,MessageID,wParam,Integer(@pMouseHookStruct(lParam)^));  
  39.     end;  
  40.   end;  
  41. end;  
  42.   
  43. //启动钩子  
  44. function StartHook(MsgID:Word):Bool;stdcall;  
  45. begin  
  46.   Result := False;  
  47.   if NextHook <> then  
  48.     Exit;  
  49.   MessageID := MsgID;  
  50.   //挂钩,SetWindowsHookEx的参数dwThreadId=0,表示挂全局的,不知道为什么,我系统是2003,用WH_MOUSE只能在本进程中实现钩子,WH_MOUSE_LL可以实现全局,在Delphi7中,是没有WH_MOUSE_LL定义的,你可以自己定义,值是14  
  51.   NextHook := SetWindowsHookEx(WH_MOUSE_LL,@HookProc,HInstance,0);  
  52.   Result := NextHook <> 0;  
  53. end;  
  54.   
  55. //脱钩  
  56. function StopHook:Bool;stdcall;  
  57. begin  
  58.   if NextHook <> then  
  59.   begin  
  60.     UnHookWindowsHookEx(NextHook);  
  61.     NextHook := 0;  
  62.   end;  
  63.   Result := NextHook = 0;  
  64. end;  
  65.   
  66. //传递调用者句柄  
  67. procedure SetCallHandle(sender:HWND);stdcall;  
  68. begin  
  69.   CallHandle := sender;  
  70.   NextHook := 0;  
  71. end;  
  72.   
  73. exports  
  74.   StartHook name 'StartHook',  
  75.   StopHook name 'StopHook',  
  76.   SetCallHandle name 'SetCallHandle';  
  77.   
  78. begin  
  79. end.  

调用者代码,HookTest

[delphi] view plaincopy
 
  1. unit HookTest;  
  2.   
  3. interface  
  4.   
  5. uses  
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  
  7.   Dialogs, StdCtrls;  
  8.   
  9. type  
  10.   TfrmHookTest = class(TForm)  
  11.     Label1: TLabel;  
  12.     procedure FormCreate(Sender: TObject);  
  13.     procedure FormClose(Sender: TObject; var Action: TCloseAction);  
  14.   private  
  15.     { Private declarations }  
  16.     //重载消息处理  
  17.     procedure WndProc(var Message: TMessage);override;  
  18.   public  
  19.     { Public declarations }  
  20.   end;  
  21.   
  22. var  
  23.   frmHookTest: TfrmHookTest;  
  24.   
  25. const  
  26.   WM_TestMsg = WM_User + 100;  
  27.   
  28. implementation  
  29.   
  30. {$R *.dfm}  
  31. function StartHook(MsgID:Word):Bool;stdcall;external 'Mouse_HookDLL.dll';  
  32. function StopHook:Bool;stdcall;external 'Mouse_HookDLL.dll';  
  33. procedure SetCallHandle(sender:HWND);stdcall;external 'Mouse_HookDLL.dll';  
  34.   
  35. procedure TfrmHookTest.FormClose(Sender: TObject; var Action: TCloseAction);  
  36. begin  
  37.   StopHook;  
  38. end;  
  39.   
  40. procedure TfrmHookTest.FormCreate(Sender: TObject);  
  41. begin  
  42.   SetCallHandle(Self.Handle);  
  43.   if not StartHook(WM_TestMsg) then  
  44.   begin  
  45.     ShowMessage('挂钩失败!');  
  46.   end;  
  47. end;  
  48.   
  49. procedure TfrmHookTest.WndProc(var Message: TMessage);  
  50. var  
  51.   x,y:integer;  
  52. begin  
  53.   //得到符合条件的钩子  
  54.   if Message.Msg = WM_TestMsg then  
  55.   begin  
  56.     x := pMouseHookStruct(Message.LParam)^.pt.X;  
  57.     y := pMouseHookStruct(Message.LParam)^.pt.Y;  
  58.     //显示x,y坐标  
  59.     Self.Label1.Caption := '鼠标当前位置:x='+IntToStr(x)+' : y='+IntToStr(y);  
  60.   end;  
  61.   inherited;  
  62. end;  
  63.   
  64. end.  

运行结果

http://blog.csdn.net/bdmh/article/details/5888287

原文地址:https://www.cnblogs.com/findumars/p/5001985.html