添加了AddFontResourceEx的字体在GDI +中不起作用 [英] Fonts added with AddFontResourceEx are not working in GDI+

查看:458
本文介绍了添加了AddFontResourceEx的字体在GDI +中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AddFontResourceEx为每种字体返回1,但字体在GDI +中不可访问(用VS2015和Windows 10测试,也在Windows 2008 R2服务器上测试)。

如果我用Windows Fonts Explorer手动安装字体一切正常。
似乎GDI +无法找到与AddFontResourceEx或AddFontResource添加的字体,任何想法我怎么能得到这个工作?

我使用CodeJock图书馆使用GDI +字体用于渲染XAML图形,所以我不能控制如何创建字体。
CPathW pw;

pw.Combine(theApp.m_strMyAppFolder,_T(Roboto-Light.ttf));
n = AddFontResourceEx(pw,FR_PRIVATE,nullptr);

pw.Combine(theApp.m_strMyAppFolder,_T(Roboto-Thin.ttf));
AddFontResourceEx(pw,FR_PRIVATE,nullptr);

pw.Combine(theApp.m_strMyAppFolder,_T(Roboto-Regular.ttf));
AddFontResourceEx(pw,FR_PRIVATE,nullptr);

pw.Combine(theApp.m_strMyAppFolder,_T(Ubuntu-R.ttf));
AddFontResourceEx(pw,FR_PRIVATE,nullptr);

pw.Combine(theApp.m_strMyAppFolder,_T(Ubuntu-L.ttf));
AddFontResourceEx(pw,FR_PRIVATE,nullptr);

:: SendNotifyMessage(HWND_BROADCAST,WM_FONTCHANGE,0,0);


解决方案

对于GDI +,您需要使用class PrivateFontCollection ,它解决了与FR_PRIVATE类似的AddFontResourceEx()类似的功能。


$ b PrivateFontCollection :: AddFontFile()所以我是只是评论。



该示例将Windows字体文件夹中的字体添加到私有字体集合中,这是毫无意义的,因为这些字体无论如何都是可用的。
您已经从AddFontResourceEx()示例的应用程序文件夹中加载字体,因此您可以对 PrivateFontCollection :: AddFontFile()使用相同的路径。



另外要注意的是,只要应用程序正在运行, PrivateFontCollection 对象应该停留在每个时间人们想用这些字体来绘制文字。一个好的地方是应用程序类中的一个成员变量。这应该是显而易见的,但是该示例使得它显示为每次都必须重新创建 PrivateFontCollection 。这将是一个主要的性能打击。
$ b

编辑:


我使用CodeJock库,它使用GDI +字体来渲染XAML
图形

在这种情况下,PrivateFontCollection 显然不是那么容易使用的,因为它不会使字体在您的应用程序中全局可用,而必须被用作的构造函数参数Font 。但你写道,你不能控制CodeJock创建字体的方式。

也许CodeJock直接在XAML中支持私有字体,参见这个例子用于C#,但也可以用于CodeJock。


AddFontResourceEx returns 1 for each font, but the fonts are not accesible in GDI+ (Tested with VS2015 and Windows 10, also on Windows 2008 R2 Server).

If I install the fonts manually with Windows Fonts Explorer everything works ok. It seems that GDI+ cannot find the fonts added with AddFontResourceEx or AddFontResource, any idea how can I get this working?

I use CodeJock Library which uses GDI+ fonts for rendering XAML graphics, so I cannot control how to fonts are created.

int n = 0;
CPathW pw;

pw.Combine(theApp.m_strMyAppFolder, _T("Roboto-Light.ttf"));
n =AddFontResourceEx(pw, FR_PRIVATE, nullptr);

pw.Combine(theApp.m_strMyAppFolder, _T("Roboto-Thin.ttf"));
AddFontResourceEx(pw, FR_PRIVATE, nullptr);

pw.Combine(theApp.m_strMyAppFolder, _T("Roboto-Regular.ttf"));
AddFontResourceEx(pw, FR_PRIVATE, nullptr);

pw.Combine(theApp.m_strMyAppFolder, _T("Ubuntu-R.ttf"));
AddFontResourceEx(pw, FR_PRIVATE, nullptr);

pw.Combine(theApp.m_strMyAppFolder, _T("Ubuntu-L.ttf"));
AddFontResourceEx(pw, FR_PRIVATE, nullptr);

::SendNotifyMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);

解决方案

For GDI+ you need to use class PrivateFontCollection which solves a similar purpose as AddFontResourceEx() with FR_PRIVATE.

There is already an example at the documentation for PrivateFontCollection::AddFontFile() so I'm just commenting on that.

The example adds fonts from the Windows Fonts folder to the private font collection which is pointless because these fonts are available anyway. You already load the fonts from the application folder in your AddFontResourceEx() example so you can use the same pathes for PrivateFontCollection::AddFontFile().

Another thing to note is that the PrivateFontCollection object should stay around as long as the application is running and should not be recreated everytime one wants to draw text with these fonts. A good place would be a member variable in the application class. This should be obvious but the example makes it appear as if you have to recreate the PrivateFontCollection everytime. This would be a major performance hit.

Edit:

I use CodeJock Library which uses GDI+ fonts for rendering XAML graphics

The PrivateFontCollection is propably not so easy to use in this context, as it doesn't make the fonts globally available in your application, but has to be used as constructor argument of Font. But you wrote that you don't control how the fonts are created by CodeJock.

Maybe CodeJock supports private fonts directly in the XAML, see this example which is for C# but maybe works for CodeJock too.

这篇关于添加了AddFontResourceEx的字体在GDI +中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆