vb 松开鼠标事件

'模块代码

'BY:QQ:1633602558

Option Explicit

Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long

Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long

Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long

Private Const HC_ACTION = 0

Private Const WH_MOUSE = 7 '本地钩子

Private Const WH_MOUSE_LL = 14 '全局钩子

'鼠标消息

Private Const WM_MOUSEMOVE = &H200 '移动鼠标

Private Const WM_LBUTTONDOWN = &H201 '按下鼠标左键

Private Const WM_LBUTTONUP = &H202 '释放鼠标左键

Private Const WM_LBUTTONDBLCLK = &H203 '双击鼠标左键

Private Const WM_RBUTTONDOWN = &H204 '按下鼠标右键

Private Const WM_RBUTTONUP = &H205 '释放鼠标右键

Private Const WM_RBUTTONDBLCLK = &H206 '双击鼠标右键

Private Const WM_MBUTTONDOWN = &H207 '按下鼠标中键

Private Const WM_MBUTTONUP = &H208 '释放鼠标中键

Private Const WM_MBUTTONDBLCLK = &H209 '双击鼠标中键

Private Const WM_MOUSEACTIVATE = &H21 '当光标在某个非激活的窗口中而用户正按着鼠标的某个键发送此消息给当前窗口

Private Const WM_MOUSEFIRST = &H200 '鼠标移动时发生(与WM_MOUSEMOVE等值),常用于判断鼠标消息的范围

Private Const WM_MOUSELAST = &H209 'WM_MBUTTONDBLCLK的别名,通常用于判断鼠标消息的范围

Private Const WM_MOUSEWHEEL = &H20A '当鼠标轮子转动时发送此消息个当前有焦点的控件

Private hHook As Long

Public Function HookProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

If nCode = HC_ACTION Then

Select Case wParam

Case WM_MOUSEMOVE '移动鼠标

Case WM_LBUTTONDOWN, WM_LBUTTONUP, WM_LBUTTONDBLCLK '按下鼠标左键/释放鼠标左键/双击鼠标左键

Case WM_RBUTTONDOWN, WM_RBUTTONUP, WM_RBUTTONDBLCLK '按下鼠标右键/释放鼠标右键 /双击鼠标右键

HookProc = 1: Exit Function '示例:吃掉鼠标右键消息

Case WM_MBUTTONDOWN, WM_MBUTTONUP, WM_MBUTTONDBLCLK '按下鼠标中键'释放鼠标中键'双击鼠标中键

Case WM_MOUSEACTIVATE '当光标在某个非激活的窗口中而用户正按着鼠标的某个键发送此消息给当前窗口

Case WM_MOUSEFIRST '鼠标移动时发生(与WM_MOUSEMOVE等值),常用于判断鼠标消息的范围

Case WM_MOUSELAST 'WM_MBUTTONDBLCLK的别名,通常用于判断鼠标消息的范围

Case WM_MOUSEWHEEL '当鼠标轮子转动时发送此消息个当前有焦点的控件

Case Else:

End Select

End If

HookProc = CallNextHookEx(hHook, nCode, wParam, lParam)

End Function

Public Sub SetMouseHook(Optional ByVal sTest As Boolean) '设置鼠标按键钩子

If hHook <> 0 Then Call UnhookWindowsHookEx(hHook)

If sTest = True Then

hHook = SetWindowsHookEx(WH_MOUSE_LL, AddressOf HookProc, App.hInstance, 0) '全局勾子

Else

hHook = SetWindowsHookEx(WH_MOUSE, AddressOf HookProc, 0, App.ThreadID) '本地勾子

End If

End Sub

Public Sub DelMouseHook() '卸载鼠标按键钩子

If hHook <> 0 Then

Call UnhookWindowsHookEx(hHook)

hHook = 0

End If

End Sub

'窗体代码

'Private Sub Form_Load()

' Call SetMouseHook(True) '设置鼠标钩子全局=True,线程=False

'End Sub

'Private Sub Form_Unload(Cancel As Integer)

' Call DelMouseHook '卸载鼠标按键钩子

'End Sub

'祥细代码发你了,如果还有不懂,可加我QQ问,最近写过类似程序,正好用到这个