win32-UI Automation

Posted strive-sun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
}

 

以上是关于win32-UI Automation的主要内容,如果未能解决你的问题,请参考以下文章

Python win32ui.error:CreateCompatibleDC 失败

结合 Tkinter 和 win32ui 使 Python 在退出时崩溃

Win32GUI

win32gui在python3.7中截屏会导致内存泄漏

Python 截屏

python 调用windowsAPI截图