如何在 Direct2D 窗口中正确初始化 Directwrite IDWriteTextLayout



我正在尝试使用Directwrite将文本写入我的ID2D1HwndRenderTarget* renderTarget窗口。这很好用,文本出现在应有的位置。但我有一种感觉,我在Graphics::DrawMyText做错了什么。我想我也应该在Graphics::Initialisation中创建我的IDWriteTextLayout* textLayout,但如果我这样做,我就无法再更改文本const wchar_t* wszText了。至少,我在界面中没有找到任何IDWriteTextLayout帮助程序功能

那么,一直创建和发布我的IDWriteTextLayout是否正确,还是有没有办法像其他界面一样我只需要创建一次?

#include<dwrite.h>
class Graphics
{
IDWriteFactory* writeFactory;
IDWriteTextLayout* textLayout;
IDWriteTextFormat* textFormat; 
}
Graphics() // constructor
{
writeFactory = NULL;
textLayout = NULL;
textFormat = NULL; 
}
Graphics::~Graphics() // destructor
{
if (writeFactory) writeFactory->Release();
if (textLayout) textLayout->Release();
if (textFormat) textFormat->Release();
}
bool Graphics::Initialise(HWND windowsHandle)
{
res = writeFactory->CreateTextFormat(
L"Lucida Console",
NULL,
DWRITE_FONT_WEIGHT_REGULAR,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
10.0f,
L"en-us",
&textFormat
);
if (res != S_OK) return false;
return true;
}
void Graphics::DrawMyText(const wchar_t* wszText, float x, float y, float boxWidth,
float boxHeight, float r, float g, float b, float a)
{
writeFactory->CreateTextLayout(wszText, (UINT32)wcslen(wszText), textFormat,
boxWidth, boxHeight, &textLayout);
brush->SetColor(D2D1::ColorF(r, g, b, a));
renderTarget->DrawTextLayout(D2D1::Point2F(x, y), textLayout, brush);
textLayout->Release(); // don't forget this one to prevent memory leaks
}

DWrite 布局的文本确实是固定的,您的选择是创建(和发布(布局对象,或者使用 IDWriteTextAnalyzer(以及 IDWriteTextAnalysisSource/IDWriteTextAnalysisSink(的更难的路线。如果布局的文本是可变的,那就太好了,但 MS 根本没有做出这个选择。

相关内容

  • 没有找到相关文章

最新更新