如何使用 .NET 提示“不是 truetype 字体”的 .TTF 字体在 .NET 程序中?
我已经下载了一种字体 [Betsy Flanagan][1],我想在我的程序中使用它来显示屏幕键盘快捷键及其在各种程序中的含义。
但是,在 Visual Studio 2010 中为标签选择字体时,我收到一条错误消息:“仅支持 TrueType 字体。这不是 TrueType 字体。”
有什么方法可以让我在 .NET 程序中显示带有此字体的文本吗?由于这是一种专门的类似吐司的形式,只有一个标签需要具有这种特定的字体,因此我并不关心这样做是否是一种黑客行为(例如 P/Invoke 或类似的)。
注意< /strong>:这是一个 .NET 4.0 Winforms 应用程序。
I have downloaded a font, [Betsy Flanagan][1], that I'd like to use in my program that shows on-screen keyboard shortcuts and their meaning in various programs.
However, when selecting the font in Visual Studio 2010 for a label, I get an error message that says "Only TrueType fonts are supported. This is not a TrueType font."
Is there any way for me to display text with this font in a .NET program? Since this is a specialized toast-like form, with just one label that needs to have this particular font, I don't really care if it is a hack to do it (like P/Invoke or similar.)
Note: This is a .NET 4.0 Winforms application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果这是您尝试使用的字体,那么也许您的本地实例已损坏?
当我尝试为 Betsy 设置标签和其他 winforms 控件时,VS2010 表现良好。下载我链接的那个,看看是否有效。我的看法是,如果您安装了有效的 TTF,VS 不会有任何异常。
If this is the font you're trying to use, then maybe your local instance is corrupt?
VS2010 was well behaved when I tried to set labels and other winforms controls to Betsy. Download the one I linked and see if that works. My take is that if you've got a valid TTF installed, VS isn't going to take exception.
看一下这段代码,该代码将嵌入字体作为资源加载并在适用的控件中使用,该示例显示了嵌入 OCR 字体的用法
典型的代码调用如下所示:
函数
InitOCRFont 使用 unsafe,这意味着打开
unsafe
编译器选项,从嵌入资源中读取并加载到PrivateFontCollection
中。函数ConvertFontEnumToFloat
使用硬编码浮点值来指示基于字体枚举的大小。完成代码后,请务必在指定类的Dispose
方法中处置PrivateFontCollection
实例。Have a look at this code that loads an embedded font as a resource and used in controls where applicable, the sample shows the usage of embedding an OCR font
Typical invocation of the code would be something like this:
The function
InitOCRFont
uses unsafe which means having theunsafe
compiler option switched on, reads from the embedded resource and loads into aPrivateFontCollection
. The functionConvertFontEnumToFloat
uses a hardcoded float value to indicate the size based on the enum of the font. When finished with the code, be sure to dispose of thePrivateFontCollection
instance in theDispose
method of designated class.