Delphi實現全局鼠標鉤子

其中涉及到的一些API,網上均能查到詳細的解釋,這裏再也不熬述。源碼下載app

 

由於是全局鉤子,因此要用dll注入。用到的鼠標消息結構以下:ide

 

 

[delphi]  view plain copy
 
  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 plain copy
 
  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.  

 

 

調用者代碼,HookTestspa

 

[delphi]  view plain copy
 
  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.  

運行結果.net

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

相關文章
相關標籤/搜索