vb.net 如何从 VB 和 C# 中的文件加载字体?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19141706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:19:14  来源:igfitidea点击:

How to load a font from a file in VB and C#?

c#vb.netvisual-studio-2010fonts

提问by Guillaume Chevalier

I want to load a font in my VB Form program from a file.

我想从文件中加载我的 VB 表单程序中的字体。

e.g.: My font is in the same folder of my .exe program, and I want it to stay an external resource (that we can replace so that it change the entire program's font).

例如:我的字体在我的 .exe 程序的同一个文件夹中,我希望它保留一个外部资源(我们可以替换它以便它改变整个程序的字体)。

回答by Saksham

Here's and example of how you can do it in C#:

以下是如何在 C# 中执行此操作的示例:

System.Drawing.Text.PrivateFontCollection privateFonts = new System.Drawing.Text.PrivateFontCollection();
privateFonts.AddFontFile("C:\Documents and Settings\somefont.ttf");
System.Drawing.Font font = new Font(privateFonts.Families[0], 12);
label1.Font = font;

Or, in VB.NET:

或者,在 VB.NET 中:

Dim privateFonts As New System.Drawing.Text.PrivateFontCollection()
privateFonts.AddFontFile("C:\Documents and Settings\somefont.ttf")
Dim font As New System.Drawing.Font(privateFonts.Families(0), 12)
label1.Font = font

Look THISfor more details.

看看这个了解更多细节。