vb.net 中的鼠标事件
Mouse Events in vb.net
我正在尝试在激活第 3 方应用程序(不受我控制)(最上面的程序)时移动鼠标。理想情况下,我想相对于当前位置移动鼠标,但使用绝对位置并不难转换。这是我目前使用的;
<DllImport("user32.dll")> _
Private Shared Sub mouse_event(dwFlags As UInteger, dx As UInteger, dy As UInteger, dwData As UInteger, dwExtraInfo As Integer)
End Sub
Public Sub MoveMouse(ByVal xPos As Long, ByVal yPos As Long)
mouse_event(MOUSEEVENTF_MOVE, xPos, yPos, 0, 0)
End Sub
Public Const MOUSEEVENTF_MOVE = &H1
调用方式为;
MoveMouse(100, 100)
现在我的问题是我的鼠标在 y 方向移动但不在 x 方向移动。我尝试了一些故障排除,结果调用中的第一个值 (xPos) 实际上是 y 中移动的距离,而不是 x。 yPos 什么都不做。
例如下面的代码将鼠标向下移动 50 像素;
MoveMouse(50, 0)
我做错了什么?
你这边肯定有问题,因为下面的代码确实有效:
Imports System.Runtime.InteropServices
Public Class Form1
Public Const MOUSEEVENTF_MOVE = &H1
<DllImport("user32.dll", EntryPoint:="mouse_event")>
Private Shared Sub mouse_event(dwFlags As UInteger, dx As Integer, dy As Integer, dwData As UInteger,
dwExtraInfo As Integer)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim dx = -10
Dim dy = -10
mouse_event(MOUSEEVENTF_MOVE, dx, dy, 0, 0)
End Sub
End Class
请注意,我稍微修改了 mouse_event
的签名以使用 Integer
,因为如果不指定 /removeintchecks
option 就无法轻松获得 C# unchecked
等价物。
看看是否有程序干扰您的程序,关闭它们并重试,如果没有,恐怕我无法提出其他修复建议。
我正在尝试在激活第 3 方应用程序(不受我控制)(最上面的程序)时移动鼠标。理想情况下,我想相对于当前位置移动鼠标,但使用绝对位置并不难转换。这是我目前使用的;
<DllImport("user32.dll")> _
Private Shared Sub mouse_event(dwFlags As UInteger, dx As UInteger, dy As UInteger, dwData As UInteger, dwExtraInfo As Integer)
End Sub
Public Sub MoveMouse(ByVal xPos As Long, ByVal yPos As Long)
mouse_event(MOUSEEVENTF_MOVE, xPos, yPos, 0, 0)
End Sub
Public Const MOUSEEVENTF_MOVE = &H1
调用方式为;
MoveMouse(100, 100)
现在我的问题是我的鼠标在 y 方向移动但不在 x 方向移动。我尝试了一些故障排除,结果调用中的第一个值 (xPos) 实际上是 y 中移动的距离,而不是 x。 yPos 什么都不做。 例如下面的代码将鼠标向下移动 50 像素;
MoveMouse(50, 0)
我做错了什么?
你这边肯定有问题,因为下面的代码确实有效:
Imports System.Runtime.InteropServices
Public Class Form1
Public Const MOUSEEVENTF_MOVE = &H1
<DllImport("user32.dll", EntryPoint:="mouse_event")>
Private Shared Sub mouse_event(dwFlags As UInteger, dx As Integer, dy As Integer, dwData As UInteger,
dwExtraInfo As Integer)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim dx = -10
Dim dy = -10
mouse_event(MOUSEEVENTF_MOVE, dx, dy, 0, 0)
End Sub
End Class
请注意,我稍微修改了 mouse_event
的签名以使用 Integer
,因为如果不指定 /removeintchecks
option 就无法轻松获得 C# unchecked
等价物。
看看是否有程序干扰您的程序,关闭它们并重试,如果没有,恐怕我无法提出其他修复建议。