Como se certificar de uma fonte existe antes de usá-lo com .NET
Pergunta
Eu tenho um projeto Formas VB.NET Windows que em um tintas de ponto de texto diretamente para o para o formulário em tempo de execução. Antes de pintar com a fonte, porém, eu quero ter certeza de que a fonte e font-size existe na máquina do usuário. Se não o fizerem, eu vou tentar algumas outras fontes semelhantes, acabou inadimplente com Arial ou algo assim.
Qual é a melhor maneira de testar e validar uma fonte no computador de um usuário?
Solução
De um artigo MSDN intitulado "How To: Enumerar Instalada Fontes", eu encontrei este código:
InstalledFontCollection installedFontCollection = new InstalledFontCollection();
// Get the array of FontFamily objects.
FontFamily[] fontFamilies = installedFontCollection.Families;
Outras dicas
Aqui é uma solução, em 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);
}
}
}
E aqui é um método em 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
Arial negrito itálico é improvável que seja uma fonte. É uma subclasse da família Arial.
Tente mantê-lo simples e teste para 'Arial'.