C++ 如何通过悬停光标来获取UIAutomationElement的NamePropertyId?

C++ 如何通过悬停光标来获取UIAutomationElement的NamePropertyId?,c++,ui-automation,microsoft-ui-automation,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 InitializeUI

我正在尝试使用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;
}
#包括
#包括
#包括
常量int MAX_WND_TEXT=60;
IUIAutomation*automation=NULL;
布尔初始化自动化(IUIAutomation**pAutomation)
{
共初始化(空);
HRESULT hr=CoCreateInstance(uuuIdof(CUIAutomation),NULL,
CLSCTX_INPROC_服务器,
__uuidof(IUIAutomation),(void**)pAutomation;
返回(成功(hr));
}
int main()
{
p点;
IUIAutomationElement*元素;
wchar_t wndName[最大文本];
BOOL stat=初始化自动化(和自动化);
while(true)
{
如果(统计)
{
GetCursorPos&p;
HRESULT hr=自动化->元素起点(p和元素);
如果(成功(hr))
{
HRESULT hr=elem->GetCurrentPropertyValue(UIA_NamePropertyId,
(变体*)wndName);
如果(成功(hr))

std::cout使用此代码解决了我的问题

#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;
}
#包括
#包括
#包括
#包括
布尔初始化自动化(自动化**自动化)
{
共初始化(空);
HRESULT hr=CoCreateInstance(uuuIdof(CUIAutomation),NULL,
CLSCTX\u在过程服务器中,\uuuuuIdof(IUIAutomation),
(a)自动化);
返回(成功(hr));
}
int main()
{
IUIAutomation*automation=NULL;
IUIAutomationeElement*elem=NULL;
BOOL stat=初始化自动化(和自动化);
莫塞普点;
BSTR elemName=NULL;
如果(统计)
{
while(true)
{
GetCursorPos(&mousePt);
HRESULT hr=自动化->元素起点(鼠标点和元素);
if(成功(hr)&&elem!=NULL)
{
元素->获取当前名称(&elemName);
std::wstring ws(elemName,SysStringLen(elemName));
std::wcout Release();
coninitialize();
返回0;
}

打印的十六进制数毕竟是BSTR头。通过将BSTR转换为WSTR来解决我的问题。

谢谢,我有没有办法知道元素是可调用的(按钮是可调用的)、可扩展的(组合框是可扩展的)或可选择的(列表项是可选择的)??