C# 设置 FontDialog 仅显示 TrueType 字体
几乎每个论坛都提出了这个问题,包括
我想显示一个 FontDialog
以便用户可以更改 Chart
上的字体,但是如果用户选择非 TrueType 字体,则会引发异常。 GDI+ 只能处理 TrueType 字体。
如何从 FontDialog 中过滤不能与 GDI+ 一起使用的字体?
This question has been asked in practically every forum, including here but there are no acceptable answers anywhere that I can find. I'm beginning to think that there is actually no solution and I just have to wrap my code in a try/catch block and apologise to the user and ask them to pick another font.
I want to show a FontDialog
so that a user can change the fonts on a Chart
, however if the user selects a non-TrueType font, then an exception is thrown. GDI+ can only handle TrueType fonts.
How can I filter the fonts from the FontDialog which cannot be used with GDI+?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
FontDialog 类已经执行此操作,它使用带有 CF_TTONLY 选项的 ChooseFont() API 调用。这会强制对话框仅显示标榜自己为 TrueType 字体的字体。这些链接表明周围有一些字体可以欺骗对话框,直到今天才听说过它。这使得这种情况非常罕见,但肯定并不意外,周围有很多垃圾字体,元数据很差。
您无法执行任何操作来捕获异常,它是在 .NET 框架中内置的回调函数中引发的。重写课程是一种选择,但不是一个令人愉快的选择。卸载麻烦制造者字体无疑是一个简单的解决方案。
The FontDialog class already does this, it uses the ChooseFont() API call with the CF_TTONLY option. Which forces the dialog to only display fonts that advertise themselves as TrueType fonts. The links suggests there are fonts around that fool the dialog, never heard of it before until today. Which makes it quite rare but certainly not unexpected, there are lots of junk fonts around with bad metadata.
There isn't anything you can do to catch the exception, it is raised in a callback function that's baked into the .NET framework. Rewriting the class is an option but not a pleasant one. Uninstalling the troublemaker font is certainly the easy solution.
除了 try/catch 阻止它之外,没有真正好的解决方法
No real nice way around this one except to try/catch block it
您可以使用此处提供的自定义 FontDialog 来克服此异常。它是用C#.Net开发的。
You can use the custom FontDialog available here to overcome this exception. It is developed in C#.Net.
我不确定它是否有效,但尝试设置 FontDialog.AllowSimulations 为 false。
I'm not sure whether it will work, but try to set FontDialog.AllowSimulations to false.