2010-02-18 132 views
3

我想爲我的.net 3.0 Winforms應用程序使用非標準字體。如何將字體與我的.net winforms應用程序捆綁在一起?

該字體可能安裝在我的用戶計算機上,但在某些用戶的計算機上可能會丟失該字體。

如何將字體與我的程序一起發送?我需要安裝字體嗎?如果是這樣,缺乏管理員權限會成爲一個問題?

+0

你的程序如何「發貨」? Zip文件?安裝程序?都? – Oded 2010-02-18 11:41:35

+0

我們支持這兩種方法,具體取決於客戶的喜好。這就是說,我們可以很容易地在客戶端啓動時直接執行字體註冊代碼(當然,它不需要管理權限) – Brann 2010-02-18 12:35:42

回答

3

您必須使用安裝程序才能獲取在目標機器上註冊的字體。但也許你不必,GDI +支持private fonts

1

This page詳細解釋瞭如何將字體嵌入到winforms項目中。

+0

偉大的文章,謝謝 – docesam 2017-07-25 08:16:48

3

這是我寫的一篇博客文章,它展示了一種將字體作爲資源嵌入到應用程序中的方式(無需dll導入:)。

Embedding Fonts in your .Net Application

這裏是我創建所有的魔法發生的類。博客文章包含使用說明和示例。

using System.Drawing; 
using System.Drawing.Text; 
using System.Runtime.InteropServices; 
namespace EmbeddedFontsExample.Fonts 
{ 
    public class ResFonts 
    { 
     private static PrivateFontCollection sFonts; 
     static ResFonts() 
     { 
      sFonts = new PrivateFontCollection(); 
      // The order the fonts are added to the collection 
      // should be the same as the order they are added 
      // to the ResFontFamily enum. 
      AddFont(MyFonts.Consolas); 
     } 
     private static void AddFont(byte[] font) 
     { 
      var buffer = Marshal.AllocCoTaskMem(font.Length); 
      Marshal.Copy(font, 0, buffer, font.Length); 
      sFonts.AddMemoryFont(buffer, font.Length); 
     } 
     public static Font Create(
      ResFontFamily family, 
      float emSize, 
      FontStyle style = FontStyle.Regular, 
      GraphicsUnit unit = GraphicsUnit.Pixel) 
     { 
      var fam = sFonts.Families[(int)family]; 
      return new Font(fam, emSize, style, unit); 
     } 
    } 
    public enum ResFontFamily 
    { 
     /// <summary>Consolas</summary> 
     Consolas = 0 
    } 
} 
+0

假設您已經將字體添加爲通常的資源,唯一的我會改變的是'AddFont(MyFonts.Consolas)'應該是'AddFont(Resources.Consolas)' – nathanchere 2013-09-14 05:05:30

+5

鏈接已經死了。 – Michael 2013-11-14 01:11:44

+0

鏈接已死! – docesam 2017-07-25 08:04:15

相關問題