首页 > 解决方案 > 如何从元素中获取孩子?

问题描述

我有我的元素IUIAutomationElement elem,现在我想得到它的孩子,

为此,我尝试遵循此代码

IUIAutomationElement* dummy = NULL;     //creating a dummy
IUIAutomationElementArray* children_array;   //creating an array 
elem->GetCachedChildren(&children_array); 
children_array->GetElement(0,&dummy);
qDebug() << dummy;

它不起作用,我得到未处理的异常。我哪里错了?

标签: c++winapiui-automationmicrosoft-ui-automation

解决方案


我能够像这样解决它,我忘记设置真实条件,在这里GetCachedChildren()我使用而不是使用FindAll(),因为这更好地服务于目的。

IUIAutomationElementArray* children_array = NULL;
                            IUIAutomationElement* single_elem = nullptr;
                            TreeScope treeScope_1 = TreeScope::TreeScope_Children;

                            IUIAutomationCondition* Condition;
                            automation->CreateTrueCondition(&Condition);

                            HRESULT hs = elem->FindAll(treeScope_1, Condition, &children_array);
                           // HRESULT hs = elem->GetCachedChildren(&children_array);
                            if (SUCCEEDED(hs) && children_array != NULL) {
                                int number = 0;
                                children_array->get_Length(&number);
                                for (int i = 0;i < number;i++)
                                {
                                    children_array->GetElement(i, &single_elem);
                                    if (single_elem != NULL)
                                    {
                                        BSTR child_name = NULL;
                                        hs = single_elem->get_CurrentName(&child_name);
                                        if (SUCCEEDED(hs) && child_name != NULL)
                                        {
                                            std::wstring child_ws(child_name, SysStringLen(child_name));
                                            QString child_qstring = QString::fromStdWString(child_ws);
                                            global_ui->xml_scripts_textbox->addItem(child_qstring);
                                            qDebug() << child_qstring;
                                        }
                                        SysFreeString(child_name);
                                    }
                                    single_elem = NULL;
                                }
                                SAFE_RELEASE(children_array);
                            }
                       ```

推荐阅读