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);
    }
  • 相关阅读:
    Android 六种核心安全机制
    Android 网络通信 HTTP
    Android Thread和AsyncTask
    C#(少用的)
    Asp.net动态生成表单
    设计模式--职责链(学习)
    Extjs表单验证小结
    C#框架
    Javascript获取IFrame内容(兼容IE&FF)
    最近在忙淘宝店的事
  • 原文地址:https://www.cnblogs.com/strive-sun/p/12895954.html
Copyright © 2011-2022 走看看