【问题标题】:Using Resource File with PrivateFontCollection将资源文件与 PrivateFontCollection 一起使用
【发布时间】:2018-11-05 23:51:03
【问题描述】:
我在 C++ 程序中使用 PrivateFontCollection 类,在“资源文件”文件夹中有一个 .ttf 文件。我希望能够按照以下方式做一些事情:
privateFontCollection.AddFontFile(L"Exo-Regular.ttf");
但我似乎可以让它工作的唯一方法是通过本地文件路径访问它,如下所示:
privateFontCollection.AddFontFile(L"C:\\Users\\maybe\\Desktop\\Exo-Regular.ttf");
【问题讨论】:
标签:
c++
windows
fonts
gdi+
【解决方案1】:
你不能用AddFontFile() 方法做到这一点;它所期望的路径字符串无法解析为嵌入在已编译程序中的资源。
相反,您将不得不使用 AddMemoryFont()...并传递一个指向您通过资源感知 API 获取的资源数据的指针。
2013 年有人在 C# 中执行此操作的问题:"addFontFile from resources"。我不知道您正在使用哪些其他类库,但如果您是直接使用 Win32 进行编程,获取字体的指针和大小将如下所示:
HMODULE module = NULL; // search current process, override if needed
HRSRC resource = FindResource(module, L"Exo-Regular.ttf", RT_RCDATA);
if (!resource) {...error handling... }
HGLOBAL handle = LoadResource(module, resource);
if (!handle) {...error handling... }
// "It is not necessary to unlock resources because the system
// automatically deletes them when the process that created
// them terminates."
//
void *memory = LockResource(handle);
DWORD length = SizeofResource(module, resource);
privateFontCollection.AddMemoryFont(memory, length);