首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在PrivateFontCollection中使用资源文件

在PrivateFontCollection中使用资源文件
EN

Stack Overflow用户
提问于 2018-11-05 23:51:03
回答 1查看 263关注 0票数 0

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

代码语言:javascript
复制
privateFontCollection.AddFontFile(L"Exo-Regular.ttf");

但是,我唯一能找到它的方法是通过本地文件路径访问它,如下所示:

代码语言:javascript
复制
privateFontCollection.AddFontFile(L"C:\\Users\\maybe\\Desktop\\Exo-Regular.ttf");
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-06 00:21:53

您不能用AddFontFile()方法来实现这一点;它所期望的路径字符串不能解析到嵌入在编译程序中的资源中。

相反,您必须使用AddMemoryFont()...and向它传递一个指针,指向通过资源感知API获取的资源数据。

2013年有个问题是有人在C#上这么做的:“来自资源的addFontFile”。我不知道您使用的是什么其他类库,但是如果您正在编写直接的Win32程序,那么获得一个指针和字体大小的结果如下所示:

代码语言:javascript
复制
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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53163955

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档