如何:建立私用字型集合
類別 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, regular)
C:\ systemroot \Fonts\CourBI.tff (Courier New, bold italic)
C:\ systemroot \Fonts\TimesBd.tff (Times New Roman, bold)
程式碼會從 Families 物件的 屬性 PrivateFontCollection 擷取 物件的陣列 FontFamily 。
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 系列使用。
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 Form 使用所設計,而且需要 PaintEventArgse
,這是 PaintEventHandler 的參數。
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應