MouseHook.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-08-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="MouseHook.cs">
  7. // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
  8. // </copyright>
  9. //
  10. // Blog: https://www.cnblogs.com/bfyx
  11. // GitHub:https://github.com/kwwwvagaa/NetWinformControl
  12. // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
  13. //
  14. // If you use this code, please keep this note.
  15. // ***********************************************************************
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using System.Runtime.InteropServices;
  20. using System.Text;
  21. using System.Windows.Forms;
  22. namespace HZH_Controls
  23. {
  24. /// <summary>
  25. /// 鼠标全局钩子
  26. /// </summary>
  27. public static class MouseHook
  28. {
  29. /// <summary>
  30. /// The wm mousemove
  31. /// </summary>
  32. private const int WM_MOUSEMOVE = 0x200;
  33. /// <summary>
  34. /// The wm lbuttondown
  35. /// </summary>
  36. private const int WM_LBUTTONDOWN = 0x201;
  37. /// <summary>
  38. /// The wm rbuttondown
  39. /// </summary>
  40. private const int WM_RBUTTONDOWN = 0x204;
  41. /// <summary>
  42. /// The wm mbuttondown
  43. /// </summary>
  44. private const int WM_MBUTTONDOWN = 0x207;
  45. /// <summary>
  46. /// The wm lbuttonup
  47. /// </summary>
  48. private const int WM_LBUTTONUP = 0x202;
  49. /// <summary>
  50. /// The wm rbuttonup
  51. /// </summary>
  52. private const int WM_RBUTTONUP = 0x205;
  53. /// <summary>
  54. /// The wm mbuttonup
  55. /// </summary>
  56. private const int WM_MBUTTONUP = 0x208;
  57. /// <summary>
  58. /// The wm lbuttondblclk
  59. /// </summary>
  60. private const int WM_LBUTTONDBLCLK = 0x203;
  61. /// <summary>
  62. /// The wm rbuttondblclk
  63. /// </summary>
  64. private const int WM_RBUTTONDBLCLK = 0x206;
  65. /// <summary>
  66. /// The wm mbuttondblclk
  67. /// </summary>
  68. private const int WM_MBUTTONDBLCLK = 0x209;
  69. /// <summary>
  70. /// 点
  71. /// </summary>
  72. [StructLayout(LayoutKind.Sequential)]
  73. public class POINT
  74. {
  75. /// <summary>
  76. /// The x
  77. /// </summary>
  78. public int x;
  79. /// <summary>
  80. /// The y
  81. /// </summary>
  82. public int y;
  83. }
  84. /// <summary>
  85. /// 钩子结构体
  86. /// </summary>
  87. [StructLayout(LayoutKind.Sequential)]
  88. public class MouseHookStruct
  89. {
  90. /// <summary>
  91. /// The pt
  92. /// </summary>
  93. public POINT pt;
  94. /// <summary>
  95. /// The h WND
  96. /// </summary>
  97. public int hWnd;
  98. /// <summary>
  99. /// The w hit test code
  100. /// </summary>
  101. public int wHitTestCode;
  102. /// <summary>
  103. /// The dw extra information
  104. /// </summary>
  105. public int dwExtraInfo;
  106. }
  107. // 全局的鼠标事件
  108. /// <summary>
  109. /// Occurs when [on mouse activity].
  110. /// </summary>
  111. public static event MouseEventHandler OnMouseActivity;
  112. /// <summary>
  113. /// The h mouse hook
  114. /// </summary>
  115. private static int _hMouseHook = 0; // 鼠标钩子句柄
  116. /// <summary>
  117. /// 启动全局钩子
  118. /// </summary>
  119. /// <exception cref="System.Exception">SetWindowsHookEx failed.</exception>
  120. /// <exception cref="Exception">SetWindowsHookEx failed.</exception>
  121. public static void Start()
  122. {
  123. // 安装鼠标钩子
  124. if (_hMouseHook != 0)
  125. {
  126. Stop();
  127. }
  128. // 生成一个HookProc的实例.
  129. WindowsHook.HookMsgChanged += WindowsHook_HookMsgChanged;
  130. _hMouseHook = WindowsHook.StartHook(HookType.WH_MOUSE_LL);
  131. //假设装置失败停止钩子
  132. if (_hMouseHook == 0)
  133. {
  134. Stop();
  135. }
  136. }
  137. static void WindowsHook_HookMsgChanged(string strHookName, int nCode, IntPtr msg, IntPtr lParam)
  138. {
  139. // 假设正常执行而且用户要监听鼠标的消息
  140. if (nCode >= 0 && OnMouseActivity != null)
  141. {
  142. MouseButtons button = MouseButtons.None;
  143. int clickCount = 0;
  144. switch ((int)msg)
  145. {
  146. case WM_LBUTTONDOWN:
  147. button = MouseButtons.Left;
  148. clickCount = 1;
  149. break;
  150. case WM_LBUTTONUP:
  151. button = MouseButtons.Left;
  152. clickCount = 1;
  153. break;
  154. case WM_LBUTTONDBLCLK:
  155. button = MouseButtons.Left;
  156. clickCount = 2;
  157. break;
  158. case WM_RBUTTONDOWN:
  159. button = MouseButtons.Right;
  160. clickCount = 1;
  161. break;
  162. case WM_RBUTTONUP:
  163. button = MouseButtons.Right;
  164. clickCount = 1;
  165. break;
  166. case WM_RBUTTONDBLCLK:
  167. button = MouseButtons.Right;
  168. clickCount = 2;
  169. break;
  170. }
  171. if (button != MouseButtons.None && clickCount > 0)
  172. {
  173. // 从回调函数中得到鼠标的信息
  174. MouseHookStruct MyMouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
  175. MouseEventArgs e = new MouseEventArgs(button, clickCount, MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y, 0);
  176. OnMouseActivity(null, e);
  177. }
  178. }
  179. }
  180. /// <summary>
  181. /// 停止全局钩子
  182. /// </summary>
  183. /// <exception cref="System.Exception">UnhookWindowsHookEx failed.</exception>
  184. /// <exception cref="Exception">UnhookWindowsHookEx failed.</exception>
  185. public static void Stop()
  186. {
  187. bool retMouse = true;
  188. if (_hMouseHook != 0)
  189. {
  190. retMouse = WindowsHook.StopHook(_hMouseHook);
  191. _hMouseHook = 0;
  192. }
  193. // 假设卸下钩子失败
  194. if (!(retMouse))
  195. throw new Exception("UnhookWindowsHookEx failed.");
  196. }
  197. }
  198. }