Direct2D DirectWrite绘制文字

时间:2021-06-20 06:53:23
绘制文本使用DirectWrite
为了简化 DirectWrite 的使用,RenderTarget有3个方法可以直接绘制文本:

写一个简单的例子:
别忘了头文件和lib
#include "wincodec.h"
dwrite.lib;
下面的是d2d必须的
d2d1.lib;
dxgi.lib;
dxguid.lib;

创建文本的流程如下:
类似于,还是需要一个Factory,只不过这次是:
  1. IDWriteFactory。然后初始化。
  2. 然后创建文本格式,包括字体,字号等。
  3. 设置绘制布局。
  4. 绘制。

pRenderTarget->BeginDraw();

	IDWriteFactory* pDWriteFactory = NULL;
IDWriteTextFormat* pTextFormat = NULL; // create dwrite factory
DWriteCreateFactory(
DWRITE_FACTORY_TYPE_SHARED,
__uuidof(IDWriteFactory),
reinterpret_cast<IUnknown**>(&pDWriteFactory)); //create text format
hr = pDWriteFactory->CreateTextFormat(
L"Arial",
NULL,
DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
20.0f * 96.0f/72.0f,
L"en-US",
&pTextFormat
); D2D1_RECT_F layoutRect = D2D1::RectF(1000.f, 1000.f, 200.f, 200.f); //draw text
pRenderTarget->DrawText(
L"Hello D2D",
wcslen(L"Hello D2D"),
pTextFormat,
layoutRect,
pBlackBrush
); pRenderTarget->EndDraw();

更多介绍MSDN