共用方式為


建立 CUIAutomation 物件

本節說明如何藉由具現化實作 IUIAutomation的對象,開始撰寫Microsoft UI 自動化用戶端應用程式。

本主題包含下列各節。

CUIAutomation 物件

使用UI自動化的第一個步驟是建立 CUIAutomation 類別的物件。 這個物件會公開 IUIAutomation 介面,這是用戶端應用程式所使用的所有其他對象和介面的閘道。 除此之外,IUIAutomation 用於下列工作:

  • 訂閱事件。
  • 創造條件。 條件是用來縮小使用者介面自動化元素搜尋範圍的物件。
  • 直接從桌面取得UI自動化元素(根元素),或從螢幕座標或視窗控點取得。
  • 建立可用於巡覽 UI 自動化元素階層的樹狀結構遍歷物件。
  • 轉換數據類型。

建立物件

若要開始在應用程式中使用UI自動化,請執行下列步驟:

  • 在您的項目標頭中包含UIAutomation.h。 UIAutomation.h 包含其他定義 API 的標頭檔。
  • 宣告 IUIAutomation指標。
  • 初始化元件物件模型 (COM)。
  • 建立 CUIAutomation 實例,並在您的指標中擷取 IUIAutomation 介面。

下列範例函式會初始化 COM,然後建立 CUIAutomation 物件,擷取 ppAutomation 指標中的 IUIAutomation 介面。

#include <uiautomation.h>

// CoInitialize must be called before calling this function, and the  
// caller must release the returned pointer when finished with it.
// 
HRESULT InitializeUIAutomation(IUIAutomation **ppAutomation)
{
    return CoCreateInstance(CLSID_CUIAutomation, NULL,
        CLSCTX_INPROC_SERVER, IID_IUIAutomation, 
        reinterpret_cast<void**>(ppAutomation));
}

概念

UI 自動化事件概觀

取得使用者介面自動化元素