windows下通过uiAutomation技术获取ui元素

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

        最近接个需求要求获取 windowsui 元素经一番搜索后了解到可通过工具 UISpy.exe 或 inspect.exe 来进行查看。以软件 davinci resolve 为例

        右侧即 UISpy 工具根据内容可以看出已捕获到 davinci 界面的各属性及对应值。而 UISpyinspect 是基于 UI 自动化技术来实现的现在试着自己写段程序来获取 ui 元素。根据 官网链接 介绍

        UI 自动化包括用于标准控件的客户端提供程序库用托管代码编写且 UI 自动化客户端应用程序可以使用 C# 或 Visual Basic .NET 轻松进行编程。 作为接口实现的 UI 自动化提供程序可以用托管代码或 C/C++ 编写。

        需要先创建个 c# 项目并添加如下三个应用

         之后主方法代码如下

static void Main(string[] args)
{
    Console.WriteLine("-------------------begin-----------------");
    // 获取 davinci 进程
    Process p = Process.GetProcessesByName("Resolve")[0];

    Console.WriteLine("进程名" + p.ProcessName);
    Console.WriteLine("进程id" + p.Id);
    Console.WriteLine("进程主窗口名称" + p.MainWindowTitle);
    Console.WriteLine("进程主窗口句柄" + p.MainWindowHandle);

    // 根据进程主窗口句柄获取 AutomationElement
    AutomationElement element = AutomationElement.FromHandle(p.MainWindowHandle);
    Console.WriteLine("current automationElement name:" + element.Current.Name);
    Console.WriteLine("current automationElement id:" + element.Current.NativeWindowHandle);

    // 色温组件 AutomationId
    object obj = "UiMainWindow.bigBossWidget.widgetStack_Panel.WStackPage_Color.m_pColorPanel.frameVerticalContainer.frameColorBottom.frameColorBottomToolsContainer.m_pFrameBottomMain.UiPrimaryWidgetContainer.BorderFrame.frameTabWidgetBorder.stackedWidget.colorWheelsTab.colorWheelsMainContainer.colorWheelsSplitter.colorWheelsStackedWidget.threeWayColorWidget.frameTopToolbar.threeWayTopWidget.sliderFrame.frameTint";
    Condition findCondition = new PropertyCondition(AutomationElement.AutomationIdProperty, obj);
    // 根据 automationId 获取色温组件
    AutomationElementCollection found = element.FindAll(TreeScope.Descendants, findCondition);
    Console.WriteLine("condition found :" + found.Count);

    foreach (AutomationElement ele in found) {
        Console.WriteLine("ele control type :" + ele.Current.LocalizedControlType);
        Console.WriteLine("ele class name :" + ele.Current.ClassName);
        Console.WriteLine("ele name :" + ele.Current.Name);
        // 根据 ClassName 值获取色温及对应值
        Condition labelCondition = new PropertyCondition(AutomationElement.ClassNameProperty, "QLabel");
        AutomationElementCollection eles = ele.FindAll(TreeScope.Descendants, labelCondition);
        Console.WriteLine("label ele num :" + eles.Count);

        foreach (AutomationElement e in eles) {
            Console.WriteLine("e name :" + e.Current.Name);
        }
    }

    Console.WriteLine("--------------------end------------------");
    Console.ReadKey();
}

        程序流程主要分以下几步

  1. 根据进程名称获取进程标识
  2. 根据进程标识的主窗口句柄获取 AutomationElement
  3. 根据 AutomationId 获取对应组件如色温
  4. 在组件中查询对应属性名称和属性值。

        这里有必要补充下色温组件的 AutomationId 来源这是通过 UISpy 来查看的

        如果查询其它属性也可以利用对应的 AutomationId 来进行查询后面的 ClassNameQLabel 也是同理。最终程序输出如下

         至此也就获取了 UISpy 中显示的 ui 元素信息以上。

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: windows

“windows下通过uiAutomation技术获取ui元素” 的相关文章