vb.net 如何使用 AutoIt 或 Windows UI 自动化单击应用程序中的按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22551669/
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-09 17:28:20  来源:igfitidea点击:

How to click buttons in an application using AutoIt or Windows UI Automation

vb.netui-automationautoit

提问by Brock Gion

Setup Environment:

设置环境:

I'm using vb.net to develop a Windows Form Application with .NET Framework 4.

我正在使用 vb.net 开发带有 .NET Framework 4 的 Windows 窗体应用程序。



My goal:

我的目标:

  1. Open calculator.exeusing Process.Start
  2. Using all vb.net code, be able to click 5 + 5 =
  1. 使用打开calculator.exeProcess.Start
  2. 使用所有vb.net代码,可以点击 5 + 5 =

I do not want to use SendKeys as a method.

我不想使用 SendKeys 作为方法。



After researching, this link provided a good start:

经过研究,此链接提供了一个良好的开端:

This tutorial (written in C#) is very similar to what I'm trying to do by using vb.net:

本教程(用 C# 编写)与我尝试使用 vb.net 所做的非常相似:



Could somebody provide a pointer on how to go about approaching this? I'd really appreciate it.

有人可以提供有关如何解决这个问题的指针吗?我真的很感激。

回答by Brock Gion

My Solution:

我的解决方案:

I tried two approaches:

我尝试了两种方法:

AutoIt was what I used because it was more reliable for my specific application.

AutoIt 是我使用的,因为它对于我的特定应用程序更可靠。

However, Windows UI worked as well. Here are both solutions.

但是,Windows UI 也能正常工作。这里有两个解决方案。



Steps if using Windows UI Automation:

使用 Windows UI 自动化的步骤

  • Identify Control IDs of buttons using Spy++
  • Add references to UIAutomationClientand UIAutomationTypes
  • Set aeDesktopas the root ae element and invoke button clicks
  • 使用 Spy++ 识别按钮的控件 ID
  • 添加对UIAutomationClient和的引用UIAutomationTypes
  • 设置aeDesktop为根 ae 元素并调用按钮点击

WindowsUI References

WindowsUI 参考

Imports System.Windows.Automation
Imports System.Threading
Imports System.Diagnostics

Public Class Form1

    Private aeDesktop As AutomationElement
    Private aeCalculator As AutomationElement

    Private ae5Btn As AutomationElement
    Private aeAddBtn As AutomationElement
    Private aeEqualsBtn As AutomationElement

    Private p As Process

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        Try

            'Set reference to the root ae element - the desktop
            aeDesktop = AutomationElement.RootElement

            'Launch Calculator application 
            p = Process.Start("C:\Windows\System32\calc.exe")

            '**********  Keep looping while waiting to get the reference to the "Calculator" on Desktop ************************************

            Dim numwaits As Integer = 0

            Do
                Debug.WriteLine("Looking for Calculator . . . ")
                aeCalculator = aeDesktop.FindFirst(TreeScope.Children, New PropertyCondition(AutomationElement.NameProperty, "Calculator"))
                numwaits += 1
                Thread.Sleep(100)

            Loop While aeCalculator Is Nothing AndAlso numwaits < 50

            If aeCalculator Is Nothing Then
                Throw New Exception("Failed to find Calculator")
            Else
                Debug.WriteLine("Found the Calculator Application!")
            End If

            '*********************************************************************************************************************************


            'NOTE: In spy++ Controlids are represented as hex (i.e. 00000087) - need to convert these to decimal (i.e. 135)

            '`5` btn
            '00000087 ---> 135
            Dim btn5hexID As String = "00000087"
            Dim btn5decimalID As String = Convert.ToInt32("00000087", 16).ToString

            '`+` btn
            '0000005D ---> 93
            Dim btnAddhexID As String = "0000005D"
            Dim btnAdddecimalID As String = Convert.ToInt32("0000005D", 16).ToString

            '`=` btn
            '00000079 ---> 121
            Dim btnEqualshexID As String = "00000079"
            Dim btnEqualsdecimalID As String = Convert.ToInt32("00000079", 16).ToString


            'Set reference for the `5` Button
            ae5Btn = aeCalculator.FindFirst(TreeScope.Descendants, New PropertyCondition(AutomationElement.AutomationIdProperty, btn5decimalID))

            'Set reference for the `+` Button
            aeAddBtn = aeCalculator.FindFirst(TreeScope.Descendants, New PropertyCondition(AutomationElement.AutomationIdProperty, btnAdddecimalID))

            'Set reference for the `=` Button
            aeEqualsBtn = aeCalculator.FindFirst(TreeScope.Descendants, New PropertyCondition(AutomationElement.AutomationIdProperty, btnEqualsdecimalID))


            'Manipulate calculator application by using invoke method to click on buttons
            Dim ipClick5Btn As InvokePattern = DirectCast(ae5Btn.GetCurrentPattern(InvokePattern.Pattern), InvokePattern)
            Dim ipClickAddBtn As InvokePattern = DirectCast(aeAddBtn.GetCurrentPattern(InvokePattern.Pattern), InvokePattern)
            Dim ipClickEqualsBtn As InvokePattern = DirectCast(aeEqualsBtn.GetCurrentPattern(InvokePattern.Pattern), InvokePattern)

            'Click 5
            ipClick5Btn.Invoke()

            'Click +
            ipClickAddBtn.Invoke()

            'Click 5
            ipClick5Btn.Invoke()

            'Click =
            ipClickEqualsBtn.Invoke()

            'Now calculator should display 10 as a result


            'Wait two seconds before closing
            Thread.Sleep(2000)

            'Exit Calculator
            p.CloseMainWindow()



        Catch ex As Exception

            'Handle any exceptions
            Debug.WriteLine("Fatal error: " & ex.Message)

        End Try

    End Sub

End Class





Steps if using AutoIt:

如果使用 AutoIt 的步骤

  • Identify ClassnameNN of buttons
  • Get a handle for calc.exe
  • Use ControlClickfunction to click buttons
  • 识别按钮的 ClassnameNN
  • 获取 calc.exe 的句柄
  • 使用ControlClick函数单击按钮

Finder Tool

查找工具



If using AutoIt choose the full installation and download the Script Editor. Paste the code in and it should work.

如果使用 AutoIt,请选择完整安装并下载脚本编辑器。粘贴代码,它应该可以工作。



;Open up Calculator
Run('calc.exe')

;Pause execution until Calculator becomes active window
WinWaitActive('Calculator')

;Get the handle for Calculator
$hWnd = WinGetHandle('Calculator')

;Using the `Finder Tool`, you can drag and drop it onto controls to see all information (i.e. Text, Class, Handle, etc.)

;`ClassnameNN: Button10` is the number 5
;`ClassnameNN: Button23` is the addition operator (+)
;`ClassnameNN: Button28` is the equals operator (=)

;***** simple operation will perform 5 + 5 = 10 **************

;click 5
ControlClick($hWnd, "", "[CLASSNN:Button10]")

;click +
ControlClick($hWnd, "", "[CLASSNN:Button23]")

;click 5
ControlClick($hWnd, "", "[CLASSNN:Button10]")

;click =
ControlClick($hWnd, "", "[CLASSNN:Button28]")

;calculator should now display 10 as a result

;************************************************************

;Wait 2 seconds to show result
Sleep(2000)

;Close Calculator
WinClose($hWnd)

Exit



Thank you for all the help and suggestions in the comments. It helped tremendously.

感谢您在评论中提供的所有帮助和建议。它有很大帮助。