Como enumerar as fontes instaladas

A InstalledFontCollection classe herda da FontCollection classe base abstrata. Você pode usar um InstalledFontCollection objeto para enumerar as fontes instaladas no computador. A 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 a Name propriedade de cada FontFamily objeto na matriz retornada pela Families propriedade. À medida que os nomes de família são recuperados, eles são concatenados para formar uma lista separada por vírgula. Em seguida, o DrawStringGraphics método da classe desenha a lista separada por vírgulas em um retângulo.

Se você executar o código de exemplo, a saída será semelhante à mostrada na ilustração a seguir:

Screenshot that shows the installed font families.

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);
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)

Compilando o código

O exemplo anterior foi projetado para uso com o Windows Forms e requer PaintEventArgse, que é um parâmetro de PaintEventHandler. Além disso, você deve importar o System.Drawing.Text namespace.

Confira também