Upravit

Sdílet prostřednictvím


Creating the CUIAutomation Object

This section describes how to get started writing a Microsoft UI Automation client application by instantiating an object that implements IUIAutomation.

This topic contains the following sections.

The CUIAutomation Object

The first step in using UI Automation is to create an object of the CUIAutomation class. This object exposes the IUIAutomation interface, which is the gateway to all the other objects and interfaces that are used by client applications. Among other things, IUIAutomation is used for the following tasks:

  • Subscribing to events.
  • Creating conditions. Conditions are objects used to narrow the scope of searches for UI Automation elements.
  • Obtaining UI Automation elements directly from the desktop (the root element), or from screen coordinates or window handles.
  • Creating tree walker objects that can be used to navigate the hierarchy of UI Automation elements.
  • Converting data types.

Creating the Object

To get started using UI Automation in your application, take the following steps:

  • Include UIAutomation.h in your project headers. UIAutomation.h brings in the other headers that define the API.
  • Declare a pointer to IUIAutomation.
  • Initialize the Component Object Model (COM).
  • Create an instance of CUIAutomation and retrieve the IUIAutomation interface in your pointer.

The following example function initializes COM, and then creates the CUIAutomation object, retrieving the IUIAutomation interface in the ppAutomation pointer.

#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));
}

Conceptual

UI Automation Events Overview

Obtaining UI Automation Elements