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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23054337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:21:43  来源:igfitidea点击:

How can i programmatically click a external Application Using VB.net

vb.net

提问by GoroundoVipa

enter image description here

在此处输入图片说明

I have created a test software to test our product, but in some cases other customer provide there API/Tool and etc... to test there specific parameter.

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

I want is to click the button on other the application (Automatically Click).

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

Basically we want it so the operator will not click the wrong button even there have a work instruction.

基本上我们想要它,这样即使有工作说明,操作员也不会点击错误的按钮。

采纳答案by GoroundoVipa

'------------------------------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

This will allow you to Left click, Right click and move cursor base on X and Y Axis.

这将允许您左键单击、右键单击并基于 X 和 Y 轴移动光标。

回答by SysDragon

Try this:

尝试这个:

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