C#设置FontDialog只显示TrueType字体 [英] C# Setting FontDialog to only display TrueType fonts

查看:197
本文介绍了C#设置FontDialog只显示TrueType字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几乎每个论坛都会问这个问题,包括这里,但是我找不到任何可以接受的答案。我开始认为实际上没有解决方案,我只需要将代码封装在try / catch块中,并向用户道歉并要求他们选择另一种字体。



我想显示一个 FontDialog ,这样用户可以在 Chart 上更改字体,但是如果用户选择一个非TrueType字体,然后抛出异常。 GDI +只能处理TrueType字体。



如何过滤FontDialog中不能用于GDI +的字体?


没有什么可以做的,以赶上例外,它在一个回调函数中被引入到.NET框架中。重写班是一个选择,但不是一个愉快的。卸载troublemaker字体肯定是简单的解决方案。


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+?

解决方案

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.

这篇关于C#设置FontDialog只显示TrueType字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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