Condividi tramite


Procedura: enumerare i tipi di carattere installati

Aggiornamento: novembre 2007

La classe InstalledFontCollection eredita dalla classe base astratta FontCollection. È possibile utilizzare un oggetto InstalledFontCollection per enumerare i caratteri installati nel computer. La proprietà Families di un oggetto InstalledFontCollection corrisponde a una matrice di oggetti FontFamily.

Esempio

Con l'esempio riportato di seguito vengono elencati i nomi di tutti i gruppi di caratteri installati sul computer. Viene recuperata la proprietà Name di ciascun oggetto FontFamily nella matrice restituita dalla proprietà Families. Una volta recuperati, i nomi dei gruppi vengono concatenati per formare un elenco separato da virgole. Quindi, tramite il metodo DrawString della classe Graphics, l'elenco separato da virgole viene visualizzato in un rettangolo.

Se si esegue il codice di esempio, l'output sarà analogo a quello mostrato nell'illustrazione seguente.

Tipi di caratteri installati

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

Compilazione del codice

L'esempio riportato in precedenza è stato creato per essere utilizzato con Windows Form e richiede PaintEventArgs e, un parametro di PaintEventHandler.

Vedere anche

Altre risorse

Utilizzo di tipi di carattere e testo