当前位置 : 主页 > 编程语言 > c语言 >

vb.net – 如何模拟鼠标点击?

来源:互联网 收集:自由互联 发布时间:2021-06-24
我正在努力制作一个用键盘点击的程序,如 Osu!所示. 我已经尝试过SendKeys()RaiseMouseEvent()和OnMouseClick().现在我正在尝试这个并且无法获得任何工作…… 我一直得到的错误是 PInvoke restricti
我正在努力制作一个用键盘点击的程序,如 Osu!所示.
我已经尝试过SendKeys()RaiseMouseEvent()和OnMouseClick().现在我正在尝试这个并且无法获得任何工作……
我一直得到的错误是 PInvoke restriction: cannot return variants.

Public Class Form1
    Dim onn As Boolean = False
    Declare Function mc Lib "user32.dll" Alias "mouse_event" (flag, x, y, button, extra)
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not onn Then
            Button1.Text = "Off"
            Label1.Text = "Status: On"
            onn = True
        ElseIf onn Then
            Button1.Text = "On"
            Label1.Text = "Status: Off"
            onn = False
        End If
    End Sub
    Private Sub Form1_KeyPress1(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If onn And (e.KeyChar = "Z" Or e.KeyChar = "X" Or e.KeyChar = "z" Or e.KeyChar = "x") Then
            mc(&H2, 0, 0, 0, 0)
            mc(&H4, 0, 0, 0, 0)
        End If
    End Sub
End Class
当功能处于“onn”状态时,此示例单击鼠标当前所在的位置:

Public Class Form1

    Private onn As Boolean = False

    Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Integer, _
      ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, _
      ByVal dwExtraInfo As Integer)

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.KeyPreview = True
        Button1.Text = "Off"
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        onn = Not onn
        Button1.Text = IIf(onn, "On", "Off")
    End Sub

    Private Sub Form1_KeyPress1(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If onn Then
            Select Case e.KeyChar
                Case "Z", "z", "X", "x"
                    mouse_event(&H2, 0, 0, 0, 0)
                    mouse_event(&H4, 0, 0, 0, 0)

            End Select
        End If
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Debug.Print("Button2")
    End Sub

    Private Sub Button3_Click(sender As Object, e As System.EventArgs) Handles Button3.Click
        Debug.Print("Button3")
    End Sub

End Class
网友评论