Compartilhar via


Como: Enumerar fontes instaladas

The InstalledFontCollection classe herda a partir do FontCollection classe base abstrata. Você pode usar um InstalledFontCollection objeto para enumerar as fontes instaladas no computador. The Families propriedade de um InstalledFontCollection objeto é uma matriz de FontFamily objetos.

Exemplo

O exemplo a seguir lista os nomes de todas as famílias de fontes instaladas no computador.O código recupera o Name propriedade de cada FontFamily objeto da matriz retornada pela Families propriedade. sistema autônomo sistema autônomo nomes da família são recuperados, eles são concatenados para lista do formulário separados um por vírgulas.Em seguida, a DrawString método para o Graphics classe desenha lista separada por vírgulas em um retângulo.

Se você executar o código de exemplo, a saída será semelhante ao mostrado na ilustração a seguir.

Dim fontFamily As New FontFamily("Arial")
Dim font As New Font( _
   fontFamily, _
   8, _
   FontStyle.Regular, _
   GraphicsUnit.Point)
Dim rectF As New RectangleF(10, 10, 500, 500)
Dim solidBrush As New SolidBrush(Color.Black)

Dim familyName As String
Dim familyList As String = ""
Dim fontFamilies() As FontFamily

Dim installedFontCollection As New InstalledFontCollection()

' Get the array of FontFamily objects.
fontFamilies = installedFontCollection.Families

' The loop below creates a large string that is a comma-separated
' list of all font family names.
Dim count As Integer = fontFamilies.Length
Dim j As Integer

While j < count
    familyName = fontFamilies(j).Name
    familyList = familyList & familyName
    familyList = familyList & ",  "
    j += 1
End While

' Draw the large string (list of all families) in a rectangle.
e.Graphics.DrawString(familyList, font, solidBrush, rectF)

FontFamily fontFamily = new FontFamily("Arial");
Font font = new Font(
   fontFamily,
   8,
   FontStyle.Regular,
   GraphicsUnit.Point);
RectangleF rectF = new RectangleF(10, 10, 500, 500);
SolidBrush solidBrush = new SolidBrush(Color.Black);

string familyName;
string familyList = "";
FontFamily[] fontFamilies;

InstalledFontCollection installedFontCollection = new InstalledFontCollection();

// Get the array of FontFamily objects.
fontFamilies = installedFontCollection.Families;

// The loop below creates a large string that is a comma-separated
// list of all font family names.

int count = fontFamilies.Length;
for (int j = 0; j < count; ++j)
{
    familyName = fontFamilies[j].Name;
    familyList = familyList + familyName;
    familyList = familyList + ",  ";
}

// Draw the large string (list of all families) in a rectangle.
e.Graphics.DrawString(familyList, font, solidBrush, rectF);

Compilando o código

The preceding example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of PaintEventHandler.

Consulte também

Outros recursos

Usando fontes e texto