共用方式為


HOW TO:建立私用字型集合

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 建構函式的第一個引數是字型家族名稱 (而非 Font 建構函式其他變化案例中的 FontFamily 物件)。 Font 物件建構完成後,會將它傳遞至 Graphics 類別的 DrawString 方法,將家族名稱和樣式名稱一起顯示出來。

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

字型文字

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

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

        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

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

編譯程式碼

上述範例是專為與 Windows Form 搭配使用而設計的,而且它需要 PaintEventArgs e (即 PaintEventHandler 的參數)。

請參閱

參考

PrivateFontCollection

其他資源

使用字型和文字