Freigeben über


Vorgehensweise: Auflisten installierter Schriftarten

Die InstalledFontCollection-Klasse erbt von der abstrakten Basisklasse FontCollection. Sie können ein InstalledFontCollection-Objekt verwenden, um die auf dem Computer installierten Schriftarten aufzuzählen. Die Eigenschaft Families eines InstalledFontCollection-Objekts ist ein Array von FontFamily-Objekten.

Beispiel

Im folgenden Beispiel werden die Namen aller Schriftfamilien genannt, die auf dem Computer installiert sind. Der Code ruft die Eigenschaft Name von jedem FontFamily-Objekt im Array ab, das von der Eigenschaft Families zurückgegeben wird. Die abgerufenen Familiennamen werden in einer durch Komma getrennten Liste verkettet. Anschließend zeichnet die Methode DrawString der Graphics-Klasse die durch Komma getrennte Liste in einem Rechteck.

Wenn Sie den Beispielcode ausführen, ähnelt die Ausgabe derjenigen in der folgenden Abbildung:

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)

Kompilieren des Codes

Das obige Beispiel ist für die Verwendung in Windows Forms konzipiert und erfordert die PaintEventArgse-Klasse, die ein Parameter von PaintEventHandler ist. Darüber hinaus sollten Sie den Namespace System.Drawing.Text importieren.

Siehe auch