Representación mediante Direct2D

Direct2D proporciona métodos para representar texto con formato descrito solo por un IDWriteTextFormat o un IDWriteTextLayout en una superficie de Direct2D.

Representación de texto descrito por IDWriteTextFormat

Para representar una cadena mediante un objeto IDWriteTextFormat para describir el formato de toda la cadena, use el método ID2D1RenderTarget::D rawText proporcionado por Direct2D.

  1. Defina el área para el diseño de texto recuperando las dimensiones del área de representación y cree un rectángulo Direct2D que tenga las mismas dimensiones.

    D2D1_RECT_F layoutRect = D2D1::RectF(
        static_cast<FLOAT>(rc.left) / dpiScaleX_,
        static_cast<FLOAT>(rc.top) / dpiScaleY_,
        static_cast<FLOAT>(rc.right - rc.left) / dpiScaleX_,
        static_cast<FLOAT>(rc.bottom - rc.top) / dpiScaleY_
        );
    
    
  2. Use el método ID2D1RenderTarget::D rawText y el objeto IDWriteTextFormat para representar texto en la pantalla. El método ID2D1RenderTarget::D rawText toma los parámetros siguientes:

    pRT_->DrawText(
        wszText_,        // The string to render.
        cTextLength_,    // The string's length.
        pTextFormat_,    // The text format.
        layoutRect,       // The region of the window where the text will be rendered.
        pBlackBrush_     // The brush used to draw the text.
        );
    
    

Representación de un objeto de diseño IDWriteText

Para dibujar el texto con la configuración de diseño de texto especificada por el objeto IDWriteTextLayout , cambie el código en el método MultiformattedText::D rawText para usar IDWriteTextLayout::D rawTextLayout.

  1. Delcare un D2D1_POINT_2F variable y establézcalo en el punto superior izquierdo de la ventana.

    D2D1_POINT_2F origin = D2D1::Point2F(
        static_cast<FLOAT>(rc.left / dpiScaleX_),
        static_cast<FLOAT>(rc.top / dpiScaleY_)
        );
    
    
  2. Dibuja el texto en la pantalla llamando al método ID2D1RenderTarget::D rawTextLayout del destino de representación de Direct2D y pasando el puntero IDWriteTextLayout .

    pRT_->DrawTextLayout(
        origin,
        pTextLayout_,
        pBlackBrush_
        );