次の方法で共有


テキストを描画する方法

Direct2D でテキストを描画するには、1 つの形式のテキストに 対して ID2D1RenderTarget::D rawText メソッドを使用します。 または、複数の形式、高度な OpenType 機能、またはヒット テストに ID2D1RenderTarget::D rawTextLayout メソッドを使用します。 これらのメソッドは、DirectWrite API を使用して高品質のテキスト表示を提供します。

DrawText メソッド

1 つの形式のテキストを描画するには、 DrawText メソッドを使用します。 このメソッドを使用するには、まず IDWriteFactory を使用して IDWriteTextFormat インスタンスを作成します。

次のコードでは、 IDWriteTextFormat オブジェクトを作成し、 m_pTextFormat 変数に格納します。

// Create resources which are not bound
// to any device. Their lifetime effectively extends for the
// duration of the app. These resources include the Direct2D and
// DirectWrite factories,  and a DirectWrite Text Format object
// (used for identifying particular font characteristics).
//
HRESULT DemoApp::CreateDeviceIndependentResources()
{
    static const WCHAR msc_fontName[] = L"Verdana";
    static const FLOAT msc_fontSize = 50;
    HRESULT hr;

    // Create a Direct2D factory.
    hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &m_pD2DFactory);

    if (SUCCEEDED(hr))
    {        
        // Create a DirectWrite factory.
        hr = DWriteCreateFactory(
            DWRITE_FACTORY_TYPE_SHARED,
            __uuidof(m_pDWriteFactory),
            reinterpret_cast<IUnknown **>(&m_pDWriteFactory)
            );
    }
    if (SUCCEEDED(hr))
    {
        // Create a DirectWrite text format object.
        hr = m_pDWriteFactory->CreateTextFormat(
            msc_fontName,
            NULL,
            DWRITE_FONT_WEIGHT_NORMAL,
            DWRITE_FONT_STYLE_NORMAL,
            DWRITE_FONT_STRETCH_NORMAL,
            msc_fontSize,
            L"", //locale
            &m_pTextFormat
            );
    }
    if (SUCCEEDED(hr))
    {
        // Center the text horizontally and vertically.
        m_pTextFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER);
        
        m_pTextFormat->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER);
    }

    return hr;
}

IDWriteFactory オブジェクトと IDWriteTextFormat オブジェクトはデバイスに依存しないリソースであるため、フレームがレンダリングされるたびに再作成するのではなく、1 回だけ作成することでアプリケーションのパフォーマンスを向上させることができます。

テキスト形式オブジェクトを作成したら、レンダー ターゲットで使用できます。 次のコードでは、レンダー ターゲットの DrawText メソッド ( m_pRenderTarget 変数) を使用してテキストを描画します。

//  Called whenever the application needs to display the client
//  window. This method writes "Hello, World"
//
//  Note that this function will automatically discard device-specific
//  resources if the Direct3D device disappears during function
//  invocation, and will recreate the resources the next time it's
//  invoked.
//
HRESULT DemoApp::OnRender()
{
    HRESULT hr;

    hr = CreateDeviceResources();

    if (SUCCEEDED(hr))
    {
        static const WCHAR sc_helloWorld[] = L"Hello, World!";

        // Retrieve the size of the render target.
        D2D1_SIZE_F renderTargetSize = m_pRenderTarget->GetSize();

        m_pRenderTarget->BeginDraw();

        m_pRenderTarget->SetTransform(D2D1::Matrix3x2F::Identity());

        m_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White));

        m_pRenderTarget->DrawText(
            sc_helloWorld,
            ARRAYSIZE(sc_helloWorld) - 1,
            m_pTextFormat,
            D2D1::RectF(0, 0, renderTargetSize.width, renderTargetSize.height),
            m_pBlackBrush
            );

        hr = m_pRenderTarget->EndDraw();

        if (hr == D2DERR_RECREATE_TARGET)
        {
            hr = S_OK;
            DiscardDeviceResources();
        }
    }

    return hr;
}

DrawTextLayout メソッド

DrawTextLayout メソッドは、IDWriteTextLayout オブジェクトをレンダリングします。 このメソッドを使用して、テキスト ブロックに複数の形式を適用し (テキストの一部に下線を付けるなど)、高度な OpenType 機能を使用したり、ヒット テストのサポートを実行したりします。

DrawTextLayout メソッドは、同じテキストを繰り返し描画する場合のパフォーマンス上の利点も提供します。 IDWriteTextLayout オブジェクトは、作成時にテキストを測定してレイアウトします。 IDWriteTextLayout オブジェクトを 1 回だけ作成し、テキストを再描画する必要があるたびに再利用すると、システムがテキストを再び測定してレイアウトする必要がないため、パフォーマンスが向上します。

DrawTextLayout メソッドを使用する前に、IDWriteFactory を使用して IDWriteTextFormat オブジェクトと IDWriteTextLayout オブジェクトを作成する必要があります。 これらのオブジェクトが作成されたら、 DrawTextLayout メソッドを呼び出します。

詳細と例については、「 テキストの書式設定とレイアウト の概要」を参照してください。

Drawtext

DrawTextLayout

IDWriteTextFormat

IDWriteTextLayout

テキストの書式設定とレイアウト