Delphi implements global Mouse hooks

Source: Internet
Author: User

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
    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 code, Mouse_hookdll

[Delphi]View Plaincopy
  1. Library Mouse_hookdll;
  2. {Important Note about DLL memory Management:sharemem must is the
  3. First unit in your library ' s USES clause and your project ' s (select
  4. Project-view Source) USES clause If your DLL exports any procedures or
  5. Functions that pass strings as parameters or function results. This
  6. Applies to all strings passed to and from your dll--even those
  7. is nested in records and classes. SHAREMEM is the interface unit to
  8. The BORLNDMM. DLL shared Memory manager, which must be deployed along
  9. With your DLL. To avoid using BORLNDMM. DLL, pass string information
  10. Using PChar or shortstring parameters. }
  11. Uses
  12. Sysutils,
  13. Windows
  14. Messages,
  15. Classes;
  16. {$R *.res}
  17. Var
  18. Nexthook:hhook;
  19. //caller's handle, used to send messages to it
  20. Callhandle:hwnd;
  21. //Notifies the caller of the message, which is passed in by the caller
  22. Messageid:word;
  23. Hook Sub-function, here only handle mouse movement, other mouse actions, the same reason
  24. function HookProc (code:integer;wparam:wparam;lparam:lparam): Lresult;stdcall;
  25. Begin
  26. Result: = 0;
  27. if code < 0 Then
  28. Result: = CallNextHookEx (Nexthook,code,wparam,lparam);
  29. Case WParam of
  30. Wm_ncmousemove,wm_mousemove:
  31. begin
  32. //Send a message to the caller
  33. SendMessage (Callhandle,messageid,wparam,integer (@pMouseHookStruct (lParam) ^));
  34. end;
  35. end;
  36. End
  37. Start Hook
  38. function Starthook (Msgid:word): Bool;stdcall;
  39. Begin
  40. Result: = False;
  41. if Nexthook <> 0 Then
  42. Exit;
  43. MessageID: = MsgID;
  44. //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
  45. Nexthook: = SetWindowsHookEx (Wh_mouse_ll, @HookProc, hinstance,0);
  46. Result: = Nexthook <> 0;
  47. End
  48. Decoupling
  49. function Stophook:bool;stdcall;
  50. Begin
  51. if Nexthook <> 0 Then
  52. begin
  53. UnhookWindowsHookEx (Nexthook);
  54. Nexthook: = 0;
  55. end;
  56. Result: = Nexthook = 0;
  57. End
  58. Passing the caller handle
  59. Procedure Setcallhandle (Sender:hwnd); stdcall;
  60. Begin
  61. Callhandle: = sender;
  62. Nexthook: = 0;
  63. End
  64. Exports
  65. Starthook name ' Starthook ',
  66. Stophook name ' Stophook ',
  67. Setcallhandle name ' Setcallhandle ';
  68. Begin
  69. End.

Caller Code, Hooktest

[Delphi]View Plaincopy
  1. Unit hooktest;
  2. Interface
  3. Uses
  4. Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
  5. Dialogs, Stdctrls;
  6. Type
  7. Tfrmhooktest = Class (Tform)
  8. Label1:tlabel;
  9. procedure formcreate (sender:tobject);
  10. procedure Formclose (sender:tobject;  var action:tcloseaction);
  11. Private
  12. {Private declarations}
  13. //Overloaded message processing
  14. procedure WndProc (var message:tmessage); override;
  15. Public
  16. {public declarations}
  17. end;
  18. Var
  19. Frmhooktest:tfrmhooktest;
  20. Const
  21. wm_testmsg = wm_user + 100;
  22. Implementation
  23. {$R *.DFM}
  24. function Starthook (msgid:word): bool;stdcall;external ' Mouse_hookdll.dll ';
  25. function stophook:bool;stdcall;external ' mouse_hookdll.dll ';
  26. Procedure Setcallhandle (Sender:hwnd); stdcall;external ' Mouse_hookdll.dll ';
  27. Procedure Tfrmhooktest. Formclose (Sender:tobject;  var action:tcloseaction);
  28. Begin
  29. Stophook;
  30. End
  31. Procedure Tfrmhooktest.  Formcreate (Sender:tobject);
  32. Begin
  33. Setcallhandle (self.  Handle);
  34. If not starthook (wm_testmsg) Then
  35. begin
  36. ShowMessage (' hook failed!  ‘);
  37. end;
  38. End
  39. Procedure Tfrmhooktest.  WndProc (var message:tmessage);
  40. Var
  41. X, y:integer;
  42. Begin
  43. //Get the hook that matches the condition
  44. if Message. MSG = wm_testmsg Then
  45. begin
  46. x: = Pmousehookstruct (Message. LParam) ^.pt.  X
  47. Y: = pmousehookstruct (Message. LParam) ^.pt.  Y
  48. //display x, y coordinates
  49. Self. Label1.  Caption: = ' mouse current position: x= ' +inttostr (x) +': y= ' +inttostr (y);
  50. end;
  51. inherited;
  52. End
  53. End.

Run results

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

Delphi implements global Mouse hooks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.