DotNET 中的硬编码字体
我曾多次遇到用户未在其计算机上安装 Windows Core 字体之一的问题(例如 Courier New、Comic Sans MS、Arial)。每当我尝试构建其中一种字体时,它都会抛出错误,而不是退回到相关的或实际上任何其他字体。
如果我希望它是 Courier 字体,但我并不特别喜欢它,构建字体的最佳方法是什么?
I've run into problems on numerous occasions of users not having one of the Windows Core fonts installed on their machine (Courier New, Comic Sans MS, Arial for example). Whenever I try to construct one of these it throws an error rather than falling back onto a related, or really, any other font.
What is the best way to construct a Font if I'd like it to be Courier, but I'm not particularly wedded to it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将字体嵌入为资源并使用
unsafe
关键字(并且必须使用“unsafe”进行编译)来获取字体...以下是我用来将用户控件设置为'OCR' 类型的字体声明字体的私有集合...使用命名约定将字体 'OCR' 作为资源嵌入,其中 'foo' 是命名空间,因此资源名称将为 'foo.ocraext.ttf '...查看此处的“InitOCRFont”,它加载字体并将其添加到集合中:
在这里,我在用户控件中设置控件,通过迭代集合将字体设置为“OCR”字体,请参阅' InitializeCtlFont',字体大小为 10,并且是粗体“字体”:
当用户控件被释放时,必须释放字体占用的资源,如
Dispose
方法中所示:希望这有帮助,
此致,
汤姆.
You can embed the fonts as resources and use the
unsafe
keyword (and must be compiled with 'unsafe') to obtain the fonts... Here's an example code that I used to set a user control to the font of type 'OCR'That declares a private collection of fonts...the font 'OCR' is embedded as a resource using the naming convention where 'foo' is a namespace, hence the resource name would be 'foo.ocraext.ttf'...Look at 'InitOCRFont' here which loads the font and adds it to the collection:
In here, I set the controls within the User control to set the font to that 'OCR' font by iterating through the collection, see 'InitializeCtlFont', font size is 10 and is a bold 'type-face':
When the user control gets disposed, it is imperative to free up the resources occupied by the font as shown here in the
Dispose
method:Hope this helps,
Best regards,
Tom.
好吧,您可以使用
FontFamily.GenericMonospace
获取 a Monospace 字体,而不是专门的 Courier:有 还有一些这样的通用字体系列。
在尝试您喜欢的字体并遇到异常后,您也可以将此作为最后的手段。
Well, you could use
FontFamily.GenericMonospace
to get a Monospace font, instead of specifically Courier:There are a few more such generic font families as well.
You can also use this as a last resort after trying your preferred font and running into an exception.
这是非常不寻常的。当您的程序请求目标计算机上不可用的字体时,Windows 字体映射器将始终提供替代字体。 XP 上的替代字体通常是 Microsoft Sans Serif,更高版本上的替代字体是 Segoe UI。这些都不应该引起问题。
话又说回来,如果机器太混乱以至于没有可用的核心字体,它可能根本就没有可用的 TrueType 字体。是的,那会轰炸你的程序。坦率地说,这不是您应该处理的事情,除非这是一个非常有价值的客户。否则,您的客户的支持团队可以轻松解决该问题。
This is very unusual. The Windows font mapper will always provide a substitute font when your program requests a font that isn't available on the target machine. That substitute font will usually be Microsoft Sans Serif on XP, Segoe UI on later versions. Neither of those should ever cause a problem.
Then again, if the machine is so messed up that it doesn't have the core fonts available, it might have no TrueType font available at all. Yes, that will bomb your program. Frankly, this isn't something you should have to deal with, unless this is a really valuable customer. It is otherwise trivially solved by your customer's support team.