在继续之前等待UI加载新窗口

时间:2016-01-25 18:01:32

标签: vb.net events automation ui-automation

我是VB.Net的新手,试图为一个相当缺乏API的程序创建一个插件。基本上我想要完成的是按下界面中的按钮,这将创建一个新窗口。加载此窗口后,我会按下其他几个按钮,最终打印出它生成的HTML文件。

我尝试使用Windows UI自动化,但无法操作第一个控件(出于某种原因,它显示为"窗格"元素,并且没有支持的控件模式)

我最终能够使用PostMessage向控件发送MouseDown和MouseUp消息以激活它。

我的问题是:在继续执行代码之前,等待此窗口完成加载的最佳方法是什么?

我将点击发送到我的控件之后尝试使用Thread.Sleep,但我的代码似乎在开始加载窗口之前触发了睡眠。我怀疑可能会有某种事件驱动的选项,但我甚至不知道从哪里开始。

附注:如果有帮助,我可以访问程序的进程ID。

修改

为了更清楚地了解我所做的事情,这里有一些代码供您查看:

' These variables declared further up in my program, when plugin is loaded
Private aeDesktop As AutomationElement
Private aeRadan As AutomationElement
Private aeBlocksBtn As AutomationElement

Private Sub UITest1()
Dim rpid As Integer
Dim intret As Integer

' Note: mApp is declared further up in the code, when the plug in initializes
' It sets a reference to the application object (API command from the software)

rpid = mApp.ProcessID

aeDesktop = AutomationElement.RootElement
Dim propcon As New PropertyCondition(AutomationElement.ProcessIdProperty, rpid)
Dim propcon2 As New PropertyCondition(AutomationElement.NameProperty, "big_button_blocks.bmp")

aeRadan = aeDesktop.FindFirst(TreeScope.Children, propcon)
aeBlocksBtn = aeRadan.FindFirst(TreeScope.Descendants, propcon2)

' MAKELONG is a function that concatenates given x and y coordinates into an appropriate "lparam" value for PostMessage.
' Coordinates 20, 20 are chosen arbitrarily
Dim lParam As Long = MAKELONG(20, 20)

intret = PostMessage(CInt(aeBlocksBtn.GetCurrentPropertyValue(AutomationElement.NativeWindowHandleProperty)), &H201, &H1, lParam)
intret = PostMessage(CInt(aeBlocksBtn.GetCurrentPropertyValue(AutomationElement.NativeWindowHandleProperty)), &H202, &H0, lParam)


Dim bwinprop As New PropertyCondition(AutomationElement.NameProperty, "Radan Block Editor")
Dim aeblockwin As AutomationElement
Dim numwaits As Integer = 0

Do
numwaits += 1
Thread.Sleep(100)
aeblockwin = aeRadan.FindFirst(TreeScope.Children, bwinprop)
Loop While (numwaits < 50) AndAlso (aeblockwin Is Nothing)

If numwaits >= 50 Then
MessageBox.Show("ERROR: Block Editor Window Not found")
End If

我很确定在程序处理PostMessage之前,发生的事情与移动到Do While循环的代码有什么关系。我尝试使用SendMessage试图绕过这个,但不幸的是,这似乎没有用。

我觉得这里有一个非常简单的解决方案,比如我可能不知道的某种替代等待或睡眠命令,所以也许有人可以帮我指导它?

编辑2:

此控件的inspect.exe output屏幕截图。

此外,user interface的屏幕截图。这是来自Radan的一种CAM软件,我用它来嵌套和处理激光切割的钣金零件。我在控件周围放了一个红框,我想激活它。

0 个答案:

没有答案