Freigeben über


So geht's: Installierte Schriftarten auflisten

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

Beispiel

Im folgenden Beispiel werden die Namen aller schriftartenfamilien aufgelistet, die auf dem Computer installiert sind. Der Code ruft die Name Eigenschaft jedes FontFamily Objekts im Array ab, das von der Families Eigenschaft zurückgegeben wird. Wenn die Familiennamen abgerufen werden, werden sie verkettet, um eine durch Trennzeichen getrennte Liste zu bilden. Anschließend zeichnet die DrawString Methode der Graphics Klasse die durch Trennzeichen getrennte Liste in einem Rechteck.

Wenn Sie den Beispielcode ausführen, ist die Ausgabe ähnlich wie in der folgenden Abbildung dargestellt:

Screenshot der installierten Schriftartfamilien.

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)

Code kompilieren

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

Siehe auch