Share via


使用 Direct2D 轉譯

Direct2D 提供方法,讓文字轉譯成隻有 IDWriteTextFormatIDWriteTextLayout 所描述的格式設定為 Direct2D 表面。

轉譯 IDWriteTextFormat 所描述的文字

若要使用IDWriteTextFormat物件轉譯字串來描述整個字串的格式,請使用Direct2D所提供的ID2D1RenderTarget::D rawText方法。

  1. 藉由擷取轉譯區域的維度,並建立具有相同維度的 Direct2D 矩形,以定義文字配置的區域。

    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. 使用 ID2D1RenderTarget::D rawText 方法和 IDWriteTextFormat 物件,將文字轉譯至畫面。 ID2D1RenderTarget::D rawText方法會採用下列參數:

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

轉譯 IDWriteText 版面設定物件

若要使用 IDWriteTextLayout 物件指定的文字版面配置設定繪製文字,請將 MultiformattedText::D rawText 方法中的程式碼變更為使用 IDWriteTextLayout::D rawTextLayout

  1. Delcare D2D1_POINT_2F 變數 ,並將它設定為視窗的左上方點。

    D2D1_POINT_2F origin = D2D1::Point2F(
        static_cast<FLOAT>(rc.left / dpiScaleX_),
        static_cast<FLOAT>(rc.top / dpiScaleY_)
        );
    
    
  2. 呼叫Direct2D轉譯目標的ID2D1RenderTarget::D rawTextLayout方法,並傳遞IDWriteTextLayout指標,將文字繪製到畫面。

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