加好友 发短信
等级:幼狐
帖子:159
积分:1718
威望:0
精华:0
注册:2014/9/1 0:35:00
|
请教一下这段如何在全局中使用 Post By:2021/7/29 10:47:00 [只看该作者]
Imports System.Reflection, System.Threading, System.ComponentModel, System.Runtime.InteropServices Private Structure MouseHookStruct Dim WHitTestCode As Integer Dim DwExtraInfo As Integer Private Structure MouseLLHookStruct Dim DwExtraInfo As Integer Private Structure KeyboardHookStruct Dim DwExtraInfo As Integer Private Declare Function SetWindowsHookExA Lib "user32" (ByVal idHook As Integer, ByVal lpfn As HookProc, ByVal hMod As IntPtr, ByVal dwThreadId As Integer) As Integer Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal idHook As Integer) As Integer Private Declare Function CallNextHookEx Lib "user32" (ByVal idHook As Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer 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 Private Declare Function GetKeyboardState Lib "user32" (ByVal pbKeyState As Byte()) As Integer Private Declare Function GetKeyState Lib "user32" (ByVal vKey As Integer) As Short Private Delegate Function HookProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer Private Const WH_MOUSE_LL = 14 Private Const WH_KEYBOARD_LL = 13 Private Const WH_MOUSE = 7 Private Const WH_KEYBOARD = 2 Private Const WM_MOUSEMOVE = &H200 Private Const WM_LBUTTONDOWN = &H201 Private Const WM_RBUTTONDOWN = &H204 Private Const WM_MBUTTONDOWN = &H207 Private Const WM_LBUTTONUP = &H202 Private Const WM_RBUTTONUP = &H205 Private Const WM_MBUTTONUP = &H208 Private Const WM_LBUTTONDBLCLK = &H203 Private Const WM_RBUTTONDBLCLK = &H206 Private Const WM_MBUTTONDBLCLK = &H209 Private Const WM_MOUSEWHEEL = &H20A Private Const WM_KEYDOWN = &H100 Private Const WM_KEYUP = &H101 Private Const WM_SYSKEYDOWN = &H104 Private Const WM_SYSKEYUP = &H105 Private Const VK_SHIFT As Byte = &H10 Private Const VK_CAPITAL As Byte = &H14 Private Const VK_NUMLOCK As Byte = &H90 Public Event MouseActivity As MouseEventHandler Public Event KeyDown As KeyEventHandler Public Event KeyPress As KeyPressEventHandler Public Event KeyUp As KeyEventHandler Private hMouseHook As Integer Private hKeyboardHook As Integer Private Shared MouseHookProcedure As HookProc Private Shared KeyboardHookProcedure As HookProc Sub New(ByVal InstallAll As Boolean) If InstallAll Then StartHook(True, True) Sub New(ByVal InstallKeyboard As Boolean, ByVal InstallMouse As Boolean) StartHook(InstallKeyboard, InstallMouse) Protected Overrides Sub Finalize() Public Sub StartHook(Optional ByVal InstallKeyboardHook As Boolean = True, Optional ByVal InstallMouseHook As Boolean = False) If InstallKeyboardHook AndAlso hKeyboardHook = 0 Then KeyboardHookProcedure = New HookProc(AddressOf KeyboardHookProc) hKeyboardHook = SetWindowsHookExA(WH_KEYBOARD_LL, KeyboardHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly.GetModules()(0)), 0) If hKeyboardHook = 0 Then Throw New Win32Exception(Marshal.GetLastWin32Error) If InstallMouseHook AndAlso hMouseHook = 0 Then MouseHookProcedure = New HookProc(AddressOf MouseHookProc) hMouseHook = SetWindowsHookExA(WH_MOUSE_LL, MouseHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly.GetModules()(0)), 0) Throw New Win32Exception(Marshal.GetLastWin32Error) Public Sub UnHook(Optional ByVal UninstallKeyboardHook As Boolean = True, Optional ByVal UninstallMouseHook As Boolean = True, Optional ByVal ThrowExceptions As Boolean = False) If hKeyboardHook <> 0 AndAlso UninstallKeyboardHook Then Dim retKeyboard As Integer = UnhookWindowsHookEx(hKeyboardHook) If ThrowExceptions AndAlso retKeyboard = 0 Then Throw New Win32Exception(Marshal.GetLastWin32Error) If hMouseHook <> 0 AndAlso UninstallMouseHook Then Dim retMouse As Integer = UnhookWindowsHookEx(hMouseHook) If ThrowExceptions AndAlso retMouse = 0 Then Throw New Win32Exception(Marshal.GetLastWin32Error) Private Function MouseHookProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer Dim mouseHookStruct As MouseLLHookStruct = DirectCast(Marshal.PtrToStructure(lParam, GetType(MouseLLHookStruct)), MouseLLHookStruct) Dim moubut As MouseButtons = MouseButtons.None Dim mouseDelta As Integer = 0 moubut = MouseButtons.Left moubut = MouseButtons.Right moubut = MouseButtons.Middle Dim int As Integer = (mouseHookStruct.MouseData >> 16) And &HFFFF If int > Short.MaxValue Then mouseDelta = int - 65536 Else mouseDelta = int Dim clickCount As Integer = 0 If moubut <> MouseButtons.None Then If wParam = WM_LBUTTONDBLCLK OrElse wParam = WM_RBUTTONDBLCLK OrElse wParam = WM_MBUTTONDBLCLK Then Dim e As New MouseEventArgs(moubut, clickCount, mouseHookStruct.PT.X, mouseHookStruct.PT.Y, mouseDelta) RaiseEvent MouseActivity(Me, e) Return CallNextHookEx(hMouseHook, nCode, wParam, lParam) Public Property KeyHookEnabled() As Boolean Return hKeyboardHook <> 0 Set(ByVal value As Boolean) If value Then StartHook(True, False) Else UnHook(True, False) Public Property MouseHookEnabled() As Boolean Set(ByVal value As Boolean) If value Then StartHook(False, True) Else UnHook(False, True)
|
|