A complete example of drawing text using DirectWrite APIs
a complete example of using DirectWrite to draw text. the procedure is to create a customized brush, set text format, and then call the DrawTextLayout()
CComPtr<ID2D1SolidColorBrush> m_pSolidColorBrush;
CComPtr<IDWriteTextLayout>
m_pTextLayout_;
CComPtr<IDWriteFactory>
m_pDWriteFactory_;
CComPtr<IDWriteTextFormat>
m_pTextFormat_;
const wchar_t* m_wszText_;
UINT32 m_cTextLength_;
D2D1_POINT_2F m_origin;
// the following init are used for text drawing -
// create a customized brush
m_hWndTarget->CreateSolidColorBrush(
D2D1::ColorF(D2D1::ColorF::Purple, 1.0f),
&m_pSolidColorBrush
);
D2D1_SIZE_F rtSize =
m_hWndTarget->GetSize();
int width = static_cast<int>(rtSize.width);
int height = static_cast<int>(rtSize.height);
// draw text now
m_origin =
D2D1::Point2F(
static_cast<FLOAT>(rect.left / 4),
static_cast<FLOAT>(rect.top / 4)
);
m_wszText_ = L"Hello World using
DirectWrite!";
m_cTextLength_
= (UINT32)wcslen(m_wszText_);
DWriteCreateFactory(
DWRITE_FACTORY_TYPE_SHARED,
__uuidof(IDWriteFactory),
reinterpret_cast<IUnknown**>(&m_pDWriteFactory_)
);
//create a customized text format
m_pDWriteFactory_->CreateTextFormat(
L"Gabriola", //
Font family name.
NULL, // Font collection (NULL sets it to use the system font
collection).
DWRITE_FONT_WEIGHT_REGULAR,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
32.0f, //72.0f, // font
size
L"en-us",
&m_pTextFormat_
);
//create text layout
m_pDWriteFactory_->CreateTextLayout(
m_wszText_, // The
string to be laid out and formatted.
m_cTextLength_, // The
length of the string.
m_pTextFormat_, // The
text format to apply to the string (contains font information, etc).
width, // The
width of the layout box.
height, // The
height of the layout box.
&m_pTextLayout_ // The
IDWriteTextLayout interface pointer.
);
//jamesz: actual text drawing function here, can call this
once per frames
if (1)
{
m_hWndTarget->DrawTextLayout(
m_origin,
m_pTextLayout_,
m_pSolidColorBrush
);
}
0 Comments:
Post a Comment
<< Home