简体   繁体   English

使用Microsoft UI自动化获取任何应用程序的TitleBar标题吗?

[英]Get TitleBar Caption of any application using Microsoft UI Automation?

In C# or else VB.Net , how I could use Microsoft UI Automation to retrieve the text of any control that contains text?. C#VB.Net中 ,如何使用Microsoft UI Automation检索包含文本的任何控件的文本?

I've been researching in the MSDN Docs, but I don't get it. 我一直在研究MSDN文档,但我不了解。

Obtain Text Attributes Using UI Automation 使用UI自动化获取文本属性

Then, for example, with the code below I'm trying to retrieve the text of the Window titlebar by giving the hwnd of that window, but I don't know exactlly how to follow the titlebar to find the child control (label?) that really contains the text. 然后,例如,使用下面的代码,我试图通过提供该窗口的hwnd来检索“窗口”标题栏的文本,但是我不知道如何完全按照标题栏查找子控件(标签?)。确实包含文字。

Imports System.Windows.Automation
Imports System.Windows.Automation.Text

.

Dim hwnd As IntPtr = Process.GetProcessesByName("notepad").First.MainWindowHandle

Dim targetApp As AutomationElement = AutomationElement.FromHandle(hwnd)

' The control type we're looking for; in this case 'TitleBar' 
Dim cond1 As New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TitleBar)

Dim targetTextElement As AutomationElement =
    targetApp.FindFirst(TreeScope.Descendants, cond1)

Debug.WriteLine(targetTextElement Is Nothing)

In the example above I'm trying with the titlebar, but just I would like to do it with any other control that contains text ...like a titlebar. 在上面的示例中,我尝试使用标题栏,但是我想使用其他任何包含文本的控件(例如标题栏)来执行此操作。

PS: I'm aware of P/Invoking GetWindowText API. PS:我知道P /正在调用GetWindowText API。

With UI Automation, in general, you have to analyze the target application using the SDK tools (UISpy or Inspect - make sure it's Inspect 7.2.0.0, the one with a tree view). 通常,使用UI自动化,您必须使用SDK工具(UISpy或Inspect-确保它是Inspect 7.2.0.0,即具有树视图的工具)来分析目标应用程序。 So here for example, when I run notepad, I run inspect and see this: 因此,例如,在这里,当我运行记事本时,我运行检查并看到以下内容:

在此处输入图片说明

I see the titlebar is a direct child of the main window, so I can just query the window tree for direct children and use the TitleBar control type as a discriminant because there's no other child of that type beneath the main window. 我看到标题栏是主窗口的直接子项,因此我可以在窗口树中查询直接子项,并使用TitleBar控件类型作为判别式,因为在主窗口下没有其他该类型的子项。

Here is a sample console app C# code that demonstrate how to get that 'Untitled - Notepad' title. 这是一个示例控制台应用程序C#代码,演示了如何获取“无标题-记事本”标题。 Note the TitleBar also supports the Value pattern but we don't need here because the titlebar's name is also the value. 请注意,TitleBar也支持Value模式,但是这里不需要,因为titlebar的名称也是值。

class Program
{
    static void Main(string[] args)    
    {
        // start our own notepad from scratch
        Process process = Process.Start("notepad.exe");
        // wait for main window to appear
        while(process.MainWindowHandle == IntPtr.Zero)
        {
            Thread.Sleep(100);
            process.Refresh();
        }
        var window = AutomationElement.FromHandle(process.MainWindowHandle);
        Console.WriteLine("window: " + window.Current.Name);

        // note: carefully choose the tree scope for perf reasons
        // try to avoid SubTree although it seems easier...
        var titleBar = window.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TitleBar));
        Console.WriteLine("titleBar: " + titleBar.Current.Name);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM