如何将 GDI+ Font 转换为 HFONT
要将 GDI+ 的 Font 转换为 HFONT,可以使用 CreateFontIndirect 函数。以下是一个示例代码:
#include <Windows.h>
HFONT ConvertFontToHFONT(const Gdiplus::Font* font)
{
LOGFONT logFont;
font->GetLogFontW(Gdiplus::Graphics::FromHWND(nullptr), &logFont);
return CreateFontIndirectW(&logFont);
}
int main()
{
Gdiplus::Font* gdiplusFont = new Gdiplus::Font(L'Arial', 12, Gdiplus::FontStyleRegular, Gdiplus::UnitPixel);
HFONT hFont = ConvertFontToHFONT(gdiplusFont);
// 使用 hFont 进行绘制或设置到窗口控件中
delete gdiplusFont;
DeleteObject(hFont);
return 0;
}
在上面的例子中,我们定义了一个 ConvertFontToHFONT 函数,该函数接受一个 GDI+ 的 Font 指针作为参数,并使用 GetLogFontW 函数从 Font 对象中获取 LOGFONT 结构。然后,我们使用 CreateFontIndirectW 函数根据 LOGFONT 结构创建 HFONT 对象。
在主函数中,我们创建一个 GDI+ 的 Font 对象,并将其转换为 HFONT 对象。你可以根据需要使用 hFont 进行绘制或将其设置到窗口控件中。最后,记得在不需要时释放 GDI+ 的 Font 对象和 HFONT 对象。
原文地址: http://www.cveoy.top/t/topic/4gh 著作权归作者所有。请勿转载和采集!