Foxtable(狐表)用户栏目专家坐堂 → 请教一下这段如何在全局中使用


  共有3592人关注过本帖树形打印复制链接

主题:请教一下这段如何在全局中使用

帅哥哟,离线,有人找我吗?
tcmhl
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:159 积分:1718 威望:0 精华:0 注册:2014/9/1 0:35:00
请教一下这段如何在全局中使用  发帖心情 Post By:2021/7/29 10:47:00 [显示全部帖子]


  1. Imports System.Reflection, System.Threading, System.ComponentModel, System.Runtime.InteropServices
  2. Public Class hook
  3. #Region "定义结构"
  4. Private Structure MouseHookStruct
  5. Dim PT As Point
  6. Dim Hwnd As Integer
  7. Dim WHitTestCode As Integer
  8. Dim DwExtraInfo As Integer
  9. End Structure
  10. Private Structure MouseLLHookStruct
  11. Dim PT As Point
  12. Dim MouseData As Integer
  13. Dim Flags As Integer
  14. Dim Time As Integer
  15. Dim DwExtraInfo As Integer
  16. End Structure
  17. Private Structure KeyboardHookStruct
  18. Dim vkCode As Integer
  19. Dim ScanCode As Integer
  20. Dim Flags As Integer
  21. Dim Time As Integer
  22. Dim DwExtraInfo As Integer
  23. End Structure
  24. #End Region
  25. #Region "API声明导入"
  26. Private Declare Function SetWindowsHookExA Lib "user32" (ByVal idHook As Integer, ByVal lpfn As HookProc, ByVal hMod As IntPtr, ByVal dwThreadId As Integer) As Integer
  27. Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal idHook As Integer) As Integer
  28. Private Declare Function CallNextHookEx Lib "user32" (ByVal idHook As Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
  29. Private Declare Function ToAscii Lib "user32" (ByVal uVirtKey As Integer, ByVal uScancode As Integer, ByVal lpdKeyState As Byte(), ByVal lpwTransKey As Byte(), ByVal fuState As Integer) As Integer
  30. Private Declare Function GetKeyboardState Lib "user32" (ByVal pbKeyState As Byte()) As Integer
  31. Private Declare Function GetKeyState Lib "user32" (ByVal vKey As Integer) As Short
  32. Private Delegate Function HookProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
  33. #End Region
  34. #Region "常量声明"
  35. Private Const WH_MOUSE_LL = 14
  36. Private Const WH_KEYBOARD_LL = 13
  37. Private Const WH_MOUSE = 7
  38. Private Const WH_KEYBOARD = 2
  39. Private Const WM_MOUSEMOVE = &H200
  40. Private Const WM_LBUTTONDOWN = &H201
  41. Private Const WM_RBUTTONDOWN = &H204
  42. Private Const WM_MBUTTONDOWN = &H207
  43. Private Const WM_LBUTTONUP = &H202
  44. Private Const WM_RBUTTONUP = &H205
  45. Private Const WM_MBUTTONUP = &H208
  46. Private Const WM_LBUTTONDBLCLK = &H203
  47. Private Const WM_RBUTTONDBLCLK = &H206
  48. Private Const WM_MBUTTONDBLCLK = &H209
  49. Private Const WM_MOUSEWHEEL = &H20A
  50. Private Const WM_KEYDOWN = &H100
  51. Private Const WM_KEYUP = &H101
  52. Private Const WM_SYSKEYDOWN = &H104
  53. Private Const WM_SYSKEYUP = &H105
  54. Private Const VK_SHIFT As Byte = &H10
  55. Private Const VK_CAPITAL As Byte = &H14
  56. Private Const VK_NUMLOCK As Byte = &H90
  57. #End Region
  58. ''' <summary>鼠标激活事件</summary>
  59. Public Event MouseActivity As MouseEventHandler
  60. ''' <summary>键盘按下事件</summary>
  61. Public Event KeyDown As KeyEventHandler
  62. ''' <summary>键盘输入事件</summary>
  63. Public Event KeyPress As KeyPressEventHandler
  64. ''' <summary>键盘松开事件</summary>
  65. Public Event KeyUp As KeyEventHandler
  66. Private hMouseHook As Integer
  67. Private hKeyboardHook As Integer
  68. Private Shared MouseHookProcedure As HookProc
  69. Private Shared KeyboardHookProcedure As HookProc
  70. ''' <summary>创建一个全局鼠标键盘钩子 (请使用Start方法开始监视)</summary>
  71. Sub New()
  72. '留空即可
  73. End Sub
  74. ''' <summary>创建一个全局鼠标键盘钩子,决定是否安装钩子</summary>
  75. ''' <param name="InstallAll">是否立刻挂钩系统消息</param>
  76. Sub New(ByVal InstallAll As Boolean)
  77. If InstallAll Then StartHook(True, True)
  78. End Sub
  79. ''' <summary>创建一个全局鼠标键盘钩子,并决定安装钩子的类型</summary>
  80. ''' <param name="InstallKeyboard">挂钩键盘消息</param>
  81. ''' <param name="InstallMouse">挂钩鼠标消息</param>
  82. Sub New(ByVal InstallKeyboard As Boolean, ByVal InstallMouse As Boolean)
  83. StartHook(InstallKeyboard, InstallMouse)
  84. End Sub
  85. ''' <summary>析构函数</summary>
  86. Protected Overrides Sub Finalize()
  87. UnHook() '卸载对象时反注册系统钩子
  88. MyBase.Finalize()
  89. End Sub
  90. ''' <summary>开始安装系统钩子</summary>
  91. ''' <param name="InstallKeyboardHook">挂钩键盘消息</param>
  92. ''' <param name="InstallMouseHook">挂钩鼠标消息</param>
  93. Public Sub StartHook(Optional ByVal InstallKeyboardHook As Boolean = True, Optional ByVal InstallMouseHook As Boolean = False)
  94. '注册键盘钩子
  95. If InstallKeyboardHook AndAlso hKeyboardHook = 0 Then
  96. KeyboardHookProcedure = New HookProc(AddressOf KeyboardHookProc)
  97. hKeyboardHook = SetWindowsHookExA(WH_KEYBOARD_LL, KeyboardHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly.GetModules()(0)), 0)
  98. If hKeyboardHook = 0 Then '检测是否注册完成
  99. UnHook(True, False) '在这里反注册
  100. Throw New Win32Exception(Marshal.GetLastWin32Error) '报告错误
  101. End If
  102. End If
  103. '注册鼠标钩子
  104. If InstallMouseHook AndAlso hMouseHook = 0 Then
  105. MouseHookProcedure = New HookProc(AddressOf MouseHookProc)
  106. hMouseHook = SetWindowsHookExA(WH_MOUSE_LL, MouseHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly.GetModules()(0)), 0)
  107. If hMouseHook = 0 Then
  108. UnHook(False, True)
  109. Throw New Win32Exception(Marshal.GetLastWin32Error)
  110. End If
  111. End If
  112. End Sub
  113. ''' <summary>立刻卸载系统钩子</summary>
  114. ''' <param name="UninstallKeyboardHook">卸载键盘钩子</param>
  115. ''' <param name="UninstallMouseHook">卸载鼠标钩子</param>
  116. ''' <param name="ThrowExceptions">是否报告错误</param>
  117. Public Sub UnHook(Optional ByVal UninstallKeyboardHook As Boolean = True, Optional ByVal UninstallMouseHook As Boolean = True, Optional ByVal ThrowExceptions As Boolean = False)
  118. '卸载键盘钩子
  119. If hKeyboardHook <> 0 AndAlso UninstallKeyboardHook Then
  120. Dim retKeyboard As Integer = UnhookWindowsHookEx(hKeyboardHook)
  121. hKeyboardHook = 0
  122. If ThrowExceptions AndAlso retKeyboard = 0 Then '如果出现错误,是否报告错误
  123. Throw New Win32Exception(Marshal.GetLastWin32Error) '报告错误
  124. End If
  125. End If
  126. '卸载鼠标钩子
  127. If hMouseHook <> 0 AndAlso UninstallMouseHook Then
  128. Dim retMouse As Integer = UnhookWindowsHookEx(hMouseHook)
  129. hMouseHook = 0
  130. If ThrowExceptions AndAlso retMouse = 0 Then
  131. Throw New Win32Exception(Marshal.GetLastWin32Error)
  132. End If
  133. End If
  134. End Sub
  135. '鼠标消息的委托处理代码
  136. Private Function MouseHookProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
  137. If nCode >= 0 Then
  138. Dim mouseHookStruct As MouseLLHookStruct = DirectCast(Marshal.PtrToStructure(lParam, GetType(MouseLLHookStruct)), MouseLLHookStruct)
  139. Dim moubut As MouseButtons = MouseButtons.None '鼠标按键
  140. Dim mouseDelta As Integer = 0 '滚轮值
  141. Select Case wParam
  142. Case WM_LBUTTONDOWN
  143. moubut = MouseButtons.Left
  144. Case WM_RBUTTONDOWN
  145. moubut = MouseButtons.Right
  146. Case WM_MBUTTONDOWN
  147. moubut = MouseButtons.Middle
  148. Case WM_MOUSEWHEEL
  149. Dim int As Integer = (mouseHookStruct.MouseData >> 16) And &HFFFF
  150. '本段代码CLE添加,模仿C#的Short从Int弃位转换
  151. If int > Short.MaxValue Then mouseDelta = int - 65536 Else mouseDelta = int
  152. End Select
  153. Dim clickCount As Integer = 0 '单击次数
  154. If moubut <> MouseButtons.None Then
  155. If wParam = WM_LBUTTONDBLCLK OrElse wParam = WM_RBUTTONDBLCLK OrElse wParam = WM_MBUTTONDBLCLK Then
  156. clickCount = 2
  157. Else
  158. clickCount = 1
  159. End If
  160. End If
  161. Dim e As New MouseEventArgs(moubut, clickCount, mouseHookStruct.PT.X, mouseHookStruct.PT.Y, mouseDelta)
  162. RaiseEvent MouseActivity(Me, e)
  163. End If
  164. Return CallNextHookEx(hMouseHook, nCode, wParam, lParam) '激活下一个钩子
  165. End Function
  166. ''' <summary>键盘钩子是否有效</summary>
  167. Public Property KeyHookEnabled() As Boolean
  168. Get
  169. Return hKeyboardHook <> 0
  170. End Get
  171. Set(ByVal value As Boolean)
  172. If value Then StartHook(True, False) Else UnHook(True, False)
  173. End Set
  174. End Property
  175. ''' <summary>鼠标钩子是否有效</summary>
  176. Public Property MouseHookEnabled() As Boolean
  177. Get
  178. Return hMouseHook <> 0
  179. End Get
  180. Set(ByVal value As Boolean)
  181. If value Then StartHook(False, True) Else UnHook(False, True)
  182. End Set
  183. End Property
  184. End Class

 回到顶部