将资源文件与PrivateFontCollection一起使用

时间:2018-11-05 23:51:04

标签: c++ windows fonts gdi+

我在c ++程序中使用PrivateFontCollection类,在“资源文件”文件夹中有一个.ttf文件。我希望能够做到这一点:

privateFontCollection.AddFontFile(L"Exo-Regular.ttf");

但是我似乎只能使用它的唯一方法是通过本地文件路径访问它,如下所示:

privateFontCollection.AddFontFile(L"C:\\Users\\maybe\\Desktop\\Exo-Regular.ttf");

1 个答案:

答案 0 :(得分:0)

您无法使用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);
相关问题