1:将字体文件(*.ttf)添加到资源文件里,注意资源的名称
2:在Program.cs文件里声明
using System; using System.Collections.Generic; using System.Drawing.Text; using System.Windows.Forms; namespace SyncTime { static class Program { public static PrivateFontCollection privateFontCollection = new PrivateFontCollection(); /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { byte[] fontData = Properties.Resources.DS_Digital; //打开"允许不安全代码编译"开关,此句才不报错 unsafe { fixed (byte* pFontData = fontData) { privateFontCollection.AddMemoryFont((System.IntPtr)pFontData, fontData.Length); } } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new FrmMain()); } } }
3:使用
Font fn = new Font(Program.privateFontCollection.Families[0], 16, FontStyle.Regular); this.Font = fn;