Procedura: enumerare i tipi di carattere installati

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

Esempio

Nell'esempio seguente sono elencati i nomi di tutte le famiglie di caratteri installate nel computer. Il codice recupera la Name proprietà di ogni FontFamily oggetto nella matrice restituita dalla Families proprietà . Man mano che vengono recuperati i nomi di famiglia, vengono concatenati per formare un elenco delimitato da virgole. Il metodo della Graphics classe disegna quindi DrawString l'elenco delimitato da virgole in un rettangolo.

Se si esegue il codice di esempio, l'output sarà simile a quello illustrato nella figura seguente:

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)

Compilazione del codice

L'esempio precedente è progettato per l'uso con Windows Form e richiede PaintEventArgse, un parametro di PaintEventHandler. Inoltre, è necessario importare lo System.Drawing.Text spazio dei nomi.

Vedi anche