Visual Basic-如何制作一个自动点击按钮的机器人?

时间:2013-01-12 20:26:46

标签: visual-studio-2010 vb.net-2010 bots

我需要它来刷新页面,直到它看到一个新按钮,然后自动点击它

1 个答案:

答案 0 :(得分:0)

如果图像是静态的,您可以创建图像比较工具。

首先,您需要this页面中的代码:

Private Sub btnGo_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnGo.Click
    Me.Cursor = Cursors.WaitCursor
    Application.DoEvents()

    ' Load the images.
    Dim bm1 As Bitmap = Image.FromFile(txtFile1.Text)
    Dim bm2 As Bitmap = Image.FromFile(txtFile2.Text)

    ' Make a difference image.
    Dim wid As Integer = Math.Min(bm1.Width, bm2.Width)
    Dim hgt As Integer = Math.Min(bm1.Height, bm2.Height)
    Dim bm3 As New Bitmap(wid, hgt)

    ' Create the difference image.
    Dim are_identical As Boolean = True
    Dim r1, g1, b1, r2, g2, b2, r3, g3, b3 As Integer
    Dim eq_color As Color = Color.White
    Dim ne_color As Color = Color.Red
    For x As Integer = 0 To wid - 1
        For y As Integer = 0 To hgt - 1
            If bm1.GetPixel(x, y).Equals(bm2.GetPixel(x, _
                y)) Then
                bm3.SetPixel(x, y, eq_color)
            Else
                bm3.SetPixel(x, y, ne_color)
                are_identical = False
            End If
        Next y
    Next x

    ' Display the result.
    picResult.Image = bm3

    Me.Cursor = Cursors.Default
    If (bm1.Width <> bm2.Width) OrElse (bm1.Height <> _
        bm2.Height) Then are_identical = False
    If are_identical Then
        MessageBox.Show("The images are identical")
    Else
        MessageBox.Show("The images are different")
    End If

    bm1.Dispose()
    bm2.Dispose()
End Sub

此代码允许您比较图像,因此您可以制作按钮的屏幕截图并将其存储为您要比较的主图像,然后您需要制作计算机屏幕的屏幕图并提取像素按钮应该在哪里。

Here是有关如何从计算机屏幕截取屏幕截图的教程。

在此之后,您可以使用系统鼠标功能使用this页面中的代码模拟点击:

Public Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Public Const MOUSEEVENTF_RIGHTDOWN As Long = &H8
Public Const MOUSEEVENTF_RIGHTUP As Long = &H10

Private Sub SingleClick()
  SetCursorPos 100, 100 'x and y position
  mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
  mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
End Sub

Private Sub DoubleClick()
  'Simulate a double click as a quick series of two clicks
  SetCursorPos 100, 100 'x and y position
  mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
  mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
  mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
  mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
End Sub

Private Sub RightClick()
  'Simulate a right click
  SetCursorPos 200, 200 'x and y position
  mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
  mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
  mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
  mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
End Sub