Как получить NamePropertyId элемента UIAutomationElement при наведении курсора? - PullRequest
0 голосов
/ 22 января 2019

Я пытаюсь создать свою собственную программу чтения с экрана с помощью 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, поэтому я все еще потерялся.

Можете ли вы мне помочь или дать советы, как решить мою проблему?

1 Ответ

0 голосов
/ 24 января 2019

Решил мою проблему с помощью этого кода.

#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.Решите мою проблему, преобразовав BSTR в wstring.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...