UI 자동화 공급자의 컨트롤 패턴 지원

이 항목에서는 Microsoft UI 자동화 공급자가 컨트롤에 대한 컨트롤 패턴을 구현하는 방법을 보여 줍니다. 컨트롤 패턴을 사용하면 클라이언트 애플리케이션이 컨트롤을 조작하고 해당 컨트롤에 대한 정보를 가져올 수 있습니다.

공급자는 다음 기본 단계에 따라 컨트롤 패턴을 구현합니다.

  1. 컨트롤 패턴을 지원하는 공급자 인터페이스를 구현합니다. 예를 들어 선택 컨트롤 패턴을 지원하기 위해 사용자 지정 목록 컨트롤에 대한 공급자는 ISelectionProvider 인터페이스를 구현합니다.
  2. UI 자동화 공급자의 IRawElementProviderSimple::GetPatternProvider 메서드를 호출할 때 컨트롤 패턴 공급자 인터페이스가 포함된 개체를 반환합니다.

다음 예제에서는 사용자 지정 단일 선택 목록 컨트롤에 대한 ISelectionProvider 인터페이스 구현을 보여 줍니다. 구현에는 IsSelectionRequired 및 CanSelectMultiple 속성에 대한 속성 검색 메서드와 선택한 목록 항목에 대한 공급자를 검색하는 메서드가 포함됩니다.

// Specifies whether the list control supports the selection of
// multiple items at the same time.
IFACEMETHODIMP ListProvider::get_CanSelectMultiple(BOOL *pRetVal)
{
    *pRetVal = FALSE;
    return S_OK;
} 

// Specifies whether the list must have an item selected at all times.
IFACEMETHODIMP ListProvider::get_IsSelectionRequired(BOOL *pRetVal)
{
   *pRetVal = TRUE;
   return S_OK;
}

// Retrieves the provider of the selected item in a custom list control. 
//
// m_pControl - pointer to an application-defined object for managing
//   the custom list control. 
//
// ListItemProvider - application-defined class that inherits the 
//   IRawElementProviderSimple interface.
//
// GetItemProviderByIndex - application-defined method that retrieves the 
//   IRawElementProviderSimple interface of the selected item.
IFACEMETHODIMP ListProvider::GetSelection(SAFEARRAY** pRetVal)
{
    // Create a safe array to store the IRawElementProviderSimple pointer.
    SAFEARRAY *psa = SafeArrayCreateVector(VT_UNKNOWN, 0, 1);

    // Retrieve the index of the selected list item. 
    int index = m_pControl->GetSelectedIndex(); 

    // Retrieve the IRawElementProviderSimple pointer of the selected list
    // item. ListItemProvider is an application-defined class that 
    // inherits the IRawElementProviderSimple interface. 
    ListItemProvider* pItem = GetItemProviderByIndex(index); 
    if (pItem != NULL)
    {
        LONG i = 0;
        SafeArrayPutElement(psa, &i, pItem);
    }
    *pRetVal = psa;
    return S_OK;
}

다음 예제에서는 ISelectionProvider를 구현하는 개체를 반환하는 IRawElementProviderSimple::GetPatternProvider의 구현을 보여 줍니다. 대부분의 목록 컨트롤은 다른 패턴도 지원하지만, 이 예제에서는 다른 모든 컨트롤 패턴 식별자에 대한 null 참조를 반환합니다.

// Retrieves an object that supports the control pattern provider interface 
// for the specified control pattern. 
IFACEMETHODIMP ListProvider::GetPatternProvider(PATTERNID patternId, IUnknown** pRetVal)
{
    *pRetVal = NULL;
    if (patternId == UIA_SelectionPatternId) 
    {
        // Return the object that implements ISelectionProvider. In this example, 
        // ISelectionProvider is implemented in the current object (this).
        *pRetVal = static_cast<IRawElementProviderSimple*>(this);
        AddRef();  
    }
    return S_OK;
}

개념

UI 자동화 컨트롤 패턴 개요

UI 자동화 공급자를 위한 방법 항목