共用方式為


如何從使用者介面自動化提供者返回屬性

本主題包含範例程式代碼,示範Microsoft使用者介面自動化提供者如何將UI元素的屬性傳回用戶端應用程式。

若要從提供者擷取屬性值,UI 自動化會呼叫提供者實作 IRawElementProviderSimple::GetPropertyValue 方法、傳遞要擷取的屬性標識碼,以及 VARIANT 結構的指標。 如果提供者支援指定的屬性,它會將 屬性的數據類型和值複製到 VARIANT 結構中。 如果不支援 屬性,提供者會將 VARIANT 結構的 vt 成員設定為 VT_EMPTY。

IFACEMETHODIMP Provider::GetPropertyValue(PROPERTYID propertyId, VARIANT* pRetVal)
{
    // The Name property is typically the same as the Caption property of the 
    // control window, if it has one. Here, the Name is overridden for the 
    // sake of illustration. 
    if (propertyId == UIA_NamePropertyId) 
    {
        pRetVal->vt = VT_BSTR;
        pRetVal->bstrVal = SysAllocString(L"Custom button");
    }
    
    else if (propertyId == UIA_ControlTypePropertyId) 
    {
        pRetVal->vt = VT_I4;
        pRetVal->lVal = UIA_ButtonControlTypeId; 
    }

    else if (propertyId == UIA_IsContentElementPropertyId) 
    {
        pRetVal->vt = VT_BOOL;
        pRetVal->lVal = TRUE; 
    }

    else if (propertyId == UIA_IsControlElementPropertyId) 
    {
        pRetVal->vt = VT_BOOL;
        pRetVal->lVal = TRUE; 
    }

    //
    // Return other properties as appropriate for the control type. 
    //

    else
    {
        pRetVal->vt = VT_EMPTY;
        // UI Automation will attempt to get the property from the  
        // provider for window that hosts the control.
    }
    return S_OK;
}

概念

UI 自動化屬性概觀

How-To UI 自動化提供者的主題