如何:建立私用字型集合
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 建構函式的第一個引數是字型系列名稱 (不是 FontFamily 物件,就像 Font 建構函式的其他變數一樣)。 建構 Font 物件之後,它會傳遞至 Graphics 類別的 DrawString 方法,以顯示系列名稱以及樣式的名稱。
下列程式碼的輸出類似於下圖所示的輸出:
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 Forms 搭配使用,而且需要 PaintEventArgse
,這是 PaintEventHandler 的參數。