建立 CUIAutomation 物件

本節說明如何藉由具現化實作IUIAutomation的物件,開始撰寫 Microsoft 消費者介面自動化 用戶端應用程式。

本主題包含下列各節。

CUIAutomation 物件

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

  • 訂閱事件。
  • 建立條件。 條件是用來縮小搜尋消費者介面自動化專案範圍的物件。
  • 直接從桌面取得消費者介面自動化元素, (根項目) ,或從螢幕座標或視窗控點取得。
  • 建立樹狀結構逐步解說工具物件,可用來巡覽消費者介面自動化專案的階層。
  • 轉換資料類型。

建立物件

若要在應用程式中開始使用消費者介面自動化,請採取下列步驟:

  • 在您的專案標頭中包含 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 自動化事件概觀

取得 UI 自動化項目