方法: インストールされているフォントを列挙する

InstalledFontCollection クラスは、FontCollection 抽象基本クラスを継承します。 InstalledFontCollection オブジェクトを使用すると、コンピューターにインストールされているフォントを列挙できます。 InstalledFontCollection オブジェクトの Families プロパティは、FontFamily オブジェクトの配列です。

次の例では、コンピューターにインストールされているすべてのフォント ファミリの名前を一覧表示しています。 このコードを実行すると、Families プロパティによって返される配列内の各 FontFamily オブジェクトの Name プロパティが取得されます。 ファミリ名が取得されると、それらが連結され、コンマ区切りのリストが形成されます。 その後、Graphics クラスの DrawString メソッドによって、コンマ区切りのリストが四角形の中に描画されます。

このコード例を実行すると、出力は次の図に示すようになります。

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)

コードのコンパイル

前の例は、Windows フォームで使用するために設計されていて、PaintEventArgs のパラメーターである ePaintEventHandler を必要とします。 また、System.Drawing.Text 名前空間をインポートする必要があります。

関連項目