.net – 如何使用AutoIt或Windows UI Automation单击应用程序中的按钮

前端之家收集整理的这篇文章主要介绍了.net – 如何使用AutoIt或Windows UI Automation单击应用程序中的按钮前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
安装环境:

我正在使用vb.net开发一个Windows窗体应用程序与.NET Framework 4.

我的目标:

>使用Process.Start打开calculator.exe
>使用所有vb.net代码,可以点击5 5 =

我不想使用SendKeys作为一种方法.

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

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

> How to click a button in another application

有人可以提供一个关于如何处理这个问题的指针吗?我真的很感激.

我的解决方

我尝试了两种方法

> Windows UI Automation
> AutoIt

AutoIt是我使用的,因为它对我的具体应用程序更可靠.

但是,Windows UI也是如此.这里有两个解决方案.

使用Windows UI自动化的步骤:

>使用Spy识别按钮的控制ID
>添加对UIAutomationClient和UIAutomationTypes的引用
>将aeDesktop设置为root ae元素并调用按钮点击

  1. Imports System.Windows.Automation
  2. Imports System.Threading
  3. Imports System.Diagnostics
  4.  
  5. Public Class Form1
  6.  
  7. Private aeDesktop As AutomationElement
  8. Private aeCalculator As AutomationElement
  9.  
  10. Private ae5Btn As AutomationElement
  11. Private aeAddBtn As AutomationElement
  12. Private aeEqualsBtn As AutomationElement
  13.  
  14. Private p As Process
  15.  
  16. Private Sub Button1_Click(sender As System.Object,e As System.EventArgs) Handles Button1.Click
  17.  
  18. Try
  19.  
  20. 'Set reference to the root ae element - the desktop
  21. aeDesktop = AutomationElement.RootElement
  22.  
  23. 'Launch Calculator application
  24. p = Process.Start("C:\Windows\System32\calc.exe")
  25.  
  26. '********** Keep looping while waiting to get the reference to the "Calculator" on Desktop ************************************
  27.  
  28. Dim numwaits As Integer = 0
  29.  
  30. Do
  31. Debug.WriteLine("Looking for Calculator . . . ")
  32. aeCalculator = aeDesktop.FindFirst(TreeScope.Children,New PropertyCondition(AutomationElement.NameProperty,"Calculator"))
  33. numwaits += 1
  34. Thread.Sleep(100)
  35.  
  36. Loop While aeCalculator Is Nothing AndAlso numwaits < 50
  37.  
  38. If aeCalculator Is Nothing Then
  39. Throw New Exception("Failed to find Calculator")
  40. Else
  41. Debug.WriteLine("Found the Calculator Application!")
  42. End If
  43.  
  44. '*********************************************************************************************************************************
  45.  
  46.  
  47. 'NOTE: In spy++ Controlids are represented as hex (i.e. 00000087) - need to convert these to decimal (i.e. 135)
  48.  
  49. '`5` btn
  50. '00000087 ---> 135
  51. Dim btn5hexID As String = "00000087"
  52. Dim btn5decimalID As String = Convert.ToInt32("00000087",16).ToString
  53.  
  54. '`+` btn
  55. '0000005D ---> 93
  56. Dim btnAddhexID As String = "0000005D"
  57. Dim btnAdddecimalID As String = Convert.ToInt32("0000005D",16).ToString
  58.  
  59. '`=` btn
  60. '00000079 ---> 121
  61. Dim btnEqualshexID As String = "00000079"
  62. Dim btnEqualsdecimalID As String = Convert.ToInt32("00000079",16).ToString
  63.  
  64.  
  65. 'Set reference for the `5` Button
  66. ae5Btn = aeCalculator.FindFirst(TreeScope.Descendants,New PropertyCondition(AutomationElement.AutomationIdProperty,btn5decimalID))
  67.  
  68. 'Set reference for the `+` Button
  69. aeAddBtn = aeCalculator.FindFirst(TreeScope.Descendants,btnAdddecimalID))
  70.  
  71. 'Set reference for the `=` Button
  72. aeEqualsBtn = aeCalculator.FindFirst(TreeScope.Descendants,btnEqualsdecimalID))
  73.  
  74.  
  75. 'Manipulate calculator application by using invoke method to click on buttons
  76. Dim ipClick5Btn As InvokePattern = DirectCast(ae5Btn.GetCurrentPattern(InvokePattern.Pattern),InvokePattern)
  77. Dim ipClickAddBtn As InvokePattern = DirectCast(aeAddBtn.GetCurrentPattern(InvokePattern.Pattern),InvokePattern)
  78. Dim ipClickEqualsBtn As InvokePattern = DirectCast(aeEqualsBtn.GetCurrentPattern(InvokePattern.Pattern),InvokePattern)
  79.  
  80. 'Click 5
  81. ipClick5Btn.Invoke()
  82.  
  83. 'Click +
  84. ipClickAddBtn.Invoke()
  85.  
  86. 'Click 5
  87. ipClick5Btn.Invoke()
  88.  
  89. 'Click =
  90. ipClickEqualsBtn.Invoke()
  91.  
  92. 'Now calculator should display 10 as a result
  93.  
  94.  
  95. 'Wait two seconds before closing
  96. Thread.Sleep(2000)
  97.  
  98. 'Exit Calculator
  99. p.CloseMainWindow()
  100.  
  101.  
  102.  
  103. Catch ex As Exception
  104.  
  105. 'Handle any exceptions
  106. Debug.WriteLine("Fatal error: " & ex.Message)
  107.  
  108. End Try
  109.  
  110. End Sub
  111.  
  112. End Class

使用AutoIt的步骤:

>识别按钮的ClassnameNN
>获取calc.exe的句柄
>使用ControlClick功能点击按钮

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

> Download AutoIt

  1. ;Open up Calculator
  2. Run('calc.exe')
  3.  
  4. ;Pause execution until Calculator becomes active window
  5. WinWaitActive('Calculator')
  6.  
  7. ;Get the handle for Calculator
  8. $hWnd = WinGetHandle('Calculator')
  9.  
  10. ;Using the `Finder Tool`,you can drag and drop it onto controls to see all information (i.e. Text,Class,Handle,etc.)
  11.  
  12. ;`ClassnameNN: Button10` is the number 5
  13. ;`ClassnameNN: Button23` is the addition operator (+)
  14. ;`ClassnameNN: Button28` is the equals operator (=)
  15.  
  16. ;***** simple operation will perform 5 + 5 = 10 **************
  17.  
  18. ;click 5
  19. ControlClick($hWnd,"","[CLASSNN:Button10]")
  20.  
  21. ;click +
  22. ControlClick($hWnd,"[CLASSNN:Button23]")
  23.  
  24. ;click 5
  25. ControlClick($hWnd,"[CLASSNN:Button10]")
  26.  
  27. ;click =
  28. ControlClick($hWnd,"[CLASSNN:Button28]")
  29.  
  30. ;calculator should now display 10 as a result
  31.  
  32. ;************************************************************
  33.  
  34. ;Wait 2 seconds to show result
  35. Sleep(2000)
  36.  
  37. ;Close Calculator
  38. WinClose($hWnd)
  39.  
  40. Exit

感谢您的意见中的所有帮助和建议.它非常有帮助

猜你在找的Windows相关文章