【问题标题】:Checkbox (checked or unchecked)复选框(选中或未选中)
【发布时间】:2021-06-20 17:15:51
【问题描述】:

在使用自动化客户端时,我正在遍历窗口的所有元素。

我想得到按钮...

[![在此处输入图片描述][1]][1]

这是代码...


如何在不运行另一个 UI 的情况下获取这 3 个属性值中的任何一个。

【问题讨论】:

  • 对于切换信息,您需要从相关元素中获取 TogglePattern:docs.microsoft.com/en-us/windows/win32/winauto/…,然后调用 IToggleProvider::get_ToggleState 方法。如果您使用 Inspect 的 Action 菜单,您可以检查它是否有效。支持的模式显示在那里,可以尝试。
  • @SimonMourier 如果我有 IUIAutomationElement 那么我如何获得该元素的 TogglePattern/IToggleProvider。
  • IUIAutomationElement::GetCurrentPatternAs(UIA_TogglePatternId 等)
  • @SimonMourier 很抱歉再次打扰您,我是第一次这样做,您能给我一个完整的交换模式示例,然后我会处理.. 非常感谢!!

标签: windows winapi win32gui microsoft-ui-automation


【解决方案1】:

这是一些示例代码,如果运行 Discord,将打印“静音”按钮状态:

#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();
}

【讨论】:

  • 感谢您付出的时间和精力来回答...我想成为像您一样的优秀程序员!...有什么口头禅,您想分享吗? :)
  • “任何事情的专家都曾是初学者”怎么样 :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-23
  • 2016-01-17
  • 2015-12-06
  • 2019-01-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多