如何从元素中获取孩子



我在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;

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

我可以这样解决它,我忘记设置真实条件,在这里,我使用的不是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);
}
```

最新更新