【发布时间】:2021-03-19 12:29:30
【问题描述】:
我想在 Windows 上使用带有 C++ 的 uiautomation 获取此 Zoom Meeting 的“静音我的音频”标题
这是我当前调用的代码:
// Get the handle of the Zoom Meetings window.
zoomWnd = ::FindWindow(NULL, "Zoom Meeting");
if (zoomWnd != NULL)
{
std::cout<<"zoom meeting"<<"\n";
IUIAutomationElement *zoom = GetTopLevelWindowByName(L"Zoom Meeting");
rawListDescendants(zoom, "Meeting");
}
输出:
zoom meeting
sName: ContentLeftPanel
sName: VideoContainerWnd
sName: Video Content
sName:
sName:
sName:
GetTopLevelWindowByName 函数定义:
IUIAutomationElement *GetTopLevelWindowByName(LPWSTR windowName)
{
if (windowName == NULL)
{
return NULL;
}
VARIANT varProp;
varProp.vt = VT_BSTR;
varProp.bstrVal = SysAllocString(windowName);
if (varProp.bstrVal == NULL)
{
return NULL;
}
IUIAutomationElement *pRoot = NULL;
IUIAutomationElement *pFound = NULL;
// Get the desktop element.
HRESULT hr = g_pAutomation->GetRootElement(&pRoot);
if (FAILED(hr) || pRoot == NULL)
{
goto cleanup;
}
// Get a top-level element by name, such as "Program Manager"
IUIAutomationCondition *pCondition = NULL;
hr = g_pAutomation->CreatePropertyCondition(UIA_NamePropertyId, varProp, &pCondition);
if (FAILED(hr))
{
goto cleanup;
}
pRoot->FindFirst(TreeScope_Children, pCondition, &pFound);
cleanup:
if (pRoot != NULL)
pRoot->Release();
if (pCondition != NULL)
pCondition->Release();
VariantClear(&varProp);
return pFound;
}
rawListDescendants 函数定义:
void rawListDescendants(IUIAutomationElement *pParent, std::string windowType)
{
if (pParent == NULL)
return;
IUIAutomationTreeWalker *pControlWalker = NULL;
IUIAutomationElement *pNode = NULL;
g_pAutomation->get_RawViewWalker(&pControlWalker);
if (pControlWalker == NULL)
{
goto cleanup;
}
pControlWalker->GetFirstChildElement(pParent, &pNode);
if (pNode == NULL)
{
goto cleanup;
}
while (pNode)
{
BSTR desc;
BSTR sName;
pNode->get_CurrentLocalizedControlType(&desc);
pNode->get_CurrentName(&sName);
std::wcout<<"sName: "<<sName<<"\n";
//std::wcout<<"desc: "<<desc<<"\n";
// only go into windows and panes
if (desc != NULL)
if (0 == wcscmp(desc, L"window") || 0 == wcscmp(desc, L"pane"))
rawListDescendants(pNode, windowType);
if (desc != NULL)
SysFreeString(desc);
if (sName != NULL)
SysFreeString(sName);
// get the next element
IUIAutomationElement *pNext;
pControlWalker->GetNextSiblingElement(pNode, &pNext);
pNode->Release();
pNode = pNext;
}
cleanup:
if (pControlWalker != NULL)
pControlWalker->Release();
if (pNode != NULL)
pNode->Release();
return;
}
Zoom Meeting 窗口的 Spy++ windows 树:
如何爬到子窗口并获取它的按钮标题
我之前没有使用过这个库,所以请尽可能详细说明(谢谢)
【问题讨论】:
-
由于您已经发现了包含源代码的文档部分(How to Find UI Elements 和 How to Walk the UI Automation Tree),您需要向上导航左侧的树视图以了解您复制的代码的作用、为什么以及如何合并这些更改,以便它满足您的需要。
-
@llnspectable 谢谢人...但是你能帮我解决这个问题吗,已经花了 2 天时间解决这个问题..
-
从使用正确的工具开始。 Spy++ 不会有帮助。 Inspect 将。
-
@llnspectable 感谢该工具,现在我可以看到其中的所有窗口和元素...imgur.com/a/tBXYqLM
-
@llnspectable 我已经创建了一个函数来列出所有具有递归功能的后代......但它没有列出所有窗口。这是代码:codepile.net/pile/xPJQXpeZ这是所有窗口:imgur.com/a/tBXYqLM
标签: c++ windows winapi ui-automation microsoft-ui-automation