2009-08-19 69 views
9

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

如果我在标签上使用自定义字体,然后构建解决方案,然后在\ bin \ Release中.ZIP文件,最终用户将看到我使用的自定义应用程序的标签,而不管它们是否安装了该字体或不?

如果情况并非如此,那么在Labels.Text上使用自定义字体的正确方法是什么?

回答

20

嵌入字体作为资源(或只是将其包含在bin目录下),然后使用PrivateFontCollection加载的字体(见AddFontFileAddMemoryFont功能)。然后,您可以像使用机器上安装的那样正常使用字体。

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

Source

21

通过可能30-50帖子这个看后,我终于能够拿出实际有效的解决方案! 请依次按照以下步骤操作:

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

enter image description here

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

enter image description here

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(数据)。这是相当普遍的不打扰和离开字体加载的应用程序的生活。

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

干杯!

+0

** ** UseCompatibleTextRendering如果你有** AddFontMemResourceEx()**注册的字体是没有必要的。作为奖励,字体也可用于TextBox和其他控件。请参阅[此答案](http://stackoverflow.com/a/1956043/25312)和[MSDN文档](https://msdn.microsoft.com/en-us/library/dd183325(v = vs.85) )的.aspx)。 – SWB 2015-03-04 02:24:37

+0

可以将它添加到控件的属性中以选择嵌入字体还是普通字体?例如:'私人字体m_FontFace = UserControl.DefaultFont; public Font FontFace {get {return m_FontFace; } set {m_FontFace = value; }}' – 2016-12-08 22:51:03

+0

对于真实世界的使用有一些小建议,确保FreeCoTaskMem在Finally块中,所以如果出现异常,内存缓冲区将被释放。 – Rushyo 2017-02-22 06:03:58

2

添加要使用的字体。

enter image description here

`

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); 


    } 

enter image description here

+1

给出“System.Runtime.InteropServices.ExternalException:GDI +中的常规错误” – Fusseldieb 2017-10-19 20:17:01