VB(模拟)鼠标单击(表单外)

时间:2015-03-22 19:25:26

标签: vb.net mouseevent simulate

我试图制作一个模拟鼠标点击的程序,并对Visual Basic中的鼠标点击事件作出反应,但我找不到我需要的东西。我已经编写了代码,我不懂逻辑并标记它请告诉我代码命令我需要什么。每个地方都是#我不知道代码。抱歉,我的才华横溢; D老师很糟糕:P

Public Class Form1
Public Tick As Integer = 0
Public MaxTick As Integer = 0
Public active As Boolean = False

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    RadioButton1.Select()
    ComboBox1.SelectedIndex = 0
    ComboBox2.SelectedIndex = 0
End Sub

Private Sub RadioButton1_Change(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.Click, RadioButton2.Click
    If RadioButton1.Checked Then
        GroupBox1.Enabled = True
        GroupBox2.Enabled = False
        Timer1.Interval = TextBox1.Text
    Else
        GroupBox2.Enabled = True
        GroupBox1.Enabled = False
        Timer2.Interval = TextBox3.Text
    End If
End Sub

Private Sub Me_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
    If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Tab) Then
        If RadioButton1.Checked Then
            active = False
            If Timer1.Enabled Then
                Timer1.Stop()
            Else
                Timer1.Start()
            End If
            e.Handled = False
        Else
            active = True
        End If
    End If
End Sub


Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    If CheckBoxLeft.Checked Then
        ###Perform Mouseclick Left###
    End If
    If CheckBoxMiddle.Checked Then
        ###Perform Mouseclick Middle###
    End If
    If CheckBoxRight.Checked Then
        ###Perform Mouseclick Right###
    End If
End Sub

###Private Sub OnClick_Event()###
    ###If Mouseclick == Right Then###
        If CheckBoxRight.Checked Then
            Timer2.start()
        End If
    ###Else if Mouseclick == Middle Then###
        If CheckBoxMiddle.Checked Then
            Timer2.Start()
        End If
    ###Else If Mouseclick == Left Then###
        If CheckBoxLeft.Checked Then
            Timer2.Start()
        End If
    End If
End Sub

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
    If active Then
        Tick += 1
        If CheckBoxLeft.Checked Then
            ###Perform Click Left###
        End If
        If CheckBoxMiddle.Checked Then
            ###Perform Click Middle###
        End If
        If CheckBoxRight.Checked Then
            ###Perform Click Right###
        End If
        If Tick = MaxTick Then
            Tick = 0
            Timer2.Stop()
        End If
    Else
        Timer2.Stop()
        Tick = 0
    End If
End Sub

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
    MaxTick = TextBox2.Text
End Sub
End Class

所以我需要一个Mouseclick事件,然后询问单击哪个鼠标按钮,我需要一个方法来执行左中键和右键单击

LG Niklas

1 个答案:

答案 0 :(得分:0)

您应该查找mouse_event()p / invoke方法。这将允许您模拟鼠标事件。它在VB.Net中的声明是

<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

您可以阅读更多相关信息here。这里有一个project on codeproject.com,它有一个完整的例子。