Render Using Direct2D
Collapse the table of content
Expand the table of content

Render Using Direct2D

Direct2D provides methods for rendering either text with formatting described by only an IDWriteTextFormat or an IDWriteTextLayout to a Direct2D surface.

Rendering Text Described by IDWriteTextFormat

To render a string using an IDWriteTextFormat object to describe the formatting for the entire string, use the ID2D1RenderTarget::DrawText method provided by Direct2D.

  1. Define the area for the text layout by retrieving the dimensions of the rendering area, and create a Direct2D rectangle that has the same dimensions.
    D2D1_RECT_F layoutRect = D2D1::RectF(
        static_cast<FLOAT>(rc.left) / dpiScaleX_,
        static_cast<FLOAT>( / dpiScaleY_,
        static_cast<FLOAT>(rc.right - rc.left) / dpiScaleX_,
        static_cast<FLOAT>(rc.bottom - / dpiScaleY_
  2. Use the ID2D1RenderTarget::DrawText method and the IDWriteTextFormat object to render text to the screen. The ID2D1RenderTarget::DrawText method takes the following parameters:
        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.

Rendering a IDWriteText Layout Object

To draw the text with the text layout settings specified by the IDWriteTextLayout object, change the code in the MultiformattedText::DrawText method to use IDWriteTextLayout::DrawTextLayout.

  1. Delcare a D2D1_POINT_2F variable and set it to the upper-left point of the window.
    D2D1_POINT_2F origin = D2D1::Point2F(
        static_cast<FLOAT>(rc.left / dpiScaleX_),
        static_cast<FLOAT>( / dpiScaleY_)
  2. Draw the text to the screen by calling the ID2D1RenderTarget::DrawTextLayout method of the Direct2D render target and passing the IDWriteTextLayout pointer.



Community Additions

© 2016 Microsoft