首页 > 解决方案 > 在 win32 应用程序中使用不同的字体

问题描述

如何在我的 win32 应用程序中使用已安装在计算机上的字体?我想使用比 win32 中已经存在的字体更多的字体。这是我现在想使用 Liberation Mono 字体的代码:

HDC hdc = GetDC(window);

const TCHAR *font_name = TEXT("Liberation Mono");

LOGFONT lf = {0};
lf.lfHeight = -MulDiv(24, GetDeviceCaps(hdc, LOGPIXELSY), 72);
lf.lfWeight = FW_NORMAL;
wcscpy_s(lf.lfFaceName, _countof(lf.lfFaceName), font_name);

HFONT newFont = CreateFontIndirect(&lf);

SendMessage(window, WM_SETFONT, (WPARAM)newFont, TRUE);

char str[256] = "Test";

TextOutA(hdc, 0, 0, str, strlen(str));

DeleteObject(newFont);
ReleaseDC(window, hdc);

但是,这只是给了我standrad win32 字体。我也尝试了以下解决方案,替换SendMessage()SelectObject(),但它给了我相同的结果。

HDC hdc = GetDC(window);

const TCHAR *font_name = TEXT("Liberation Mono");

LOGFONT lf = {0};
lf.lfHeight = -MulDiv(24, GetDeviceCaps(hdc, LOGPIXELSY), 72);
lf.lfWeight = FW_NORMAL;
lf.lfOutPrecision = OUT_TT_ONLY_PRECIS;
wcscpy_s(lf.lfFaceName, _countof(lf.lfFaceName), font_name);

HFONT newFont = CreateFontIndirect(&lf);

SelectObject(hdc, &newFont);

char str[256] = "Test";

TextOutA(hdc, 0, 0, str, strlen(str));

DeleteObject(newFont);
ReleaseDC(window, hdc);

标签: cwinapi

解决方案


推荐阅读