将资源文件与 PrivateFontCollection 一起使用
Using Resource File with PrivateFontCollection
我在 C++ 程序中使用 PrivateFontCollection class,"Resource Files" 文件夹中有一个 .ttf 文件。我希望能够按照以下方式做一些事情:
privateFontCollection.AddFontFile(L"Exo-Regular.ttf");
但我似乎可以让它工作的唯一方法是通过本地文件路径访问它,如下所示:
privateFontCollection.AddFontFile(L"C:\Users\maybe\Desktop\Exo-Regular.ttf");
你不能用AddFontFile()
方法做到这一点;它期望的路径字符串无法解析为已编译程序中嵌入的资源。
相反,您将不得不使用 AddMemoryFont()
...并将指向您通过资源感知 API 获取的资源数据的指针传递给它。
有人在 2013 年用 C# 做这个问题:"addFontFile from resources"。我不知道您使用的其他 class 库是什么,但是如果您直接针对 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);
我在 C++ 程序中使用 PrivateFontCollection class,"Resource Files" 文件夹中有一个 .ttf 文件。我希望能够按照以下方式做一些事情:
privateFontCollection.AddFontFile(L"Exo-Regular.ttf");
但我似乎可以让它工作的唯一方法是通过本地文件路径访问它,如下所示:
privateFontCollection.AddFontFile(L"C:\Users\maybe\Desktop\Exo-Regular.ttf");
你不能用AddFontFile()
方法做到这一点;它期望的路径字符串无法解析为已编译程序中嵌入的资源。
相反,您将不得不使用 AddMemoryFont()
...并将指向您通过资源感知 API 获取的资源数据的指针传递给它。
有人在 2013 年用 C# 做这个问题:"addFontFile from resources"。我不知道您使用的其他 class 库是什么,但是如果您直接针对 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);