设置环境:
我使用Visual Studio 2010并在vb.net中编写代码(使用.NET Framework 4)。
我的目标:
Shell("C:\myprogram.exe", AppWinStyle.NormalFocus)
打开新应用程序如果活动窗口是vb.net表单,那么识别双击事件很简单:
Private Sub Button2_DoubleClick(......) Handles Button2.DoubleClick
End Sub
此代码用于识别单击左键:
这是 Form1.vb
Public Class Form1
Private WithEvents MouseDetector As MouseDetector
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
MouseDetector = New MouseDetector
End Sub
Private Sub MouseDetector_MouseLeftButtonClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MouseDetector.MouseLeftButtonClick
'Left click occurred
MsgBox("Left click")
End Sub
End Class
这里是 MouseDetector.vb中的枚举
Private Enum MouseMessages
WM_LeftButtonDown = 513
WM_LeftButtonUp = 514
WM_LeftDblClick = 515
WM_RightButtonDown = 516
WM_RightButtonUp = 517
WM_RightDblClick = 518
End Enum
此处的 MouseDetector.vb 中的功能可识别单击
Private Function MouseHookProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
If nCode < 0 Then
Return CallNextHookEx(MouseHookID, nCode, wParam, lParam)
End If
Dim MouseData As MouseHookStruct = Marshal.PtrToStructure(lParam, GetType(MouseHookStruct))
Select Case wParam
Case MouseMessages.WM_LeftButtonUp
RaiseEvent MouseLeftButtonClick(Nothing, New MouseEventArgs(MouseButtons.Left, 1, MouseData.pt.x, MouseData.pt.y, 0))
End Select
Return CallNextHookEx(MouseHookID, nCode, wParam, lParam)
End Function
如何识别双击?
显然没有MouseButtons.LeftDoubleclick
,但这就是我所追求的目标。
有人愿意就如何做到这一点提出建议吗?我真的很感激。
答案 0 :(得分:2)
如果你看一下CodePlex GlobalMouseKeyHook项目,他们正在为鼠标左键和右键全局处理MouseDouble点击事件。
此库附加到Windows全局钩子,跟踪键盘和鼠标点击和移动,并使用KeyEventArgs和MouseEventArgs引发常见的.NET事件,因此您可以轻松检索所需的任何信息:
鼠标坐标
单击鼠标按钮
鼠标滚轮滚动
按键和释放按钮
特殊关键状态