Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
604 views
in Technique[技术] by (71.8m points)

c# - How do I embed my own fonts in a WinForms app?

I want to embed fonts in my WinForms application so that I don't have to worry about them being installed on the machine. I've searched a bit on the MSDN site and found a few hints about using native Windows API calls, for instance Michael Caplan's (sp?) tutorial linked to by Scott Hanselman. Now, do I really have to go through all that trouble? Can't I just use the resource part of my app?

If not I'll probably go the installing route. In that case, can I do that programmatically? By just copying the font file to the WindowsFonts folder?

I am aware of licensing issues.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is what worked for me in VS 2013, without having to use an unsafe block.

Embed the resource

  1. Double-click Resources.resx, and in the toolbar for the designer click Add Resource/Add Existing File and select your .ttf file
  2. In your solution explorer, right-click your .ttf file (now in a Resources folder) and go to Properties. Set the Build Action to "Embedded Resource"

Load the font into memory

  1. Add using System.Drawing.Text; to your Form1.cs file

  2. Add code above and inside your default constructor to create the font in memory (without using "unsafe" as other examples have shown). Below is my entire Form1.cs:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Reflection;
    
    using System.Drawing.Text;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            [System.Runtime.InteropServices.DllImport("gdi32.dll")]
            private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont,
                IntPtr pdv, [System.Runtime.InteropServices.In] ref uint pcFonts);
    
            private PrivateFontCollection fonts = new PrivateFontCollection();
    
            Font myFont;
    
            public Form1()
            {
                InitializeComponent();
    
                byte[] fontData = Properties.Resources.MyFontName;
                IntPtr fontPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(fontData.Length);
                System.Runtime.InteropServices.Marshal.Copy(fontData, 0, fontPtr, fontData.Length);
                uint dummy = 0;
                fonts.AddMemoryFont(fontPtr, Properties.Resources.MyFontName.Length);
                AddFontMemResourceEx(fontPtr, (uint)Properties.Resources.MyFontName.Length, IntPtr.Zero, ref dummy);
                System.Runtime.InteropServices.Marshal.FreeCoTaskMem(fontPtr);
    
                myFont = new Font(fonts.Families[0], 16.0F);
            }
        }
    }
    

Use your font

  1. Add a label to your main form, and add a load event to set the font in Form1.cs:

    private void Form1_Load(object sender, EventArgs e)
    {
        label1.Font = myFont;
    }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...