Share via


Renderizar usando Direct2D

Direct2D fornece métodos para renderizar um texto com formatação descrita apenas por um IDWriteTextFormat ou um IDWriteTextLayout em uma superfície de Direct2D.

Renderizando texto descrito por IDWriteTextFormat

Para renderizar uma cadeia de caracteres usando um objeto IDWriteTextFormat para descrever a formatação de toda a cadeia de caracteres, use o método ID2D1RenderTarget::D rawText fornecido pelo Direct2D.

  1. Defina a área para o layout de texto recuperando as dimensões da área de renderização e crie um retângulo Direct2D que tenha as mesmas dimensões.

    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 o método ID2D1RenderTarget::D rawText e o objeto IDWriteTextFormat para renderizar o texto na tela. O método ID2D1RenderTarget::D rawText usa os seguintes parâmetros:

    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.
        );
    
    

Renderizando um objeto de layout IDWriteText

Para desenhar o texto com as configurações de layout de texto especificadas pelo objeto IDWriteTextLayout , altere o código no método MultiformattedText::D rawText para usar IDWriteTextLayout::D rawTextLayout.

  1. Delcare uma variável D2D1_POINT_2F e defina-a como o ponto superior esquerdo da janela.

    D2D1_POINT_2F origin = D2D1::Point2F(
        static_cast<FLOAT>(rc.left / dpiScaleX_),
        static_cast<FLOAT>(rc.top / dpiScaleY_)
        );
    
    
  2. Desenhe o texto para a tela chamando o método ID2D1RenderTarget::D rawTextLayout do destino de renderização Direct2D e passando o ponteiro IDWriteTextLayout.

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