使用Microsoft UI自动化获取任何应用程序的TitleBar标题吗? [英] Get TitleBar Caption of any application using Microsoft UI Automation?

查看:88
本文介绍了使用Microsoft UI自动化获取任何应用程序的TitleBar标题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C# VB.Net 中,如何使用 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?.

我一直在研究MSDN文档,但我不了解.

I've been researching in the MSDN Docs, but I don't get it.

Obtain Text Attributes Using UI Automation

然后,例如,使用下面的代码,我试图通过提供该窗口的hwnd来检索Window标题栏的文本,但是我不知道如何完全按照标题栏查找子控件(标签?)确实包含文字.

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.

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:我知道P/正在调用GetWindowText API.

PS: I'm aware of P/Invoking GetWindowText API.

推荐答案

通常,使用UI自动化,您必须使用SDK工具(UISpy或Inspect-确保它是Inspect 7.2.0.0,即一个并具有树状视图). 因此,例如,在这里,当我运行记事本时,我运行检查并看到以下内容:

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). So here for example, when I run notepad, I run inspect and see this:

我看到标题栏是主窗口的直接子项,因此我可以在窗口树中查询直接子项,并使用TitleBar控件类型作为判别式,因为在主窗口下没有其他该类型的子项.

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.

这是示例控制台应用程序C#代码,演示了如何获取无标题-记事本"标题.请注意,TitleBar也支持值"模式,但此处不需要,因为titlebar的名称也是该值.

Here is a sample console app C# code that demonstrate how to get that 'Untitled - Notepad' title. Note the TitleBar also supports the Value pattern but we don't need here because the titlebar's name is also the value.

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);
    }
}

这篇关于使用Microsoft UI自动化获取任何应用程序的TitleBar标题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆