如何:创建专用的字体集合
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 系列。
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 窗体,需要 PaintEventArgse
,这是 PaintEventHandler 的参数。
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈