Freigeben über


Aufzählen der installierten Schriftarten

Die InstalledFontCollection-Klasse erbt von der abstrakten FontCollection-Basisklasse. Mit einem InstalledFontCollection-Objekt können Sie die Schriftarten aufzählen lassen, die auf dem Computer installiert sind. Die Families-Eigenschaft eines InstalledFontCollection-Objekts ist ein Array von FontFamily-Objekten.

Im folgenden Beispiel werden die Namen aller auf dem Computer installierten Schriftartenkategorien aufgelistet. Der Code ruft die Name-Eigenschaft jedes FontFamily-Objekts im Array ab, das von der Families-Eigenschaft zurückgegeben wird. Beim Abrufen werden die Kategorienamen verkettet, so dass sie eine durch Kommas getrennte Liste ergeben. Anschließend wird die Liste mit Trennkommas von der DrawString-Methode der Graphics-Klasse in einem Rechteck gezeichnet.

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 + ",  "
End While

' Draw the large string (list of all families) in a rectangle.
e.Graphics.DrawString(familyList, font, solidBrush, rectF)
[C#]
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);

Wenn Sie den vorangehenden Code ausführen, erhalten Sie ein Ergebnis, das mit dem in der folgenden Abbildung vergleichbar ist.