I have a VB.NET Windows Forms project that at one point paints text directly to onto the form at runtime. Before I paint with the font though, I want to make sure that the font and font-size exists on the user's machine. If they don't, I'll try a few other similar fonts, eventually defaulting with Arial or something.
我有一个VB.NET Windows窗体项目,在运行时一次将文本直接绘制到窗体上。在我使用字体绘制之前,我想确保用户机器上存在字体和字体大小。如果他们不这样做,我会尝试一些其他类似的字体,最终默认使用Arial或其他东西。
What's the best way to test and validate a font on a user's computer?
在用户计算机上测试和验证字体的最佳方法是什么?
4 个解决方案
#1
8
From an MSDN article titled "How To: Enumerate Installed Fonts", I found this code:
从一篇名为“如何:枚举已安装的字体”的MSDN文章中,我找到了以下代码:
InstalledFontCollection installedFontCollection = new InstalledFontCollection();
// Get the array of FontFamily objects.
FontFamily[] fontFamilies = installedFontCollection.Families;
#2
2
Here is one solution, in c#:
这是c#中的一个解决方案:
public partial class Form1 : Form
{
public Form1()
{
SetFontFinal();
InitializeComponent();
}
/// <summary>
/// This method attempts to set the font in the form to Cambria, which
/// will only work in some scenarios. If Cambria is not available, it will
/// fall back to Times New Roman, so the font is good on almost all systems.
/// </summary>
private void SetFontFinal()
{
string fontName = "Cambria";
Font testFont = new Font(fontName, 16.0f, FontStyle.Regular,
GraphicsUnit.Pixel);
if (testFont.Name == fontName)
{
// The font exists, so use it.
this.Font = testFont;
}
else
{
// The font we tested doesn't exist, so fallback to Times.
this.Font = new Font("Times New Roman", 16.0f,
FontStyle.Regular, GraphicsUnit.Pixel);
}
}
}
And here is one method in VB:
这是VB中的一种方法:
Public Function FontExists(FontName As String) As Boolean
Dim oFont As New StdFont
Dim bAns As Boolean
oFont.Name = FontName
bAns = StrComp(FontName, oFont.Name, vbTextCompare) = 0
FontExists = bAns
End Function
#3
1
See also this same question that results in this code:
另请参阅导致此代码的相同问题:
private bool IsFontInstalled(string fontName) {
using (var testFont = new Font(fontName, 8)) {
return 0 == string.Compare(
fontName,
testFont.Name,
StringComparison.InvariantCultureIgnoreCase);
}
}
#4
0
Arial Bold Italic is unlikely to be a font. It's a subclass of the Arial family.
Arial Bold Italic不太可能是一种字体。它是Arial家族的一个子类。
Try keeping it simple and test for 'Arial'.
尽量保持简单并测试'Arial'。
#1
8
From an MSDN article titled "How To: Enumerate Installed Fonts", I found this code:
从一篇名为“如何:枚举已安装的字体”的MSDN文章中,我找到了以下代码:
InstalledFontCollection installedFontCollection = new InstalledFontCollection();
// Get the array of FontFamily objects.
FontFamily[] fontFamilies = installedFontCollection.Families;
#2
2
Here is one solution, in c#:
这是c#中的一个解决方案:
public partial class Form1 : Form
{
public Form1()
{
SetFontFinal();
InitializeComponent();
}
/// <summary>
/// This method attempts to set the font in the form to Cambria, which
/// will only work in some scenarios. If Cambria is not available, it will
/// fall back to Times New Roman, so the font is good on almost all systems.
/// </summary>
private void SetFontFinal()
{
string fontName = "Cambria";
Font testFont = new Font(fontName, 16.0f, FontStyle.Regular,
GraphicsUnit.Pixel);
if (testFont.Name == fontName)
{
// The font exists, so use it.
this.Font = testFont;
}
else
{
// The font we tested doesn't exist, so fallback to Times.
this.Font = new Font("Times New Roman", 16.0f,
FontStyle.Regular, GraphicsUnit.Pixel);
}
}
}
And here is one method in VB:
这是VB中的一种方法:
Public Function FontExists(FontName As String) As Boolean
Dim oFont As New StdFont
Dim bAns As Boolean
oFont.Name = FontName
bAns = StrComp(FontName, oFont.Name, vbTextCompare) = 0
FontExists = bAns
End Function
#3
1
See also this same question that results in this code:
另请参阅导致此代码的相同问题:
private bool IsFontInstalled(string fontName) {
using (var testFont = new Font(fontName, 8)) {
return 0 == string.Compare(
fontName,
testFont.Name,
StringComparison.InvariantCultureIgnoreCase);
}
}
#4
0
Arial Bold Italic is unlikely to be a font. It's a subclass of the Arial family.
Arial Bold Italic不太可能是一种字体。它是Arial家族的一个子类。
Try keeping it simple and test for 'Arial'.
尽量保持简单并测试'Arial'。