Porady: wyliczanie zainstalowanych czcionek

Klasa InstalledFontCollection dziedziczy z abstrakcyjnej klasy bazowej FontCollection . Można użyć InstalledFontCollection obiektu , aby wyliczyć czcionki zainstalowane na komputerze. Właściwość FamiliesInstalledFontCollection obiektu jest tablicą FontFamily obiektów.

Przykład

W poniższym przykładzie wymieniono nazwy wszystkich rodzin czcionek zainstalowanych na komputerze. Kod pobiera Name właściwość każdego FontFamily obiektu w tablicy zwróconej Families przez właściwość . Podczas pobierania nazw rodzin są one łączone w celu utworzenia listy rozdzielanej przecinkami. DrawString Następnie metoda Graphics klasy rysuje listę rozdzielaną przecinkami w prostokątze.

Jeśli uruchomisz przykładowy kod, dane wyjściowe będą podobne do pokazanych na poniższej ilustracji:

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)

Kompilowanie kodu

Powyższy przykład jest przeznaczony do użycia z formularzami Systemu Windows i wymaga PaintEventArgseparametru , który jest parametrem PaintEventHandler. Ponadto należy zaimportować System.Drawing.Text przestrzeń nazw.

Zobacz też