2009-08-19 64 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