Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class MouseHook
- Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As MouseProcDelegate, ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer
- Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As MSLLHOOKSTRUCT) As Integer
- Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Integer) As Integer
- Private Delegate Function MouseProcDelegate(ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As MSLLHOOKSTRUCT) As Integer
- Private Structure MSLLHOOKSTRUCT
- Public pt As Point
- Public mouseData As Integer
- Public flags As Integer
- Public time As Integer
- Public dwExtraInfo As Integer
- End Structure
- Public Enum Wheel_Direction
- WheelUp
- WheelDown
- End Enum
- Private Const HC_ACTION As Integer = 0
- Private Const WH_MOUSE_LL As Integer = 14
- Private Const WM_LBUTTONDOWN As Integer = &H201
- Private Const WM_RBUTTONDOWN As Integer = &H204
- Private Const WM_MBUTTONDOWN As Integer = &H207
- Private Const WM_MOUSEWHEEL As Integer = &H20A
- Private Const WM_XBUTTONDOWN As Integer = &H20B
- Private MouseHook As Integer
- Private MouseHookDelegate As MouseProcDelegate
- Public Event Mouse_Left()
- Public Event Mouse_Right()
- Public Event Mouse_Middle()
- Public Event Mouse_Wheel(ByVal Direction As Wheel_Direction)
- Public Event Mouse_XButton1()
- Public Event Mouse_XButton2()
- Public Sub New()
- MouseHookDelegate = New MouseProcDelegate(AddressOf MouseProc)
- MouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookDelegate, System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
- End Sub
- Private Function MouseProc(ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As MSLLHOOKSTRUCT) As Integer
- If (nCode = HC_ACTION) Then
- Select Case wParam
- Case WM_LBUTTONDOWN
- RaiseEvent Mouse_Left()
- Case WM_RBUTTONDOWN
- RaiseEvent Mouse_Right()
- Case WM_MBUTTONDOWN
- RaiseEvent Mouse_Middle()
- Case WM_MOUSEWHEEL
- Dim wDirection As Wheel_Direction
- If lParam.mouseData < 0 Then
- wDirection = Wheel_Direction.WheelDown
- Else
- wDirection = Wheel_Direction.WheelUp
- End If
- RaiseEvent Mouse_Wheel(wDirection)
- Case (WM_MOUSEWHEEL)
- Dim wDirection As Wheel_Direction
- If lParam.mouseData < 0 Then
- wDirection = Wheel_Direction.WheelDown
- Else
- wDirection = Wheel_Direction.WheelUp
- End If
- RaiseEvent Mouse_Wheel(wDirection)
- End Select
- End If
- Return CallNextHookEx(MouseHook, nCode, wParam, lParam)
- End Function
- Protected Overrides Sub Finalize()
- UnhookWindowsHookEx(MouseHook)
- MyBase.Finalize()
- End Sub
- End Class
Add Comment
Please, Sign In to add comment