如何以编程方式单击使用VB.net的外部应用程序

时间:2014-04-14 07:28:04

标签: vb.net

enter image description here

我已经创建了一个测试软件来测试我们的产品,但在某些情况下,其他客户提供了API /工具等...来测试那些特定的参数。

我想点击其他应用程序上的按钮(自动点击)。

基本上我们想要它,所以即使有工作指令,操作员也不会点击错误的按钮。

2 个答案:

答案 0 :(得分:1)

“------------------------------ FORM --------------- --------------------------------

Public Class Form1

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        LeftClick()
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If CommandType.Text = "Start" Then
            Timer1.Enabled = True
            CommandType.Text = "Stop"
        Else
            Timer1.Enabled = False
            CommandType.Text = "Start"
        End If
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        MoveMouse(300, 75)
        LeftClick()
        LeftClick()
End Sub
End Class

“------------------------------ MODULE --------------- -------------------------------

Module Module1

    Public 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)

    Public Declare Function SetCursorPos Lib "user32" (ByVal x As Integer, ByVal y As Integer) As Integer
    Public Declare Function GetCursorPos Lib "user32" (ByVal lpPoint As POINTAPI) As Integer
    Public Const MOUSEEVENTF_LEFTDOWN = &H2
    Public Const MOUSEEVENTF_LEFTUP = &H4
    Public Const MOUSEEVENTF_MIDDLEDOWN = &H20
    Public Const MOUSEEVENTF_MIDDLEUP = &H40
    Public Const MOUSEEVENTF_RIGHTDOWN = &H8
    Public Const MOUSEEVENTF_RIGHTUP = &H10
    Public Const MOUSEEVENTF_MOVE = &H1
    Public Structure POINTAPI
        Dim x As Integer
        Dim y As Integer
    End Structure
    Public Function GetX() As Integer
        : Dim n As POINTAPI
        GetCursorPos(n)
        GetX = n.x
    End Function
    Public Function GetY() As Integer
        Dim n As POINTAPI
        GetCursorPos(n)
        GetY = n.y

    End Function
    Public Sub LeftClick()
        LeftDown()
        LeftUp()
    End Sub
    Public Sub LeftDown()
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
    End Sub
    Public Sub LeftUp()
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
    End Sub
    Public Sub MiddleClick()
        MiddleDown()
        MiddleUp()
    End Sub
    Public Sub MiddleDown()
        mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0)
    End Sub
    Public Sub MiddleUp()
        mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0)
    End Sub
    Public Sub RightClick()
        RightDown()
        RightUp()
    End Sub
    Public Sub RightDown()
        mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
    End Sub
    Public Sub RightUp()
        mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
    End Sub
    Public Sub MoveMouse(ByVal xMove As Integer, ByVal yMove As Integer)
        mouse_event(MOUSEEVENTF_MOVE, xMove, yMove, 0, 0)
    End Sub
    Public Sub SetMousePos(ByVal xPos As Integer, ByVal yPos As Integer)
        SetCursorPos(xPos, yPos)
    End Sub
End Module

这将允许您左键单击,右键单击并在X轴和Y轴上移动光标

答案 1 :(得分:1)

试试这个:

Private Const BM_CLICK = &HF5
Private Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As IntPtr
Private Declare Auto Function FindWindowEx Lib "user32.dll" (ByVal hwndParent As IntPtr, ByVal hwndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr   

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Dim hWndMsgBox, hWndButton As Long

    hWndMsgBox = FindWindow("#32770", "About")
    If hWndMsgBox Then hWndButton = FindWindowEx(hWndMsgBox, 0&, "Button", "OK")
    If hWndButton Then SendMessage(hWndButton, BM_CLICK, 0&, 0&) 
End Sub