如何:创建专用的字体集合

PrivateFontCollection 类继承自 FontCollection 抽象基类。 可使用 PrivateFontCollection 对象维护应用程序专用的字体集。 专用的字体集合既可包括计算机上安装的系统字体,又可包括计算机上尚未安装的字体。 若要向专用的字体集内添加字体文件,请调用 PrivateFontCollection 对象的 AddFontFile 方法。

PrivateFontCollection 对象的 Families 属性包含一组 FontFamily 对象。

专用字体集合中字体系列的数量不必与已添加到字体集内的字体文件的数量相同。 例如,假定将 ArialBd.tff、Times.tff 和 TimesBd.tff 文件添加到一个集合内。 该字体集合内将有三个文件,但是只有两个系列,这是因为 Times.tff 和 TimesBd.tff 属于相同的系列。

示例

下面的示例将以下三个字体文件添加到 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 方法,以显示字体名称和字形名称。

下面的代码的输出类似于下面插图中所显示的输出。

字体文本

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

其他资源

使用字体和文本