【发布时间】:2023-03-19 17:52:01
【问题描述】:
在我使用 Windows API 构建的应用程序中,我目前正在将文本写入屏幕。我正在尝试使用CreateTextLayout 方法,但是使用该特定功能时,我遇到了无法解决的问题。当我刚刚运行我的应用程序时,我开始正常,并且刚刚退出,没有任何类型的错误警告。使用断点,我将问题追溯到该函数。这是我用来调用它的代码:
IDWriteFactory* writeFactory;
IDWriteTextFormat* writeTextFormat;
IDWriteTextLayout* writeTextLayout;
HRESULT App::CreateDeviceIndependentResources()
{
HRESULT hr;
// Create a Direct2D factory.
hr = D2D1CreateFactory(
D2D1_FACTORY_TYPE_SINGLE_THREADED,
&writeFactory
);
if (SUCCEEDED(hr))
{
hr = DWriteCreateFactory(
DWRITE_FACTORY_TYPE_SHARED,
__uuidof(writeFactory),
reinterpret_cast<IUnknown**>(&writeFactory)
);
}
if (writeFactory == NULL)
OutputDebugStringA("NULL\n");
if (SUCCEEDED(hr))
{
hr = writeFactory->CreateTextFormat(
L"Times New Roman",
NULL,
DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
14.0f,
L"EN-US",
&writeTextFormat
);
}
if (SUCCEEDED(hr))
{
// Create a DirectWrite text format object.
hr = writeFactory->CreateTextLayout(
L"Projects", // The string to be laid out and formatted.
8, // The length of the string.
writeTextFormat, // The text format to apply to the string (contains font information, etc).
10.0f, // The width of the layout box.
10.0f, // The height of the layout box.
&writeTextLayout // The IDWriteTextLayout interface pointer.
);
}
}
我觉得这很基本,因为我只是按照 Microsoft.com 上的教程进行操作;但是,有些地方显然是错误的。我的猜测是它可能与writeTextFormat 有关。它是一个IDWriteTextFormat 对象。此外,所有这些对象都在 CreateDeviceIndependentResources() 函数中初始化。我应该在其他地方初始化它们吗?
【问题讨论】:
-
你检查过
writeTextFormat的值吗?您首先需要使用IDWriteFactory::CreateTextFormat方法创建一个IDWriteTextFormat接口对象。如果还是不行,请提供a Minimal, Reproducible Example。 -
还要检查CreateTextLayout的返回值。
-
我刚刚检查过,
writeTextFactory为 NULL。
标签: c++ windows user-interface winapi