Gewusst wie: Auflisten installierter Schriftarten
Aktualisiert: November 2007
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 entspricht einem Array von FontFamily-Objekten.
Beispiel
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, sodass sie eine durch Kommas getrennte Liste ergeben. Anschließend wird die durch Kommas getrennte Liste von der DrawString-Methode der Graphics-Klasse in einem Rechteck gezeichnet.
Wenn Sie den Beispielcode ausführen, erhalten Sie ein Ergebnis, das mit dem in der folgenden Abbildung vergleichbar ist.
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);
Kompilieren des Codes
Das vorhergehende Beispiel ist für die Verwendung mit Windows Forms konzipiert und erfordert PaintEventArgs e, einen Parameter von PaintEventHandler.