在winform中安装和使用特定字体

2 c# installation fonts winforms

我想在winform应用程序中使用特定字体.此字体将从我的应用程序资源自动安装在用户个人计算机中.

我怎样才能做到这一点?

我使用了一些来自用户个人计算机的代码.如果我使用它font必须保留在用户个人计算机中,但我不希望这样.

         System.Drawing.Text.PrivateFontCollection fontCollection = new     System.Drawing.Text.PrivateFontCollection();
         fontCollection.AddFontFile(@"C:\Windows\Fonts\SUTOM__.TTF");
         FontFamily family = new FontFamily("SutonnyMJ", fontCollection);
         Font font3of9 = new Font(family, 15);
         label1.Font = font3of9;
Run Code Online (Sandbox Code Playgroud)

Yak*_*maz 7

1.使用安装项目安装

您可以从安装项目安装字体来执行此操作

文件系统>右键单击目标计算机上的文件系统>添加特殊文件夹字体文件夹

然后选择字体文件夹和 Add > File... 添加字体文件夹SS

2.以编程方式安装字体
为了实现此目的,您不得不进行一些外部调用.

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

然后从任何你想要的地方打电话

AddFontResource(@"C:\FontLocation\MyFont.TTF");
Run Code Online (Sandbox Code Playgroud)