如何:建立私用字型集合
PrivateFontCollection 類別繼承自抽象基底類別 FontCollection。 您可以使用 PrivateFontCollection 物件來維護一組專為您的應用程式使用的字型。 私人字型集合可以包含已安裝的系統字型,以及電腦上尚未安裝的字型。 若要將字型檔案新增至私人字型集合,請呼叫 AddFontFile 物件的 PrivateFontCollection 方法。
Families 物件的 PrivateFontCollection 屬性包含 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,粗體)
程式碼會從 FontFamily 物件的 Families 屬性擷取 PrivateFontCollection 物件的陣列。
針對集合中的每個 FontFamily 物件,程式代碼會呼叫 IsStyleAvailable 方法,以判斷是否可以使用各種樣式 (標準、粗體、斜體、粗體斜體、底線和刪除線)。 傳遞至 IsStyleAvailable 方法的引數是 FontStyle 列舉的成員。
如果有指定的系列/樣式組合可用,則會使用該系列和樣式來建構 Font 物件。 傳遞至 Font 建構函式的第一個引數是字型系列名稱 (不是 FontFamily 物件,就像 Font 建構函式的其他變數一樣)。 建構 Font 物件之後,它會傳遞至 DrawString 類別的 Graphics 方法,以顯示系列名稱以及樣式的名稱。
下列程式碼的輸出類似於下圖所示的輸出:
Arial.tff (已新增至下列程式代碼範例中的私人字型集合) 是 Arial 標準樣式的字型檔案。 不過請注意,程序輸出會針對 Arial 字型系列顯示數個可用的樣式,而非標準樣式。 這是因為 GDI+ 可以從標準樣式模擬粗體、斜體和粗體斜體樣式。 GDI+ 也可以從標準樣式產生底線和刪除線。
同樣地,GDI+ 可以從粗體樣式或斜體樣式模擬粗體斜體樣式。 程式輸出顯示,儘管 TimesBd.tff (Times New Roman, bold) 是集合中唯一的 Times 檔案,但粗體斜體樣式仍可供 Times 系列使用。
// Helper function to print text in a font and style
private float DrawFont(Graphics graphicsObj,
FontFamily family,
FontStyle style,
SolidBrush colorBrush,
PointF location,
string styleName)
{
// The string to print, which contains the family name and style
string familyNameAndStyle = $"{family.Name} {styleName}";
// Create the font object
using (Font fontObject = new Font(family.Name, 16, style, GraphicsUnit.Pixel))
{
// Draw the string
graphicsObj.DrawString(familyNameAndStyle, fontObject, colorBrush, location);
// Return the height of the font
return fontObject.Height;
}
}
// The OnPaint method of a form, which provides the graphics object
protected override void OnPaint(PaintEventArgs e)
{
PointF location = new PointF(10, 0);
SolidBrush solidBrush = new SolidBrush(Color.Black);
FontFamily[] fontFamilies;
PrivateFontCollection privateFontCollection = new PrivateFontCollection(); // Dispose later
// Add three font files to the private collection.
privateFontCollection.AddFontFile(System.Environment.ExpandEnvironmentVariables("%systemroot%\\Fonts\\Arial.ttf"));
privateFontCollection.AddFontFile(System.Environment.ExpandEnvironmentVariables("%systemroot%\\Fonts\\CourBI.ttf"));
privateFontCollection.AddFontFile(System.Environment.ExpandEnvironmentVariables("%systemroot%\\Fonts\\TimesBD.ttf"));
// Get the array of FontFamily objects.
fontFamilies = privateFontCollection.Families;
// Process each font in the collection
for (int i = 0; i < fontFamilies.Length; i++)
{
// Draw the font in every style
// Regular
if (fontFamilies[i].IsStyleAvailable(FontStyle.Regular))
location.Y += DrawFont(e.Graphics, fontFamilies[i], FontStyle.Regular, solidBrush, location, "Regular");
// Bold
if (fontFamilies[i].IsStyleAvailable(FontStyle.Bold))
location.Y += DrawFont(e.Graphics, fontFamilies[i], FontStyle.Bold, solidBrush, location, "Bold");
// Italic
if (fontFamilies[i].IsStyleAvailable(FontStyle.Italic))
location.Y += DrawFont(e.Graphics, fontFamilies[i], FontStyle.Italic, solidBrush, location, "Italic");
// Bold and Italic
if (fontFamilies[i].IsStyleAvailable(FontStyle.Bold) &&
fontFamilies[i].IsStyleAvailable(FontStyle.Italic))
location.Y += DrawFont(e.Graphics, fontFamilies[i], FontStyle.Bold | FontStyle.Italic, solidBrush, location, "BoldItalic");
// Underline
if (fontFamilies[i].IsStyleAvailable(FontStyle.Underline))
location.Y += DrawFont(e.Graphics, fontFamilies[i], FontStyle.Underline, solidBrush, location, "Underline");
// Strikeout
if (fontFamilies[i].IsStyleAvailable(FontStyle.Strikeout))
location.Y += DrawFont(e.Graphics, fontFamilies[i], FontStyle.Strikeout, solidBrush, location, "Strikeout");
// Extra space between font families
location.Y += 10;
}
privateFontCollection.Dispose();
}
' Helper function to print text in a font and style
Private Function DrawFont(graphicsObj As Graphics,
family As FontFamily,
style As FontStyle,
colorBrush As SolidBrush,
location As PointF,
styleName As String) As Single
' The string to print, which contains the family name and style
Dim familyNameAndStyle As String = $"{family.Name} {styleName}"
' Create the font object
Using fontObject As New Font(family.Name, 16, style, GraphicsUnit.Pixel)
' Draw the string
graphicsObj.DrawString(familyNameAndStyle, fontObject, colorBrush, location)
' Return the height of the font
Return fontObject.Height
End Using
End Function
' The OnPaint method of a form, which provides the graphics object
Protected Overrides Sub OnPaint(e As PaintEventArgs)
Dim location As New PointF(10, 0)
Dim solidBrush As New SolidBrush(Color.Black)
Dim fontFamilies() As FontFamily
Dim privateFontCollection As New PrivateFontCollection() ' Dispose later
' Add three font files to the private collection.
privateFontCollection.AddFontFile(System.Environment.ExpandEnvironmentVariables("%systemroot%\Fonts\Arial.ttf"))
privateFontCollection.AddFontFile(System.Environment.ExpandEnvironmentVariables("%systemroot%\Fonts\CourBI.ttf"))
privateFontCollection.AddFontFile(System.Environment.ExpandEnvironmentVariables("%systemroot%\Fonts\TimesBD.ttf"))
' Get the array of FontFamily objects.
fontFamilies = privateFontCollection.Families
' Process each font in the collection
For i = 0 To fontFamilies.Length - 1
' Draw the font in every style
' Regular
If fontFamilies(i).IsStyleAvailable(FontStyle.Regular) Then
location.Y += DrawFont(e.Graphics, fontFamilies(i), FontStyle.Regular, solidBrush, location, "Regular")
End If
' Bold
If fontFamilies(i).IsStyleAvailable(FontStyle.Bold) Then
location.Y += DrawFont(e.Graphics, fontFamilies(i), FontStyle.Bold, solidBrush, location, "Bold")
End If
' Italic
If fontFamilies(i).IsStyleAvailable(FontStyle.Italic) Then
location.Y += DrawFont(e.Graphics, fontFamilies(i), FontStyle.Italic, solidBrush, location, "Italic")
End If
' Bold and Italic
If fontFamilies(i).IsStyleAvailable(FontStyle.Italic) And
fontFamilies(i).IsStyleAvailable(FontStyle.Italic) Then
location.Y += DrawFont(e.Graphics, fontFamilies(i), FontStyle.Bold Or FontStyle.Italic, solidBrush, location, "BoldItalic")
End If
' Underline
If fontFamilies(i).IsStyleAvailable(FontStyle.Underline) Then
location.Y += DrawFont(e.Graphics, fontFamilies(i), FontStyle.Underline, solidBrush, location, "Underline")
End If
' Strikeout
If fontFamilies(i).IsStyleAvailable(FontStyle.Strikeout) Then
location.Y += DrawFont(e.Graphics, fontFamilies(i), FontStyle.Strikeout, solidBrush, location, "Strikeout")
End If
' Extra space between font families
location.Y += 10
Next
privateFontCollection.Dispose()
End Sub
編譯程式碼
上述範例的設計目的是要與 Windows Forms 搭配使用,而且需要 PaintEventArgse
,這是 PaintEventHandler 的參數。