方法: プライベート フォント コレクションを作成するHow to: Create a Private Font Collection

PrivateFontCollectionクラスから継承、FontCollection抽象基本クラス。The PrivateFontCollection class inherits from the FontCollection abstract base class. 使用することができます、PrivateFontCollection具体的には、アプリケーションのフォントのセットを保持するオブジェクト。You can use a PrivateFontCollection object to maintain a set of fonts specifically for your application. プライベート フォント コレクションには、インストールされているシステム フォントだけでなく、コンピューターにインストールされていないフォントを含めることができます。A private font collection can include installed system fonts as well as fonts that have not been installed on the computer. プライベート フォント コレクションには、フォント ファイルを追加するには、呼び出し、AddFontFileのメソッドをPrivateFontCollectionオブジェクト。To add a font file to a private font collection, call the AddFontFile method of a PrivateFontCollection object.

FamiliesのプロパティをPrivateFontCollectionオブジェクトには配列が含まれていますFontFamilyオブジェクト。The Families property of a PrivateFontCollection object contains an array of FontFamily objects.

プライベート フォント コレクション内のフォント ファミリの数は必ずしもコレクションに追加されたフォント ファイルの数と同じです。The number of font families in a private font collection is not necessarily the same as the number of font files that have been added to the collection. たとえば、ArialBd.tff、Times.tff、および TimesBd.tff ファイルをコレクションに追加するとします。For example, suppose you add the files ArialBd.tff, Times.tff, and TimesBd.tff to a collection. ありますが、3 つのファイル、コレクション内の 2 つだけのファミリ Times.tff と TimesBd.tff 同じファミリに属しているためです。There will be three files but only two families in the collection because Times.tff and TimesBd.tff belong to the same family.

Example

次の例では、次の 3 つのフォント ファイルをPrivateFontCollectionオブジェクト。The following example adds the following three font files to a PrivateFontCollection object:

  • C:\systemroot\Fonts\Arial.tff (Arial、正規表現)C:\systemroot\Fonts\Arial.tff (Arial, regular)

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

  • C:\systemroot\Fonts\TimesBd.tff (Times New Roman, bold)C:\systemroot\Fonts\TimesBd.tff (Times New Roman, bold)

コードの配列を取得するFontFamilyオブジェクトから、Familiesのプロパティ、PrivateFontCollectionオブジェクト。The code retrieves an array of FontFamily objects from the Families property of the PrivateFontCollection object.

FontFamilyコードでは、コレクション内のオブジェクト、IsStyleAvailableするさまざまなスタイル (標準、太字、斜体、太字、斜体、下線、取り消し線) が使用できるかどうかを判断するメソッド。For each FontFamily object in the collection, the code calls the IsStyleAvailable method to determine whether various styles (regular, bold, italic, bold italic, underline, and strikeout) are available. 渡される引数、IsStyleAvailableメソッドのメンバーである、FontStyle列挙体。The arguments passed to the IsStyleAvailable method are members of the FontStyle enumeration.

指定されたファミリ/スタイルの組み合わせがある場合、Fontそのファミリとスタイルを使用してオブジェクトを構築します。If a given family/style combination is available, a Font object is constructed using that family and style. 渡される最初の引数、Fontコンス トラクターは、フォント ファミリ名 (いない、FontFamilyオブジェクトの他のバリエーションの場合と同様、Fontコンス トラクター)。The first argument passed to the Font constructor is the font family name (not a FontFamily object as is the case for other variations of the Font constructor). 後に、Fontオブジェクトが構築されたに渡される、DrawStringのメソッド、Graphicsスタイルの名前と共に、ファミリ名を表示するクラス。After the Font object is constructed, it is passed to the DrawString method of the Graphics class to display the family name along with the name of the style.

次のコードの出力は次の図に示すように出力になります。The output of the following code is similar to the output shown in the following illustration:

さまざまなフォントでテキストを示すスクリーン ショット。

次のコード例では、プライベート フォント コレクションに追加された) Arial.tff は、Arial 標準スタイルのフォント ファイルです。Arial.tff (which was added to the private font collection in the following code example) is the font file for the Arial regular style. ただし、プログラムの出力が Arial フォント ファミリの標準以外の複数の使用可能なスタイルを表示することに注意してください。Note, however, that the program output shows several available styles other than regular for the Arial font family. GDI + は標準のスタイルから太字、斜体、および太字斜体スタイルでシミュレートできるためです。That is because GDI+ can simulate the bold, italic, and bold italic styles from the regular style. GDI + も生成できる下線や標準のスタイルから取り消し線。GDI+ can also produce underlines and strikeouts from the regular style.

同様に、GDI + をシミュレートできます、太字または斜体のスタイルのいずれかから太字斜体のスタイル。Similarly, GDI+ can simulate the bold italic style from either the bold style or the italic style. プログラムの出力が TimesBd.tff (Times New Roman、太字) が唯一にもかかわらず太字斜体スタイルが回ファミリに使用できることを示しています、コレクション内の回ファイル。The program output shows that the bold italic style is available for the Times family even though TimesBd.tff (Times New Roman, bold) is the only Times file in the collection.

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

コードのコンパイルCompiling the Code

前の例は、Windows フォームで使用するために設計されています。 また必要がPaintEventArgs e、はのパラメーターPaintEventHandlerします。The preceding example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of PaintEventHandler.

関連項目See also