Share via


Utilisation des attributs d’affichage

Text Services Framework (TSF) permet à un service de texte de fournir des attributs d’affichage pour le texte. Cela permet à une application d’afficher des commentaires visuels supplémentaires. Par exemple, un service de texte de vérificateur d’orthographe peut mettre en surbrillance un mot mal orthographié avec un soulignement rouge. Les attributs d’affichage qui peuvent être fournis sont définis par la structure TF_DISPLAYATTRIBUTE et incluent la couleur du texte, la couleur d’arrière-plan du texte, le style de soulignement, la couleur de soulignement et le poids du soulignement.

Lors du rendu du texte, une application doit obtenir les attributs d’affichage du texte dessiné et utiliser les attributs pour modifier la façon dont le texte est dessiné. Effectuez les étapes suivantes pour obtenir des attributs d’affichage.

  1. Obtenez un objet de propriété pour GUID_PROP_ATTRIBUTE en appelant ITfContext::GetProperty.
  2. Obtenez la valeur du GUID_PROP_ATTRIBUTE pour la plage spécifiée en appelant ITfReadOnlyProperty::GetValue. Cela fournit une valeur TfGuidAtom .
  3. Convertissez la valeur TfGuidAtom en GUID en appelant ITfCategoryMgr::GetGUID.
  4. Créez un objet ITfDisplayAttributeInfo pour l’attribut display en appelant ITfDisplayAttributeMgr::GetDisplayAttributeInfo.
  5. Obtenez les informations d’attribut d’affichage en appelant ITfDisplayAttributeInfo::GetAttributeInfo.

L’exemple de code suivant illustre une fonction qui obtient les attributs d’affichage à partir d’un cookie de contexte, de plage et de modification fourni.

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