Using Display Attributes

Text Services Framework (TSF) enables a text service to provide display attributes for text. This enables an application to display additional visual feedback. For example, a spelling checker text service can highlight a misspelled word with a red underline. The display attributes that can be provided are defined by the TF_DISPLAYATTRIBUTE structure and include text color, text background color, underline style, underline color, and underline weight.

When rendering text, an application should obtain the display attributes for the text drawn and use the attributes to modify how the text is drawn. Complete the following steps to obtain display attributes.

  1. Obtain a property object for GUID_PROP_ATTRIBUTE by calling ITfContext::GetProperty.
  2. Obtain the value of the GUID_PROP_ATTRIBUTE for the specified range by calling ITfReadOnlyProperty::GetValue. This supplies a TfGuidAtom value.
  3. Convert the TfGuidAtom value into a GUID by calling ITfCategoryMgr::GetGUID.
  4. Create an ITfDisplayAttributeInfo object for the display attribute by calling ITfDisplayAttributeMgr::GetDisplayAttributeInfo.
  5. Obtain the display attribute information by calling ITfDisplayAttributeInfo::GetAttributeInfo.

The following code example demonstrates a function that obtains the display attributes from a supplied context, range, and edit cookie.

HRESULT GetDispAttrFromRange(   ITfContext *pContext, 
                                ITfRange *pRange, 
                                TfEditCookie ec, 
                                TF_DISPLAYATTRIBUTE *pDispAttr)
{
    HRESULT     hr;
    
    //Create the category manager. 
    ITfCategoryMgr  *pCategoryMgr;
    hr = CoCreateInstance(  CLSID_TF_CategoryMgr,
                            NULL, 
                            CLSCTX_INPROC_SERVER, 
                            IID_ITfCategoryMgr, 
                            (LPVOID*)&pCategoryMgr);
    if(FAILED(hr))
    {
        return hr;
    }

    //Create the display attribute manager. 
    ITfDisplayAttributeMgr  *pDispMgr;
    hr = CoCreateInstance(  CLSID_TF_DisplayAttributeMgr,
                            NULL, 
                            CLSCTX_INPROC_SERVER, 
                            IID_ITfDisplayAttributeMgr, 
                            (LPVOID*)&pDispMgr);
    if(FAILED(hr))
    {
        pCategoryMgr->Release();
        return hr;
    }
    
    //Get the display attribute property. 
    ITfProperty *pProp;
    hr = pContext->GetProperty(GUID_PROP_ATTRIBUTE, &pProp);
    if(SUCCEEDED(hr))
    {
        VARIANT var;

        VariantInit(&var);
        hr = pProp->GetValue(ec, pRange, &var);
        if(S_OK == hr)  //Returns S_FALSE if the range is not completely covered by the property.  
        {
            if(VT_I4 == var.vt)
            {
                //The property is a guidatom. 
                GUID    guid;

                //Convert the guidatom into a GUID. 
                hr = pCategoryMgr->GetGUID((TfGuidAtom)var.lVal, &guid);
                if(SUCCEEDED(hr))
                {
                    ITfDisplayAttributeInfo *pDispInfo;

                    //Get the display attribute info object for this attribute. 
                    hr = pDispMgr->GetDisplayAttributeInfo(guid, &pDispInfo, NULL);
                    if(SUCCEEDED(hr))
                    {
                        //Get the display attribute info. 
                        hr = pDispInfo->GetAttributeInfo(pDispAttr);

                        pDispInfo->Release();
                    }
                }
            }
            else
            {
                //An error occurred; GUID_PROP_ATTRIBUTE must always be VT_I4. 
                hr = E_FAIL;
            }
            VariantClear(&var);
        }
    pProp->Release();
    }

    pCategoryMgr->Release();
    pDispMgr->Release();

    return hr;
}

TF_DISPLAYATTRIBUTE

ITfContext::GetProperty

ITfReadOnlyProperty::GetValue

TfGuidAtom

ITfCategoryMgr::GetGUID

ITfDisplayAttributeInfo

ITfDisplayAttributeMgr::GetDisplayAttributeInfo

ITfDisplayAttributeInfo::GetAttributeInfo