zoukankan      html  css  js  c++  java
  • win32-UI Automation

    使用UI Automation遍历窗口的所有控件标题和类

    #include <Windows.h>
    #include <stdio.h>
    #include <UIAutomation.h>
    
    IUIAutomation* pClientUIA;
    IUIAutomationElement* pRootElement;
    
    void FindControl(const long controlType)
    {
        HRESULT hr;
        BSTR name;
        IUIAutomationCondition* pCondition;
        VARIANT varProp;
        varProp.vt = VT_I4;
        varProp.uintVal = controlType;
        hr = pClientUIA->CreatePropertyCondition(UIA_ControlTypePropertyId, varProp, &pCondition);
        if (S_OK != hr)
        {
            printf("CreatePropertyCondition error: %d
    ", GetLastError());
        }
    
        IUIAutomationElementArray* pElementFound;
        hr = pRootElement->FindAll(TreeScope_Subtree, pCondition, &pElementFound);
        if (S_OK != hr)
        {
            printf("CreatePropertyCondition error: %d
    ", GetLastError());
        }
        int eleCount;
        pElementFound->get_Length(&eleCount);
        for (int i = 0; i <= eleCount; i++)
        {
            IUIAutomationElement* pElement;
            hr = pElementFound->GetElement(i, &pElement);
            if (S_OK != hr)
            {
                printf("CreatePropertyCondition error: %d
    ", GetLastError());
            }
            hr = pElement->get_CurrentName(&name);
            if (S_OK != hr)
            {
                printf("CreatePropertyCondition error: %d
    ", GetLastError());
            }
            wprintf(L"Control Name: %s
    ", name);
            hr = pElement->get_CurrentClassName(&name);
            if (S_OK != hr)
            {
                printf("CreatePropertyCondition error: %d
    ", GetLastError());
            }
            wprintf(L"Class Name: %s
    ", name);
        }
    }
    
    int main()
    {
        HRESULT hr = CoInitializeEx(NULL, COINITBASE_MULTITHREADED | COINIT_DISABLE_OLE1DDE);
        if (S_OK != hr)
        {
            printf("CoInitializeEx error: %d
    ", GetLastError());
            return 1;
        }
    
        hr = CoCreateInstance(CLSID_CUIAutomation, NULL, CLSCTX_INPROC_SERVER, IID_IUIAutomation, reinterpret_cast<void**>(&pClientUIA));
        if (S_OK != hr)
        {
            printf("CoCreateInstance error: %d
    ", GetLastError());
            return 1;
        }
    
    
        HWND hwnd = (HWND)0x00030AF6;
        if (hwnd == NULL)
        {
            printf("FindWindow error: %d
    ", GetLastError());
            return 1;
        }
    
        hr = pClientUIA->ElementFromHandle(hwnd, &pRootElement);
        if (S_OK != hr)
        {
            printf("ElementFromHandle error: %d
    ", GetLastError());
            return 1;
        }
    
        FindControl(UIA_TextControlTypeId);
    }
  • 相关阅读:
    为EasySharding.EFCore提供Dapper相关查询扩展
    古典音乐作品编号标记解读
    乐理基础
    音乐指挥家
    2021年,年终总结
    有关Android launchMode 在APP(task)之间的应用——扔物线视频
    Springboot Jpa 有关多数据源的问题
    Python运算符及优先级
    高中信息技术(Python)重难点4:切片
    高中信息技术(Python)必修1 数据与计算 出现的模块和函数
  • 原文地址:https://www.cnblogs.com/strive-sun/p/12895954.html
Copyright © 2011-2022 走看看