如何使用 .NET 提示“不是 truetype 字体”的 .TTF 字体在 .NET 程序中?

发布于 2024-09-15 05:39:00 字数 345 浏览 14 评论 0原文

我已经下载了一种字体 [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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栩栩如生 2024-09-22 05:39:00

如果这是您尝试使用的字体,那么也许您的本地实例已损坏?

当我尝试为 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.

心是晴朗的。 2024-09-22 05:39:00

看一下这段代码,该代码将嵌入字体作为资源加载并在适用的控件中使用,该示例显示了嵌入 OCR 字体的用法

    private PrivateFontCollection pfc = new PrivateFontCollection();
    private Font _fntOCRFont = null;
    private enum FontEnum{
       OCR = 0
    };

    private FontSize _fntSizeDefault = FontSize.Small;
    private float _fFontSize = 0.0F;

    private void InitOCRFont(){
        try{
            System.IO.Stream streamFont = this.GetType().Assembly.GetManifestResourceStream("ocraext.ttf");
            if (streamFont != null){
                byte[] fontData = new byte[streamFont.Length];
                streamFont.Read(fontData, 0, (int)streamFont.Length);
                streamFont.Close();
                unsafe{
                    fixed(byte *pFontData = fontData){
                    this.pfc.AddMemoryFont((System.IntPtr)pFontData, fontData.Length);
                }
            }
        }else{
            throw new Exception("Error! Could not read built-in Font.");
        }
    }catch(Exception eX){
        throw new Exception("Exception was: " + eX.Message);
    }
}

private void ConvertFontEnumToFloat(){
    switch(this._fntSizeDefault){
        case FontSize.Small :
            this._fFontSize = 8.0F;
            break;
        case FontSize.Medium :
            this._fFontSize = 10.0F;
            break;
        case FontSize.Large :
            this._fFontSize = 12.0F;
            break;
    }
}

典型的代码调用如下所示:

this.ConvertFontEnumToFloat();
this._fntOCRFont = new Font(this.pfc.Families[(int)FontEnum.OCR], this._fFontSize, System.Drawing.FontStyle.Bold);
if (this._fntOCRFont != null){
   // Do something here... perhaps assign it to a control
}

函数 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

    private PrivateFontCollection pfc = new PrivateFontCollection();
    private Font _fntOCRFont = null;
    private enum FontEnum{
       OCR = 0
    };

    private FontSize _fntSizeDefault = FontSize.Small;
    private float _fFontSize = 0.0F;

    private void InitOCRFont(){
        try{
            System.IO.Stream streamFont = this.GetType().Assembly.GetManifestResourceStream("ocraext.ttf");
            if (streamFont != null){
                byte[] fontData = new byte[streamFont.Length];
                streamFont.Read(fontData, 0, (int)streamFont.Length);
                streamFont.Close();
                unsafe{
                    fixed(byte *pFontData = fontData){
                    this.pfc.AddMemoryFont((System.IntPtr)pFontData, fontData.Length);
                }
            }
        }else{
            throw new Exception("Error! Could not read built-in Font.");
        }
    }catch(Exception eX){
        throw new Exception("Exception was: " + eX.Message);
    }
}

private void ConvertFontEnumToFloat(){
    switch(this._fntSizeDefault){
        case FontSize.Small :
            this._fFontSize = 8.0F;
            break;
        case FontSize.Medium :
            this._fFontSize = 10.0F;
            break;
        case FontSize.Large :
            this._fFontSize = 12.0F;
            break;
    }
}

Typical invocation of the code would be something like this:

this.ConvertFontEnumToFloat();
this._fntOCRFont = new Font(this.pfc.Families[(int)FontEnum.OCR], this._fFontSize, System.Drawing.FontStyle.Bold);
if (this._fntOCRFont != null){
   // Do something here... perhaps assign it to a control
}

The function InitOCRFont uses unsafe which means having the unsafe compiler option switched on, reads from the embedded resource and loads into a PrivateFontCollection. The function ConvertFontEnumToFloat 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 the PrivateFontCollection instance in the Dispose method of designated class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文