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
583 views
in Technique[技术] by (71.8m points)

c# - Installing and using a specific font in a winform

I want to use a specific font in my winform app. This font will be auto installed in user personal computer from my application resource.

How can I do that?

I used some code which is from user personal computer. If I use this the font must be remain previously in user personal computer but I don't want that.

         System.Drawing.Text.PrivateFontCollection fontCollection = new     System.Drawing.Text.PrivateFontCollection();
         fontCollection.AddFontFile(@"C:WindowsFontsSUTOM__.TTF");
         FontFamily family = new FontFamily("SutonnyMJ", fontCollection);
         Font font3of9 = new Font(family, 15);
         label1.Font = font3of9;
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

1. Install Using Setup Project

You can install font from your setup project to do this you have to

File System > Right Click under the File System on Target Machine > Add Special Folder Fonts Folder

Then Select Fonts Folder and Add > File... Add Font Folder SS

2. Install Font Programmatically
To achive this you have to do some external calls unfortunately.

[DllImport("gdi32.dll", EntryPoint="AddFontResourceW", SetLastError=true)]
public static extern int AddFontResource([In][MarshalAs(UnmanagedType.LPWStr)]
                                         string lpFileName);

Then call it from anywhere you want

AddFontResource(@"C:FontLocationMyFont.TTF");

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

...