共用方式為


列舉已安裝的字型

InstalledFontCollection 類別是從 FontCollection 抽象基底類別繼承而來。您可以使用 InstalledFontCollection 物件來列舉安裝在電腦上的字型。InstalledFontCollection 物件的 Families 屬性是 FontFamily 物件的陣列。

下列範例會列出安裝在電腦上的所有字型家族名稱清單。程式碼會在 Families 屬性傳回來的陣列中擷取每一個 FontFamily 物件的 Name 屬性。由於家族名稱是擷取而來的,因此可將它們串連起來形成以逗號分隔的清單。接著 Graphics 類別的 DrawString 方法便會在矩形中繪製以逗號分隔的清單。

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);

如果執行上述程式碼,它的輸出將會與下圖中所顯示的類似。