2

While using automation client I am iterating over all the elements of the window.

And I want to get button...

[![enter image description here][1]][1]

Here is code...


How can I get any of these 3 property values without running through another UI.

Ðаn
  • 10,934
  • 11
  • 59
  • 95
AtiqGauri
  • 1,483
  • 13
  • 26
  • For toggle information, you need to get the TogglePattern from the element in question: https://learn.microsoft.com/en-us/windows/win32/winauto/uiauto-implementingtoggle and then call IToggleProvider::get_ToggleState method. You can check that it works if you use Inspect's Action menu. The supported patterns are displayed there and can be tried. – Simon Mourier Jun 20 '21 at 18:18
  • @SimonMourier If I have IUIAutomationElement then how can I get TogglePattern/IToggleProvider of the element. – AtiqGauri Jun 20 '21 at 18:25
  • IUIAutomationElement::GetCurrentPatternAs(UIA_TogglePatternId, etc.) – Simon Mourier Jun 20 '21 at 19:40
  • @SimonMourier Sorry to bother you again I am doing this first time, can you give me a complete example of exchanging pattern and after that, I will handle.. means a lot thanks!! – AtiqGauri Jun 20 '21 at 20:17

1 Answers1

2

Here is some sample code which, if Discord is ran, will print the "Mute" button state:

#include <windows.h>
#include <atlbase.h>
#include <atlcom.h>
#include <UIAutomationCore.h>
#include <UIAutomationClient.h>

int main()
{
    // warning: error checks omitted!
    CoInitializeEx(NULL, COINITBASE_MULTITHREADED);
    {
        // start UIA & get root
        CComPtr<IUIAutomation> automation;
        automation.CoCreateInstance(CLSID_CUIAutomation8);

        CComPtr<IUIAutomationElement> root;
        automation->GetRootElement(&root);

        // find the "Discord" window
        CComPtr<IUIAutomationCondition> discordCondition;
        automation->CreatePropertyCondition(UIA_NamePropertyId, CComVariant(L"Discord"), &discordCondition);

        CComPtr<IUIAutomationElement> discord;
        root->FindFirst(TreeScope_Children, discordCondition, &discord);

        // create a name="Mute" && type = button condition
        CComPtr<IUIAutomationCondition> muteCondition;
        automation->CreatePropertyCondition(UIA_NamePropertyId, CComVariant(L"Mute"), &muteCondition);

        CComPtr<IUIAutomationCondition> buttonCondition;
        automation->CreatePropertyCondition(UIA_ControlTypePropertyId, CComVariant(UIA_ButtonControlTypeId), &buttonCondition);

        CComPtr<IUIAutomationCondition> andCondition;
        automation->CreateAndCondition(muteCondition, buttonCondition, &andCondition);

        // get "Mute" button
        CComPtr<IUIAutomationElement> muteButton;
        discord->FindFirst(TreeScope_Subtree, andCondition, &muteButton);

        // get toggle pattern
        CComPtr<IUIAutomationTogglePattern> toggle;
        muteButton->GetCurrentPatternAs(UIA_TogglePatternId, IID_PPV_ARGS(&toggle));

        // get toggle state
        ToggleState state;
        toggle->get_CurrentToggleState(&state);

        switch (state)
        {
        case ToggleState_Off:
            wprintf(L"state is off.\n");
            break;

        case ToggleState_On:
            wprintf(L"state is on.\n");
            break;

        case ToggleState_Indeterminate:
            wprintf(L"state is indeterminate.\n");
            break;
        }
    }
    CoUninitialize();
}
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298