共用方式為


建立私用字型集合

PrivateFontCollection 類別是從 FontCollection 抽象基底類別繼承而來。您可以使用 PrivateFontCollection 物件來保留一組專供應用程式使用的字型。私用字型集合可包括已安裝的系統字型,以及尚未安裝到電腦上的字型。若要將字型檔案加入私用字型集合中,請呼叫 PrivateFontCollection 物件的 AddFontFile 方法。

PrivateFontCollection 物件的 Families 屬性包含 FontFamily 物件的陣列。

私用字型集合中的字型家族數目不一定和已加入集合中的字型檔案數目相同。例如,假設您將 ArialBd.tff、Times.tff 和 TimesBd.tff 檔案加入集合中。雖然集合中有三個檔案,但是只有兩個家族,因為 Times.tff 和 TimesBd.tff 屬於同一個家族。

下列範例會將下列三個字型檔案加入 PrivateFontCollection 物件中:

  • C:\WINNT\Fonts\Arial.tff (Arial Regular)
  • C:\WINNT\Fonts\CourBI.tff (Courier New Bold Italic)
  • C:\WINNT\Fonts\TimesBd.tff (Times New Roman Bold)

程式碼會從 PrivateFontCollection 物件的 Families 屬性中擷取 FontFamily 物件的陣列。

對於集合中的每一個 FontFamily 物件而言,程式碼都會呼叫 IsStyleAvailable 方法,來決定是否可使用不同的樣式 (標準、粗體、斜體、粗斜體、底線和刪除線)。傳遞至 IsStyleAvailable 方法的引數是 FontStyle 列舉型別的成員。

如果指定的家族/樣式組合可供使用,便會使用該家族和樣來建構 Font 物件。傳遞至 Font 建構函式的第一個引數是字型家族名稱 (而非 Font 建構函式其他變化案例中的 FontFamily 物件)。建構完 Font 物件之後,會將它傳遞至 Graphics 類別的 DrawString 方法,將家族名稱和樣式名稱一起顯示出來。

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:\WINNT\Fonts\Arial.ttf")
privateFontCollection.AddFontFile("D:\WINNT\Fonts\CourBI.ttf")
privateFontCollection.AddFontFile("D:\WINNT\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
[C#]
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:\\WINNT\\Fonts\\Arial.ttf");
privateFontCollection.AddFontFile("D:\\WINNT\\Fonts\\CourBI.ttf");
privateFontCollection.AddFontFile("D:\\WINNT\\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

上述程式碼的輸出與下圖中所顯示的輸出類似。

Arial.tff (已在上述程式碼範例中加入私用字型集合中) 是 Arial Regular 樣式的字型檔案。但是請注意,程式輸出除了會顯示 Arial 字型家族的標準樣式之外,還會顯示其他幾種可用的樣式。這是因為 GDI+ 可根據標準樣式來模擬粗體、斜體和粗斜體樣式。GDI+ 也可根據標準樣式來產生底線和刪除線。

同樣地,GDI+ 可根據粗體樣式或斜體樣式來模擬粗斜體樣式。因此,即使 TimesBd.tff (Times New Roman Bold) 是集合中唯一的 Times 檔案,但是程式輸出仍然會顯示 Times 家族可使用粗斜體樣式。