我正在尝试使用UIAutomation构建自己的屏幕阅读器。我希望程序返回光标指向的元素的NameProperty

到目前为止,这是我所做的;无论如何,这只是示例代码:

#include <iostream>
#include <windows.h>
#include <UIAutomation.h>

const int MAX_WND_TEXT = 60;

IUIAutomation *automation = NULL;

BOOL InitializeUIAutomation(IUIAutomation **pAutomation)
{
    CoInitialize(NULL);
    HRESULT hr = CoCreateInstance(__uuidof(CUIAutomation), NULL,
        CLSCTX_INPROC_SERVER,
        __uuidof(IUIAutomation), (void**)pAutomation);
    return (SUCCEEDED(hr));
}

int main()
{
    POINT p;

    IUIAutomationElement *elem;
    wchar_t wndName[MAX_WND_TEXT];
    BOOL stat = InitializeUIAutomation(&automation);
    while (true)
    {
        if (stat)
        {
            GetCursorPos(&p);
            HRESULT hr = automation->ElementFromPoint(p, &elem);
            if (SUCCEEDED(hr))
            {
                HRESULT hr = elem->GetCurrentPropertyValue(UIA_NamePropertyId,
                    (VARIANT*)wndName);
                if (SUCCEEDED(hr))
                    std::cout << wndName << std::endl;
                else
                    wndName[0] = '\0';
            }
            else
                std::cout << "No element selected." << std::endl;

            Sleep(100);
            elem->Release();
        }
    }
    automation->Release();
    CoUninitialize();
    return 0;
}

现在的问题是我无法打印所需的值。该程序仅输出特定的十六进制数。而且我还是UIAutomation的初学者,所以我仍然迷路。

您能帮我还是给我一些解决问题的技巧?

最佳答案

使用此代码解决了我的问题。

#include <iostream>
#include <string>
#include <Windows.h>
#include <UIAutomation.h>

BOOL InitializeUIAutomation(IUIAutomation **automation)
{
    CoInitialize(NULL);
    HRESULT hr = CoCreateInstance(__uuidof(CUIAutomation), NULL,
        CLSCTX_INPROC_SERVER, __uuidof(IUIAutomation),
        (void**)automation);
    return (SUCCEEDED(hr));
}

int main()
{
    IUIAutomation *automation = NULL;
    IUIAutomationElement *elem = NULL;
    BOOL stat = InitializeUIAutomation(&automation);
    POINT mousePt;
    BSTR elemName = NULL;
    if (stat)
    {
        while(true)
        {
            GetCursorPos(&mousePt);
            HRESULT hr = automation->ElementFromPoint(mousePt, &elem);
            if (SUCCEEDED(hr) && elem != NULL)
            {
                elem->get_CurrentName(&elemName);
                std::wstring ws(elemName, SysStringLen(elemName));
                std::wcout << ws << std::endl;
            }
            SysFreeString(elemName);
            elem->Release();
            Sleep(200);
        }
    }
    automation->Release();
    CoUninitialize();

    return 0;
}

毕竟,打印的十六进制数字是BSTR header 。通过将BSTR转换为wstring解决我的问题。

关于c++ - 如何通过将光标悬停在UIAutomationElement的NamePropertyId上?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54309909/

10-12 20:40