試圖按照這里的例子:
https://docs.microsoft.com/en-us/windows/win32/winauto/uiauto-howto-find-ui-elements
WCHAR window_name[250] = L "tools"。
IUIAutomationElement *window = GetTopLevelWindowByName(window_name)。
IUIAutomationElement* GetTopLevelWindowByName(LPWSTR windowName)/span>
{
if (windowName == NULL)
return NULL。
CComPtr<IUIAutomation> g_pAutomation;
VARIANT varProp;
varProp.vt = VT_BSTR;
varProp.bstrVal = SysAllocString(windowName)。
if (varProp.bstrVal == NULL)
return NULL;
IUIAutomationElement* pRoot = NULL;
IUIAutomationElement* pFound = NULL;
IUIAutomationCondition* pCondition = NULL;
//獲得桌面元素。
HRESULT hr = g_pAutomation->GetRootElement(&pRoot)。
if (FAILED(hr) || pRoot == NULL)
goto清理。
///通過名稱獲得一個頂層元素,如 "程式經理"。
hr = g_pAutomation->CreatePropertyCondition(UIA_NamePropertyId, varProp, &pCondition)。
if (FAILED(hr))
goto cleanup;
pRoot->FindFirst(TreeScope_Children, pCondition, &pFound) 。
清理。
if (pRoot != NULL)
pRoot->Release()。
if (pCondition != NULL)
pCondition->Release()。
VariantClear(&varProp)。
return pFound;
}
但是應用程式在行HRESULT hr = g_pAutomation->GetRootElement(&pRoot);
出現錯誤:
File: C:Program Files (x86)Microsoft Visual Studio2019ProfessionalVCToolsMSVC14. 28.29333atlmfcincludeatlcomcli.h
行。204
運算式: p! =0
我想做的是,學習如何迭代給定視窗的名稱、描述等元素。
uj5u.com熱心網友回復:
你必須創建g_pAutomation,它不能為空,例如像這樣:
CoCreateInstance(
__uuidof(CUIAutomation),
NULL,
CLSCTX_ALL,
_uuidof(IUIAutomation)。
(void**)&g_pAutomation)。)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/320294.html
標籤:
上一篇:使用VBA刮取實時資料
