在.Net中创建和使用自已的私有字体

.Net 专栏收录该内容
3 篇文章 0 订阅

 

派生自抽象类FontCollection的PrivateFontCollection可以帮你维护一个为你的应用程序定制的字体集。叫做私有字体集。一个私有字体集可以既包括已经安装到操作系统中的字体,也可以包括未安装的字体。

 

要添加一个私有字体到私有字体集,只需调用PrivateFontCollection的AddFontFile方法。

 

PrivateFontCollection对象的Families属性是一个FontFamily类型的数组,包含所有已经成功添加进去的字体。通过访问该属性可以得到一个FontFamily数组。如:

FontFamily[] fontFamilies = privateFontCollection.Families;

 

通过访问PrivateFontCollection的Length属性可以得到PrivateFontCollection中的FontFamily个数。需要注意的是,Length属性的值可能会小于你成功调用AddFontFile的次数。因为PrivateFontCollection会分析你添加的每一个文件,如果一个文件描述的字体与PrivateFontCollection中已经存在的某个字体属于同一个FontFamily,则不作为新字体添加。如:假如你添加ArialBd.ttf, Times.ttf和TimesDb.ttf到一个PrivateFontCollection,而Times.ttf和TimesDb.ttf属于同一个FontFamily,那么虽然你添加了三个文件, Length的值却为2。

  • 示例:

本例添加下列三个字体文件到一个PrivateFontCollection对象中:

· C:/systemroot/Fonts/Arial.ttf (Arial, regular)

· C:/systemroot/Fonts/CourBI.ttf (Courier New, bold italic)

· C:/systemroot/Fonts/TimesBd.ttf (Times New Roman, bold)

代码首先通过访问PrivateFontCollection对象的Families属性获得FontFamily数组。

然后,对于每一个FontFamily,代码通过将相应的FontStyle枚举值作为参数调用FontFamily的IsStyleAvailable方法来判断一个字体风格(regular、bold、italic、bold italic、underline和strikeout)是否支持。

接下来,如果给定的字体风格可用,代码就将该FontFamily对象的Name属性值(即字体名,注意不是FontFamily对象本身)作为第一个参数传给Font类构造函数创建一个该风格的字体。

当一个字体被创建成功后,它被传给Graphics对象的DrawString方法,从而写一个字符串到窗体上。

代码执行完毕后,窗体样子看起来象下图:

clip_image002

Arial.ttf字体文件仅描述了Arial的regular风格。必须注意的是,上图窗体中用Arial字体写出了几种不同的风格,不仅仅是regular。这是因为GDI+可以从regular模拟出bold、italic和bold italic风格。GDI+还能够从regular风格制造出下划线和删除线风格。相似地,GDI+也可以从bold或italic风格模拟出bold italic风格。所以,虽然TimesDb.ttf只描述了Times New Roman字体的bold风格,示例中还是写出了bold italic风格的字符串。

  • n 源码:

C#源码:

protected override void OnPaint(PaintEventArgs e)

{

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("C://systemroot//Fonts//Arial.ttf");

privateFontCollection.AddFontFile("C://systemroot//Fonts//CourBI.ttf");

privateFontCollection.AddFontFile("C://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;

}

 

base.OnPaint(e);

}

 

Visual Basic 源码:

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("C:/systemroot/Fonts/Arial.ttf")

privateFontCollection.AddFontFile("C:/systemroot/Fonts/CourBI.ttf")

privateFontCollection.AddFontFile("C:/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

 

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值