c - 在 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);
解决方案
推荐阅读
- javascript - 无法集成笔
- node.js - 从 aws sqs 获取所有消息
- java - 当我声明我的 CalenderView 时,Android Studio 崩溃
- sql - SQL 打印子查询的选定值
- r - 文档中代码块的内联引用
- bezier - 在 Photoshop 中循环路径点
- python-3.x - 在 Python 上将日期转换为数字
- javascript - 菜单中的图标不可点击 - 尝试使用正相关和 z-index
- android - 如何在 Flutter 中实现一个可以垂直和水平扩展并再次收缩的网格?
- java - 如何从 firebase 中检索值(一对多关系)?