Come camminare sull'albero Automazione interfaccia utente
Questo argomento contiene codice di esempio che illustra come usare l'interfaccia IUIAutomationTreeWalker per esaminare e esaminare gli elementi nell'albero di Microsoft Automazione interfaccia utente. Vengono illustrati gli argomenti seguenti:
Scorrere i discendenti di un elemento
L'esempio di codice seguente è una funzione ricorsiva che illustra tutti i discendenti di un elemento dell'interfaccia utente e visualizza i relativi tipi di controllo in un elenco gerarchico.
// CAUTION: Do not pass in the root (desktop) element. Traversing the entire subtree
// of the desktop could take a very long time and even lead to a stack overflow.
void ListDescendants(IUIAutomationElement* pParent, int indent)
{
if (pParent == NULL)
return;
IUIAutomationTreeWalker* pControlWalker = NULL;
IUIAutomationElement* pNode = NULL;
g_pAutomation->get_ControlViewWalker(&pControlWalker);
if (pControlWalker == NULL)
goto cleanup;
pControlWalker->GetFirstChildElement(pParent, &pNode);
if (pNode == NULL)
goto cleanup;
while (pNode)
{
BSTR desc;
pNode->get_CurrentLocalizedControlType(&desc);
for (int x = 0; x <= indent; x++)
{
std::wcout << L" ";
}
std::wcout << desc << L"\n";
SysFreeString(desc);
ListDescendants(pNode, indent+1);
IUIAutomationElement* pNext;
pControlWalker->GetNextSiblingElement(pNode, &pNext);
pNode->Release();
pNode = pNext;
}
cleanup:
if (pControlWalker != NULL)
pControlWalker->Release();
if (pNode != NULL)
pNode->Release();
return;
}
Scorrere gli elementi predecessori
L'esempio di codice seguente è una funzione che illustra i predecessori di un elemento per identificare l'elemento padre. Ciò è utile quando è necessario identificare la finestra padre di un controllo. La funzione restituisce NULL per gli elementi di primo livello; ovvero gli elementi il cui padre è il desktop.
IUIAutomationElement* GetContainingWindow(IUIAutomationElement* pChild)
{
if (pChild == NULL)
return NULL;
IUIAutomationElement* pDesktop = NULL;
HRESULT hr = g_pAutomation->GetRootElement(&pDesktop);
if (FAILED(hr))
return NULL;
BOOL same;
g_pAutomation->CompareElements(pChild, pDesktop, &same);
if (same)
{
pDesktop->Release();
return NULL; // No parent, so return NULL.
}
IUIAutomationElement* pParent = NULL;
IUIAutomationElement* pNode = pChild;
// Create the treewalker.
IUIAutomationTreeWalker* pWalker = NULL;
g_pAutomation->get_ControlViewWalker(&pWalker);
if (pWalker == NULL)
goto cleanup;
// Walk up the tree.
while (TRUE)
{
hr = pWalker->GetParentElement(pNode, &pParent);
if (FAILED(hr) || pParent == NULL)
{
break;
}
g_pAutomation->CompareElements(pParent, pDesktop, &same);
if (same)
{
pDesktop->Release();
pParent->Release();
pWalker->Release();
// Reached desktop, so return next element below it.
return pNode;
}
if (pNode != pChild) // Do not release the in-param.
pNode->Release();
pNode = pParent;
}
cleanup:
if ((pNode != NULL) && (pNode != pChild))
pNode->Release();
if (pDesktop != NULL)
pDesktop->Release();
if (pWalker != NULL)
pWalker->Release();
if (pParent != NULL)
pParent->Release();
return NULL;
}
Argomenti correlati
-
Informazioni concettuali
Commenti e suggerimenti
https://aka.ms/ContentUserFeedback.
Presto disponibile: Nel corso del 2024 verranno gradualmente disattivati i problemi di GitHub come meccanismo di feedback per il contenuto e ciò verrà sostituito con un nuovo sistema di feedback. Per altre informazioni, vedereInvia e visualizza il feedback per