Cara Menggunakan Penembolokan
Topik ini berisi contoh kode yang menunjukkan cara menggunakan kemampuan penembolokan (atau pengambilan massal) pohon Otomatisasi UI Microsoft. Ini membahas topik-topik berikut:
- Mengambil Properti dan Pola Kontrol ke dalam Cache
- Mengambil Properti dan Pola Kontrol dari Cache
- Topik terkait
Mengambil Properti dan Pola Kontrol ke dalam Cache
Contoh kode berikut menunjukkan fungsi yang mengambil item dari kontrol daftar dan menyimpan pola kontrol SelectionItem dan properti Nama untuk setiap item. Secara default, item daftar dikembalikan dengan referensi lengkap, sehingga semua properti saat ini masih tersedia.
// Given a list element, caches a control pattern and a property for
// all the list items.
IUIAutomationElementArray* FindAndCacheListItems(IUIAutomationElement* pList)
{
if (pList == NULL)
return NULL;
IUIAutomationCondition* pCondition = NULL;
IUIAutomationCacheRequest* pCacheRequest = NULL;
IUIAutomationElementArray* pFound = NULL;
HRESULT hr = g_pAutomation->CreateTrueCondition(&pCondition);
if (FAILED(hr))
goto cleanup;
hr = g_pAutomation->CreateCacheRequest(&pCacheRequest);
if (FAILED(hr))
goto cleanup;
hr = pCacheRequest->AddPattern(UIA_SelectionItemPatternId);
if (FAILED(hr))
goto cleanup;
hr = pCacheRequest->AddProperty(UIA_NamePropertyId);
if (FAILED(hr))
goto cleanup;
pList->FindAllBuildCache(TreeScope_Children, pCondition, pCacheRequest, &pFound);
cleanup:
if (pCondition != NULL)
pCondition->Release();
if (pCacheRequest != NULL)
pCacheRequest->Release();
return pFound;
}
Mengambil Properti dan Pola Kontrol dari Cache
Kode berikut menunjukkan cara mengambil properti dan mengontrol pola dari cache. Ini mengambil pola kontrol SelectionItem dan properti Nama untuk item daftar.
// Demonstrates retrieval of cached properties from a list item
// obtained in FindAndCacheListItems.
HRESULT GetCachedListItem(IUIAutomationElement* pItem)
{
if (pItem == NULL)
{
return E_INVALIDARG;
}
IUIAutomationSelectionItemPattern* pSelectionItemPattern;
HRESULT hr = pItem->GetCachedPatternAs(UIA_SelectionItemPatternId,
IID_IUIAutomationSelectionItemPattern, (void**)&pSelectionItemPattern);
if (pSelectionItemPattern != NULL)
{
// ... To do: Do something with the pattern.
pSelectionItemPattern->Release();
}
// Retrieve a cached property.
BSTR bstrName;
hr = pItem->get_CachedName(&bstrName);
if (SUCCEEDED(hr))
{
// ... To do: Do something with the property.
// Clean up when done with name.
SysFreeString(bstrName);
bstrName = NULL;
}
BOOL isControl;
// The following returns E_INVALIDARG because the property was not cached.
// hr = pItem->get_CachedIsControlElement(&isControl);
// The following is valid because we have a full reference to the object, therefore
// we can get current properties. If the cache request had been made with
// AutomationElementMode_None, no current properties would be available from
// this IUIAutomationElement.
hr = pItem->get_CurrentIsControlElement(&isControl);
return hr;
}
Topik terkait