UI自动化的应用程序按钮

UI自动化的应用程序按钮

本文介绍了如何单击使用的AutoIt或Windows UI自动化的应用程序按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置环境:

我用vb.net开发与Windows窗体应用程序的.NET Framework 4。

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


我的目标:

  1. 开启的 calculator.exe 的使用的Process.Start
  2. 使用所有vb.net code,可以点击 5 + 5 =
  1. Open calculator.exe using Process.Start
  2. Using all vb.net code, be able to click 5 + 5 =

我不希望使用的SendKeys作为方法

I do not want to use SendKeys as a method.


研究之后,这个链接提供了一个良好的开端:

本教程(C#编写的)非常相似,我想要做的使用vb.net:

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


可能有人提供关于如何去接近这个指针?我真的AP preciate吧。

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

推荐答案

我的解决方法:

我尝试了两种方式:

  • Windows UI Automation
  • AutoIt

AutoIt的是我使用,因为它是更可靠的为我的具体应用。

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

不过,Windows用户界面工作过。这里有两种解决方案。

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


步骤如果使用Windows UI自动化

  • 在确定使用间谍按钮控件ID ++
  • 添加引用 UIAutomationClient UIAutomationTypes
  • 设置 aeDesktop 作为根AE元素,并调用按钮点击
  • Identify Control IDs of buttons using Spy++
  • Add references to UIAutomationClient and UIAutomationTypes
  • Set aeDesktop as the root ae element and invoke button clicks

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





步骤,如果使用的AutoIt

  • 确定按钮ClassnameNN
  • 获取一个句柄的calc.exe
  • 使用 ControlClick 的功能,点击按钮
  • Identify ClassnameNN of buttons
  • Get a handle for calc.exe
  • Use ControlClick function to click buttons


如果使用的AutoIt选择完全安装并下载脚本编辑器。在粘贴code,它应该工作。

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


;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.

这篇关于如何单击使用的AutoIt或Windows UI自动化的应用程序按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 08:27