Gewusst wie: Erstellen einer privaten Schriftartenauflistung

Die PrivateFontCollection-Klasse erbt von der abstrakten FontCollection-Basisklasse. Mithilfe eines PrivateFontCollection-Objekts können Sie eine Gruppe von Schriftarten verwalten, die speziell für eine Anwendung verwendet werden. Eine private Schriftartensammlung kann sowohl installierte Systemschriftarten als auch Schriftarten umfassen, die nicht auf dem Computer installiert sind. Um einer privaten Schriftartenauflistung eine Schriftartdatei hinzuzufügen, rufen Sie die AddFontFile-Methode eines PrivateFontCollection-Objekts auf.

Die Families-Eigenschaft eines PrivateFontCollection-Objekts enthält ein Array von FontFamily-Objekten.

Die Anzahl der Schriftfamilien in einer privaten Schriftartensammlung muss nicht unbedingt mit der Anzahl der Schriftartdateien übereinstimmen, die der Sammlung hinzugefügt wurden. Angenommen, Sie fügen der Sammlung die Dateien ArialBd.tff, Times.tff und TimesBd.tff hinzu. Die Sammlung enthält daraufhin drei Dateien, aber nur zwei Kategorien, da Times.tff und TimesBd.tff derselben Kategorie angehören.

Beispiel

Im folgenden Beispiel werden einem PrivateFontCollection-Objekt die folgenden drei Schriftartdateien hinzugefügt:

  • C:\Systemstamm\Fonts\Arial.tff (Arial, Standard)

  • C:\Systemstamm\Fonts\CourBI.tff (Courier New, Fett kursiv)

  • C:\Systemstamm\Fonts\TimesBd.tff (Times New Roman, Fett)

Durch den Code wird ein Array von FontFamily-Objekten aus der Families-Eigenschaft des PrivateFontCollection-Objekts abgerufen.

Für jedes FontFamily-Objekt in der Auflistung ruft der Code die IsStyleAvailable-Methode auf, um festzustellen, ob verschiedene Schriftschnitte (Normal, Fett, Kursiv, Fett Kursiv, Unterstrichen und Durchgestrichen) verfügbar sind. Die an die IsStyleAvailable-Methode übergebenen Argumente sind Member der FontStyle-Enumeration.

Wenn eine bestimmte Kategorie-/Schriftschnittkombination verfügbar ist, wird anhand dieser Kategorie und dieses Schriftschnitts ein Font-Objekt erstellt. Das erste an den Font-Konstruktor übergebene Argument entspricht dem Namen der Schriftfamilie (keinem FontFamily-Objekt, wie dies bei anderen Varianten des Font-Konstruktors der Fall ist). Nach seiner Erstellung wird das Font-Objekt an die DrawString-Methode der Graphics-Klasse übergeben, damit der Kategoriename zusammen mit dem Namen des Schriftschnitts angezeigt wird.

Das Ergebnis des nachstehenden Codes ist vergleichbar mit dem Ergebnis in der folgenden Abbildung.

Schriftartentext

Die Datei Arial.tff (die der privaten Schriftartenauflistung im folgenden Codebeispiel hinzugefügt wurde) ist die Schriftartdatei für die Schriftart Arial mit dem Schriftschnitt Normal. Beachten Sie jedoch, dass das Programm für die Schriftfamilie Arial außer "Normal" mehrere andere verfügbare Schriftschnitte ausgibt. Dies liegt daran, dass GDI+ in der Lage ist, ausgehend vom Schriftschnitt Normal die Schriftschnitte Fett, Kursiv sowie Fett Kursiv zu simulieren. Außerdem kann GDI+ die Effekte Unterstrichen und Durchgestrichen vom Schriftschnitt Normal ableiten.

Entsprechend kann GDI+ ausgehend vom Schriftschnitt Fett oder Kursiv den Schriftschnitt Fett Kursiv simulieren. Das Programm gibt für die Kategorie Times "Fett Kursiv" als verfügbaren Schriftschnitt aus, obwohl TimesBd.tff (Times New Roman Fett) die einzige Times-Datei in der Sammlung ist.

        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

Kompilieren des Codes

Das vorhergehende Beispiel ist für die Verwendung mit Windows Forms konzipiert und erfordert PaintEventArgs e, einen Parameter von PaintEventHandler.

Siehe auch

Referenz

PrivateFontCollection

Weitere Ressourcen

Verwenden von Schriftarten und Text