次の方法で共有


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

更新 : 2007 年 11 月

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

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

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

使用例

次の 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 コンストラクタの場合、最初に渡される引数は FontFamily オブジェクトですが、この Font コンストラクタに渡される最初の引数はフォント ファミリ名です。Font オブジェクトが作成されると、そのオブジェクトが Graphics クラスの DrawString メソッドに渡され、ファミリ名と一緒にスタイルの名前が表示されます。

下記のコードによる出力の例を次の図に示します。

フォント テキスト

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

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

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

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

コードのコンパイル方法

前述の例は Windows フォームと一緒に使用することが想定されていて、PaintEventHandler のパラメータである PaintEventArgs e が必要です。

参照

参照

PrivateFontCollection

その他の技術情報

フォントとテキストの使用