方法: プライベート フォント コレクションを作成する

PrivateFontCollection クラスは、FontCollection 抽象基底クラスから継承されたものです。 PrivateFontCollection オブジェクトを使用すれば、自分のアプリケーション専用のフォント セットを維持することができます。 プライベート フォント コレクションには、インストールされているシステム フォントに加えて、コンピューターにインストールされていないフォントも含めることができます。 フォント ファイルをプライベート フォント コレクションに追加するには、PrivateFontCollection オブジェクトの AddFontFile メソッドを呼び出します。

PrivateFontCollection オブジェクトの Families プロパティには、FontFamily オブジェクトの配列が格納されます。

プライベート フォント コレクション内のフォント ファミリの数は、必ずしもコレクションに追加されたフォント ファイルの数と同じではありません。 たとえば、ファイル ArialBd.tff、Times.tff、および TimesBd.tff をコレクションに追加するとします。 コレクションには 3 つのファイルが存在することになりますが、ファミリは 2 つだけです。Times.tff と TimesBd.tff は同じファミリに属しているためです。

以下の例では、次の 3 つのフォント ファイルが PrivateFontCollection オブジェクトに追加されます。

  • C:\systemroot\Fonts\Arial.tff (Arial、標準)

  • C:\systemroot\Fonts\CourBI.tff (Courier New、太字斜体)

  • C:\systemroot\Fonts\TimesBd.tff (Times New Roman、太字)

このコードによって、PrivateFontCollection オブジェクトの Families プロパティから FontFamily オブジェクトの配列が取得されます。

コードを実行すると、コレクション内の FontFamily オブジェクトごとに、IsStyleAvailable メソッドが呼び出され、各種のスタイル (標準、太字、斜体、太字斜体、下線、および取り消し線) が使用可能かどうかが判断されます。 IsStyleAvailable メソッドに渡される引数は、FontStyle 列挙体のメンバーです。

指定されたファミリとスタイルの組み合わせを使用できる場合、そのファミリとスタイルを使用して Font オブジェクトが構築されます。 Font コンストラクターに渡される最初の引数はフォント ファミリ名です (Font コンストラクターの他のバリエーションとは違って、FontFamily オブジェクトではありません)。 Font オブジェクトが構築されると、それは Graphics クラスの DrawString メソッドに渡され、ファミリ名と共にスタイル名が表示されます。

下記のコードの出力は、次の図に示す出力に似ています。

Screenshot that shows text in various fonts.

Arial.tff (以下のコード例でプライベート フォント コレクションに追加されたもの) は、Arial 標準スタイルのフォント ファイルです。 ただし、プログラムの出力には、Arial フォント ファミリの標準以外に使用可能なスタイルがいくつか表示されていることに注意してください。 これは、GDI+ を使用すると標準スタイルから太字、斜体、太字斜体の各スタイルをシミュレートできるためです。 また、GDI+ を使用すると、標準スタイルから下線と取り消し線を生成することもできます。

同様に、GDI+ では、太字スタイルまたは斜体スタイルのどちらからも太字斜体スタイルをシミュレートすることができます。 プログラムの出力を見ると、コレクション内では TimesBd.tff (Times New Roman、太字) が唯一の Times ファイルであるのに、太字斜体スタイルも Times ファミリで使用できるようになっているのがわかります。

PointF pointF = new PointF(10, 0);
SolidBrush solidBrush = new SolidBrush(Color.Black);

int count = 0;
string familyName = "";
string familyNameAndStyle;
FontFamily[] fontFamilies;
PrivateFontCollection privateFontCollection = new PrivateFontCollection();

// Add three font files to the private collection.
privateFontCollection.AddFontFile("D:\\systemroot\\Fonts\\Arial.ttf");
privateFontCollection.AddFontFile("D:\\systemroot\\Fonts\\CourBI.ttf");
privateFontCollection.AddFontFile("D:\\systemroot\\Fonts\\TimesBD.ttf");

// Get the array of FontFamily objects.
fontFamilies = privateFontCollection.Families;

// How many objects in the fontFamilies array?
count = fontFamilies.Length;

// Display the name of each font family in the private collection
// along with the available styles for that font family.
for (int j = 0; j < count; ++j)
{
    // Get the font family name.
    familyName = fontFamilies[j].Name;

    // Is the regular style available?
    if (fontFamilies[j].IsStyleAvailable(FontStyle.Regular))
    {
        familyNameAndStyle = "";
        familyNameAndStyle = familyNameAndStyle + familyName;
        familyNameAndStyle = familyNameAndStyle + " Regular";

        Font regFont = new Font(
           familyName,
           16,
           FontStyle.Regular,
           GraphicsUnit.Pixel);

        e.Graphics.DrawString(
           familyNameAndStyle,
           regFont,
           solidBrush,
           pointF);

        pointF.Y += regFont.Height;
    }

    // Is the bold style available?
    if (fontFamilies[j].IsStyleAvailable(FontStyle.Bold))
    {
        familyNameAndStyle = "";
        familyNameAndStyle = familyNameAndStyle + familyName;
        familyNameAndStyle = familyNameAndStyle + " Bold";

        Font boldFont = new Font(
           familyName,
           16,
           FontStyle.Bold,
           GraphicsUnit.Pixel);

        e.Graphics.DrawString(familyNameAndStyle, boldFont, solidBrush, pointF);

        pointF.Y += boldFont.Height;
    }
    // Is the italic style available?
    if (fontFamilies[j].IsStyleAvailable(FontStyle.Italic))
    {
        familyNameAndStyle = "";
        familyNameAndStyle = familyNameAndStyle + familyName;
        familyNameAndStyle = familyNameAndStyle + " Italic";

        Font italicFont = new Font(
           familyName,
           16,
           FontStyle.Italic,
           GraphicsUnit.Pixel);

        e.Graphics.DrawString(
           familyNameAndStyle,
           italicFont,
           solidBrush,
           pointF);

        pointF.Y += italicFont.Height;
    }

    // Is the bold italic style available?
    if (fontFamilies[j].IsStyleAvailable(FontStyle.Italic) &&
    fontFamilies[j].IsStyleAvailable(FontStyle.Bold))
    {
        familyNameAndStyle = "";
        familyNameAndStyle = familyNameAndStyle + familyName;
        familyNameAndStyle = familyNameAndStyle + "BoldItalic";

        Font italicFont = new Font(
           familyName,
           16,
           FontStyle.Italic | FontStyle.Bold,
           GraphicsUnit.Pixel);

        e.Graphics.DrawString(
           familyNameAndStyle,
           italicFont,
           solidBrush,
           pointF);

        pointF.Y += italicFont.Height;
    }
    // Is the underline style available?
    if (fontFamilies[j].IsStyleAvailable(FontStyle.Underline))
    {
        familyNameAndStyle = "";
        familyNameAndStyle = familyNameAndStyle + familyName;
        familyNameAndStyle = familyNameAndStyle + " Underline";

        Font underlineFont = new Font(
           familyName,
           16,
           FontStyle.Underline,
           GraphicsUnit.Pixel);

        e.Graphics.DrawString(
           familyNameAndStyle,
           underlineFont,
           solidBrush,
           pointF);

        pointF.Y += underlineFont.Height;
    }

    // Is the strikeout style available?
    if (fontFamilies[j].IsStyleAvailable(FontStyle.Strikeout))
    {
        familyNameAndStyle = "";
        familyNameAndStyle = familyNameAndStyle + familyName;
        familyNameAndStyle = familyNameAndStyle + " Strikeout";

        Font strikeFont = new Font(
           familyName,
           16,
           FontStyle.Strikeout,
           GraphicsUnit.Pixel);

        e.Graphics.DrawString(
           familyNameAndStyle,
           strikeFont,
           solidBrush,
           pointF);

        pointF.Y += strikeFont.Height;
    }

    // Separate the families with white space.
    pointF.Y += 10;
} // for
Dim pointF As New PointF(10, 0)
Dim solidBrush As New SolidBrush(Color.Black)

Dim count As Integer = 0
Dim familyName As String = ""
Dim familyNameAndStyle As String
Dim fontFamilies() As FontFamily
Dim privateFontCollection As New PrivateFontCollection()

' Add three font files to the private collection.
privateFontCollection.AddFontFile("D:\systemroot\Fonts\Arial.ttf")
privateFontCollection.AddFontFile("D:\systemroot\Fonts\CourBI.ttf")
privateFontCollection.AddFontFile("D:\systemroot\Fonts\TimesBD.ttf")

' Get the array of FontFamily objects.
fontFamilies = privateFontCollection.Families

' How many objects in the fontFamilies array?
count = fontFamilies.Length

' Display the name of each font family in the private collection
' along with the available styles for that font family.
Dim j As Integer

While j < count
    ' Get the font family name.
    familyName = fontFamilies(j).Name

    ' Is the regular style available?
    If fontFamilies(j).IsStyleAvailable(FontStyle.Regular) Then
        familyNameAndStyle = ""
        familyNameAndStyle = familyNameAndStyle & familyName
        familyNameAndStyle = familyNameAndStyle & " Regular"

        Dim regFont As New Font( _
           familyName, _
           16, _
           FontStyle.Regular, _
           GraphicsUnit.Pixel)

        e.Graphics.DrawString( _
           familyNameAndStyle, _
           regFont, _
           solidBrush, _
           pointF)

        pointF.Y += regFont.Height
    End If

    ' Is the bold style available?
    If fontFamilies(j).IsStyleAvailable(FontStyle.Bold) Then
        familyNameAndStyle = ""
        familyNameAndStyle = familyNameAndStyle & familyName
        familyNameAndStyle = familyNameAndStyle & " Bold"

        Dim boldFont As New Font( _
           familyName, _
           16, _
           FontStyle.Bold, _
           GraphicsUnit.Pixel)

        e.Graphics.DrawString( _
           familyNameAndStyle, _
           boldFont, _
           solidBrush, _
           pointF)

        pointF.Y += boldFont.Height
    End If

    ' Is the italic style available?
    If fontFamilies(j).IsStyleAvailable(FontStyle.Italic) Then
        familyNameAndStyle = ""
        familyNameAndStyle = familyNameAndStyle & familyName
        familyNameAndStyle = familyNameAndStyle & " Italic"

        Dim italicFont As New Font( _
           familyName, _
           16, _
           FontStyle.Italic, _
           GraphicsUnit.Pixel)

        e.Graphics.DrawString( _
           familyNameAndStyle, _
           italicFont, _
           solidBrush, pointF)

        pointF.Y += italicFont.Height
    End If

    ' Is the bold italic style available?
    If fontFamilies(j).IsStyleAvailable(FontStyle.Italic) And _
       fontFamilies(j).IsStyleAvailable(FontStyle.Bold) Then
        familyNameAndStyle = ""
        familyNameAndStyle = familyNameAndStyle & familyName
        familyNameAndStyle = familyNameAndStyle & "BoldItalic"

        Dim italicFont As New Font( _
            familyName, _
            16, _
            FontStyle.Italic Or FontStyle.Bold, _
            GraphicsUnit.Pixel)

        e.Graphics.DrawString( _
           familyNameAndStyle, _
           italicFont, _
           solidBrush, _
           pointF)

        pointF.Y += italicFont.Height
    End If
    ' Is the underline style available?
    If fontFamilies(j).IsStyleAvailable(FontStyle.Underline) Then
        familyNameAndStyle = ""
        familyNameAndStyle = familyNameAndStyle & familyName
        familyNameAndStyle = familyNameAndStyle & " Underline"

        Dim underlineFont As New Font( _
           familyName, _
           16, _
           FontStyle.Underline, _
           GraphicsUnit.Pixel)

        e.Graphics.DrawString( _
           familyNameAndStyle, _
           underlineFont, _
           solidBrush, _
           pointF)

        pointF.Y += underlineFont.Height
    End If

    ' Is the strikeout style available?
    If fontFamilies(j).IsStyleAvailable(FontStyle.Strikeout) Then
        familyNameAndStyle = ""
        familyNameAndStyle = familyNameAndStyle & familyName
        familyNameAndStyle = familyNameAndStyle & " Strikeout"

        Dim strikeFont As New Font( _
           familyName, _
           16, _
           FontStyle.Strikeout, _
           GraphicsUnit.Pixel)

        e.Graphics.DrawString( _
           familyNameAndStyle, _
           strikeFont, _
           solidBrush, _
           pointF)

        pointF.Y += strikeFont.Height
    End If

    ' Separate the families with white space.
    pointF.Y += 10
End While

コードのコンパイル

前の例は、Windows フォームで使用するために設計されていて、PaintEventHandler のパラメーターである PaintEventArgse を必要とします。

関連項目