在 VB.net 中使用鼠标滚动屏幕上的任意位置
Use mouse scroll anywhere on screen in VB.net
我正在尝试使用鼠标按钮和滚动组合设置全局快捷方式,(向上(或向下)滚动同时按下鼠标右键)。
我在想我应该尝试对鼠标滚动做出反应,但我不知道如何检测表单外的鼠标滚轮。
现在我让它对带有计时器的鼠标点击做出反应,并将表单设置为始终在最前面(没有问题,因为它总是在最前面),但不知道如何处理它。
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If MouseButtons.HasFlag(MouseButtons.Right) = True Then
TextBox1.Text = "YES"
Else
TextBox1.Text = "NO"
End If
End Sub
目标是尽量减少用户使用该快捷方式设置的应用程序,以防信息有帮助。我有其余的代码只是无法弄清楚那部分。
编辑:
它在表单内部工作:
Private Sub ListBoxCHOWAJ_MouseWheel(sender As Object, e As MouseEventArgs) Handles ListBoxCHOWAJ.MouseWheel
If MouseButtons.HasFlag(MouseButtons.Right) = True Then
If e.Delta > 0 Then
TextBox1.Text = "up"
Else
TextBox1.Text = "down"
End If
End If
End Sub
您需要一个鼠标钩子来检测应用程序外的滚动。看看我的 InputHelper library and its low-level mouse hook.
要将其包含在您的项目中,请下载 compiled DLL 并将其添加为参考:
在 Solution Explorer
中右键单击您的项目,然后按 Add Reference...
转到 Browse
选项卡。
找到 InputHelper DLL 文件,select 它并按 OK
。
您可以在挂钩的 MouseWheel
事件处理程序中执行所有逻辑,不再需要计时器:
Imports InputHelperLib
Public Class Form1
Dim WithEvents MouseHook As New InputHelper.Hooks.MouseHook
Private Sub MouseHook_MouseWheel(sender As Object, e As InputHelperLib.InputHelper.Hooks.MouseHookEventArgs) Handles MouseHook.MouseWheel
If MouseButtons.HasFlag(MouseButtons.Right) = True Then
'Do something...
End If
End Sub
End Class
不需要像您那样检查 e.Delta
,因为它永远不会是 0。
我正在尝试使用鼠标按钮和滚动组合设置全局快捷方式,(向上(或向下)滚动同时按下鼠标右键)。
我在想我应该尝试对鼠标滚动做出反应,但我不知道如何检测表单外的鼠标滚轮。
现在我让它对带有计时器的鼠标点击做出反应,并将表单设置为始终在最前面(没有问题,因为它总是在最前面),但不知道如何处理它。
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If MouseButtons.HasFlag(MouseButtons.Right) = True Then
TextBox1.Text = "YES"
Else
TextBox1.Text = "NO"
End If
End Sub
目标是尽量减少用户使用该快捷方式设置的应用程序,以防信息有帮助。我有其余的代码只是无法弄清楚那部分。
编辑: 它在表单内部工作:
Private Sub ListBoxCHOWAJ_MouseWheel(sender As Object, e As MouseEventArgs) Handles ListBoxCHOWAJ.MouseWheel
If MouseButtons.HasFlag(MouseButtons.Right) = True Then
If e.Delta > 0 Then
TextBox1.Text = "up"
Else
TextBox1.Text = "down"
End If
End If
End Sub
您需要一个鼠标钩子来检测应用程序外的滚动。看看我的 InputHelper library and its low-level mouse hook.
要将其包含在您的项目中,请下载 compiled DLL 并将其添加为参考:
在
Solution Explorer
中右键单击您的项目,然后按Add Reference...
转到
Browse
选项卡。找到 InputHelper DLL 文件,select 它并按
OK
。
您可以在挂钩的 MouseWheel
事件处理程序中执行所有逻辑,不再需要计时器:
Imports InputHelperLib
Public Class Form1
Dim WithEvents MouseHook As New InputHelper.Hooks.MouseHook
Private Sub MouseHook_MouseWheel(sender As Object, e As InputHelperLib.InputHelper.Hooks.MouseHookEventArgs) Handles MouseHook.MouseWheel
If MouseButtons.HasFlag(MouseButtons.Right) = True Then
'Do something...
End If
End Sub
End Class
不需要像您那样检查 e.Delta
,因为它永远不会是 0。