【问题标题】:Using custom fonts on a Label on Winforms在 Winforms 上的标签上使用自定义字体
【发布时间】:2010-11-20 19:14:14
【问题描述】:

我的 Winform 上有一个标签,我想使用一种名为 XCalibur 的自定义字体让它看起来更时髦。

如果我在标签上使用自定义字体,然后构建解决方案,然后将文件压缩到 \bin\Release 中,最终用户将看到带有我使用的自定义应用程序的标签,无论他们是否安装了该字体?

如果不是这样,在 Labels.Text 上使用自定义字体的正确方法是什么?

【问题讨论】:

    标签: c# fonts label


    【解决方案1】:

    在浏览了大约 30 到 50 篇关于此的帖子后,我终于能够想出一个真正有效的解决方案! 请按顺序执行:

    1.) 在您的应用程序资源中包含您的字体文件(在我的例子中是 ttf 文件)。为此,请双击“Resources.resx”文件。

    2.) 突出显示“添加资源”选项并单击向下箭头。选择“添加现有文件”选项。现在,搜索您的字体文件,选择它,然后单击确定。保存“Resources.resx”文件。

    3.) 创建一个函数(比如 InitCustomLabelFont() ),并在其中添加以下代码。

            //Create your private font collection object.
            PrivateFontCollection pfc = new PrivateFontCollection();
    
            //Select your font from the resources.
            //My font here is "Digireu.ttf"
            int fontLength = Properties.Resources.Digireu.Length;
    
            // create a buffer to read in to
            byte[] fontdata = Properties.Resources.Digireu;
    
            // create an unsafe memory block for the font data
            System.IntPtr data = Marshal.AllocCoTaskMem(fontLength);
    
            // copy the bytes to the unsafe memory block
            Marshal.Copy(fontdata, 0, data, fontLength);
    
            // pass the font to the font collection
            pfc.AddMemoryFont(data, fontLength);
    

    您的自定义字体现已添加到 PrivateFontCollection。

    4.) 接下来,将字体分配给您的标签,并在其中添加一些默认文本。

            //After that we can create font and assign font to label
            label1.Font = new Font(pfc.Families[0], label1.Font.Size);
            label1.Text = "My new font";
    

    5.) 转到您的表单布局并选择您的标签。右键单击它并选择“属性”。查找属性“UseCompatibleTextRendering”并将其设置为“True”。

    6.) 如有必要,您可以在确定字体无法再次使用后释放该字体。拨打PrivateFontCollection.Dispose() method,您也可以安全地拨打 Marshal.FreeCoTaskMem(data)。在应用程序的生命周期内不打扰并让字体加载是很常见的。

    7.) 运行您的应用程序。您现在应该看到已为给定标签设置了自定义字体。

    干杯!

    【讨论】:

    • 如果您使用 AddFontMemResourceEx() 注册字体,则不需要 UseCompatibleTextRendering。作为奖励,该字体还可以与 TextBoxes 和其他控件一起使用。请参阅 this answerMSDN documentation
    • 这可以添加到控件的属性中以选择其中一种嵌入字体或普通字体吗?例如:private Font m_FontFace = UserControl.DefaultFont; public Font FontFace { get { return m_FontFace; } set { m_FontFace = value; } }
    • 对实际使用的次要建议,确保 FreeCoTaskMem 位于 finally 块中,以便在出现异常时释放内存缓冲区。
    • 这适用于我开发的系统,但不适用于其他系统
    【解决方案2】:

    添加您要使用的字体。

    `

        PrivateFontCollection modernFont = new PrivateFontCollection();
    
        modernFont.AddFontFile("Font.otf");
    
        label.Font = new Font(modernFont.Families[0], 40);`
    

    我也做了一个方法。

     void UseCustomFont(string name, int size, Label label)
        {
    
            PrivateFontCollection modernFont = new PrivateFontCollection();
    
            modernFont.AddFontFile(name);
    
            label.Font = new Font(modernFont.Families[0], size);
    
    
        }
    

    【讨论】:

    • 给出“System.Runtime.InteropServices.ExternalException:GDI+ 中的一般错误”
    【解决方案3】:

    将字体作为资源嵌入(或仅将其包含在 bin 目录中),然后使用PrivateFontCollection 加载字体(参见AddFontFileAddMemoryFont 函数)。然后,您可以正常使用该字体,就像它安装在机器上一样。

    PrivateFontCollection 类允许 安装私有应用程序 现有字体的版本,没有 更换系统的要求 字体的版本。例如,GDI+ 可以创建一个私有版本的 Arial 字体除了 Arial 系统使用的字体。 也可以使用 PrivateFontCollection 安装不存在的字体 操作系统。

    Source

    【讨论】:

      【解决方案4】:

      我认为解决方案是将所需的字体嵌入到您的应用程序中。

      试试这个链接:

      http://www.emoreau.com/Entries/Articles/2007/10/Embedding-a-font-into-an-application.aspx

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-19
        • 2012-11-14
        • 1970-01-01
        • 2013-02-24
        • 2020-03-14
        相关资源
        最近更新 更多