次の方法で共有


インストール済みフォントの列挙

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

上のコードを実行した場合、次の図に示すような出力が生成されます。