我正在尝试使用System.Windows.Automation来访问VLC媒体播放器中的UI元素(特别是最左边角落的状态框,显示当前正在播放的视频的文件名)。我可以获取父元素和兄弟元素,但在Spy ++中,所有在它们旁边都有一个暗淡图标的元素我无法在代码中到达...我假设暗淡的图标意味着它们是私有的或隐藏的或类似的东西。这是一张显示我的意思的图片:
请注意,我有一个带有句柄0x30826的父对象的引用,我从那里做了一个FindAll()*并且只得到一个结果,即对句柄为0x30858的子进程的引用。你可以在Spy ++中看到有5个孩子的0x30826,但只有其中一个,我在做FindAll时得到的,有一个全黑的图标,其他的有一个灰色图标,我无法找到它们。另请注意,我想要的是0x20908,它有一个灰色图标......
我怎样才能在代码中找到它?
*这是我用来尝试获取0x30826的所有子代的代码:
Dim aeDesktop As AutomationElement
Dim aeVLC As AutomationElement
Dim c As AutomationElementCollection
Dim cd As New AndCondition(New PropertyCondition(AutomationElement.IsEnabledProperty, True), New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.StatusBar))
aeVLC = aeDesktop.FindFirst(TreeScope.Children, New PropertyCondition(AutomationElement.NameProperty, "got s01e01.avi - VLC media player"))
c = aeVLC.FindAll(TreeScope.Children, cd)
c = c(0).FindAll(TreeScope.Children, Condition.TrueCondition)
第一个FindAll()只给我0x30826,这很好,因为这就是我想要的,但是没有指定条件的第二个FindAll只给出0x30858,当我在Spy ++中看到加上4个其他时,包括我想要的那个。
你真的在使用Spy ++代替Inspect Program来阻碍你的努力。使用Inspect,您可以轻松地看到目标元素是一个文本元素,该元素是作为主窗口元素父级的状态栏元素的父级。
使用该信息,获得对目标文本元素的引用是直截了当的。首先获取主窗口,然后是状态栏,最后是状态栏的第一个文本元素。
' find the VLC process
Dim targets As Process() = Process.GetProcessesByName("vlc")
If targets.Length > 0 Then
' assume its the 1st process
Dim vlcMainWindowHandle As IntPtr = targets(0).MainWindowHandle
' release all processes obtained
For Each p As Process In targets
p.Dispose()
Next
' use vlcMainWindowHandle to get application window element
Dim vlcMain As AutomationElement = AutomationElement.FromHandle(vlcMainWindowHandle)
' get the statusbar
Dim getStatusBarCondition As Condition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.StatusBar)
Dim statusBar As AutomationElement = vlcMain.FindFirst(TreeScope.Children, getStatusBarCondition)
' get the 1st textbox in the statusbar
Dim getTextBoxCondition As Condition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Text)
Dim targetTextBox As AutomationElement = statusBar.FindFirst(TreeScope.Children, getTextBoxCondition)
' normally you use either a TextPattern.Pattern or ValuePattern.Pattern
' to obtain the text, but this textbox exposes neither and it uses the
' the Name property for the text.
Dim textYouWant As String = targetTextBox.Current.Name
End If