c++ - 如何通过将光标悬停在UIAutomationElement的NamePropertyId上?

标签 c++ ui-automation microsoft-ui-automation

我正在尝试使用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/

相关文章:

c++ - 测试返回三角形类型的函数

c# - 使用 WinAppDriver 自动化 DevExpress 电子表格/ GridView

c# - 有什么方法可以保留 UIAutomation 元素吗?

c# - 是否可以获取任何窗口的选定文本,包括非 UI 自动化元素?

c++ - std::unique_ptr 作为参数的正确复制语义

C++ 11 智能指针的使用

c# - 计算新的 ScrollPercent - 在 ViewSize 改变之后

windows - 复选框(选中或未选中)

C++ 理解类和构造函数

testing - Selenium Web测试自动化框架最佳实践