【发布时间】:2020-03-01 22:04:50
【问题描述】:
我有一个应用程序,我将一些字体导入到资源中。
现在,我想在应用程序中使用这些资源字体,而不是将它们安装到运行它的计算机上。
我想使用字体资源的方式是,我想通过发送WM_SETFONT 消息将标签的字体设置为资源字体。
一般来说,如果计算机上已经安装了字体,我会使用以下代码:
HDC hdc = GetDC(hwnd);
//here hwnd is the handle to the window.
const TCHAR* fontName = TEXT("/* THE FONT NAME */");
//this is where I'd enter the font name, but it only works when the font is already installed on the computer.
const long nFontSize = NFONTSIZE(7);
//this is where I set the font size.
LOGFONT logFont = {0};
logFont.lfHeight = -MulDiv(nFontSize, GetDeviceCaps(hdc, LOGPIXELSY), 72);
logFont.lfWeight = FW_SEMIBOLD;
_tcscpy_s(logFont.lfFaceName, fontName);
HFONT font = CreateFontIndirect(&logFont); //get the font handle
只要我得到HFONT 句柄,就可以毫不费力地将WM_SETFONT 消息发送到标签:
SendMessage(hwnd, WM_SETFONT, (WPARAM)font, static_cast<LPARAM>(MAKELONG(TRUE, 0)));
//here hwnd is the handle of the static label.
但是现在,我不想通过这种方式设置字体,因为这仅在计算机上已安装指定字体时才有效。我有 MY OWN 字体文件,其 .ttf 格式导入为资源。我想将标签的字体设置为 THIS .ttf 字体。
【问题讨论】:
标签: c++ winapi fonts resources hwnd