0

I'm trying to get the info about the current window under the cursor, I got the function working when I manually specify the hwnd, how I could get the hwnd from the current window under the mouse?

#include <UIAutomation.h>

POINT p;
GetCursorPos(&p);CComPtr<IAccessible> pAcc;


VARIANT varChild;
if (SUCCEEDED(AccessibleObjectFromWindow((HWND)hwnd, 
    OBJID_WINDOW,IID_IAccessible, reinterpret_cast<void**>(&pAcc))))
{
    CComBSTR bstrName, bstrValue, bstrDescription;
    varChild.vt = VT_I4;
    varChild.lVal = CHILDID_SELF;
    if (SUCCEEDED(pAcc->get_accName(varChild, &bstrName)))
        auto name = bstrName.m_str;

    if (SUCCEEDED(pAcc->get_accValue(varChild, &bstrValue)))
        auto value = bstrValue.m_str;

    if (SUCCEEDED(pAcc->get_accValue(varChild, &bstrDescription)))
        auto description = bstrDescription.m_str;

}
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298

1 Answers1

2

What you're looking for is IUIAutomation::ElementFromPoint method.

Here is a small console app (C++ with Visual Studio ATL) that continuously displays the name and window handle (if any) of the automation element under the cursor:

// needs
//#include <UIAutomationCore.h>
//#include <UIAutomationClient.h>

int main()
{
    if (SUCCEEDED(CoInitialize(NULL)))
    {
        CComPtr<IUIAutomation> automation;
        if (SUCCEEDED(automation.CoCreateInstance(CLSID_CUIAutomation8))) // or CLSID_CUIAutomation
        {
            do
            {
                POINT pos;
                if (GetCursorPos(&pos))
                {
                    CComPtr<IUIAutomationElement> element;
                    if (SUCCEEDED(automation->ElementFromPoint(pos, &element)))
                    {
                        CComBSTR name;
                        element->get_CurrentName(&name);
                        wprintf(L"name: %s\n", name);

                        UIA_HWND hwnd;
                        element->get_CurrentNativeWindowHandle(&hwnd);
                        wprintf(L"hwnd: %p\n", hwnd);
                    }
                }
                Sleep(500);
            } while (TRUE);
        }
    }

    CoUninitialize();
    return 0;
}
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • Could you tell me how to perform the default action of the current element under the cursor? –  Sep 06 '21 at 05:04
  • @Caio - There's no "default action", it depends on what's under the cursor. You must find what "pattern" is implemented by the element https://learn.microsoft.com/en-us/windows/win32/winauto/uiauto-controlpatternsoverview and use that pattern. You can use inspect tool from Windows SDK to test all patterns. https://learn.microsoft.com/en-us/windows/win32/winauto/inspect-objects – Simon Mourier Sep 06 '21 at 06:05