共用方式為


HOW TO:列舉已安裝的字型

更新:2007 年 11 月

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 & ",  "
    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);

編譯程式碼

上述範例是專為與 Windows Form 搭配使用而設計的,而且需要 PaintEventArgs e (即 PaintEventHandler 的參數)。

請參閱

其他資源

使用字型和文字