Procedura: creare un insieme di caratteri privato

La classe PrivateFontCollection eredita dalla classe di base astratta FontCollection. È possibile utilizzare un oggetto PrivateFontCollection per conservare un insieme di caratteri specifico per l'applicazione. Un insieme di caratteri privato può includere sia caratteri di sistema installati sia caratteri non installati sul computer. Per aggiungere un file di caratteri a un insieme di caratteri privato, chiamare il metodo AddFontFile di un oggetto PrivateFontCollection.

La proprietà Families di un oggetto PrivateFontCollection include la matrice di oggetti FontFamily.

Il numero dei gruppi di caratteri in un insieme di caratteri privato non coincide necessariamente con il numero dei file di caratteri inclusi nell'insieme. Si supponga ad esempio di aggiungere i file ArialBd.tff, Times.tff e TimesBd.tff a un insieme. Nell'insieme ci saranno tre file ma solamente due gruppi, poiché Times.tff e TimesBd.tff appartengono allo stesso gruppo.

Esempio

Nell'esempio riportato di seguito vengono aggiunti i seguenti tre file di caratteri a un oggetto PrivateFontCollection:

  • C:\systemroot\Fonts\Arial.tff (Arial, normale)

  • C:\systemroot\Fonts\CourBI.tff (Courier New, grassetto corsivo)

  • C:\systemroot\Fonts\TimesBd.tff (Times New Roman, grassetto)

Viene recuperata una matrice di oggetti FontFamily dalla proprietà Families dell'oggetto PrivateFontCollection.

Per ogni oggetto FontFamily dell'insieme, viene chiamato il metodo IsStyleAvailable per determinare se gli stili normale, grassetto, corsivo, grassetto corsivo, sottolineato e barrato siano disponibili. Gli argomenti passati al metodo IsStyleAvailable sono membri dell'enumerazione FontStyle.

Se è disponibile una data combinazione stile/gruppo, viene costruito un oggetto Font utilizzando quel gruppo e quello stile. Il primo argomento passato al costruttore Font è il nome del gruppo di caratteri. Si noti che in altre variazioni del costruttore Font viene invece passato un oggetto FontFamily. L'oggetto Font costruito viene passato al metodo DrawString della classe Graphics per visualizzare il nome del gruppo insieme al nome dello stile.

Se si esegue il codice che segue, l'output sarà simile a quello mostrato nell'immagine seguente.

Testo caratteri

Arial.tff, aggiunto nell'esempio di codice seguente all'insieme di caratteri privato, è il file di caratteri per lo stile normale del carattere Arial. Si noti, tuttavia, che l'output di programma mostra numerosi stili disponibili, oltre a quello normale, per il gruppo di caratteri Arial. Questo perché in GDI+ è possibile simulare gli stili grassetto, corsivo e grassetto corsivo dallo stile normale. In GDI+ è anche possibile ottenere, a partire dallo stile normale, caratteri barrati e sottolineati.

Analogamente, in GDI+ è possibile simulare lo stile grassetto corsivo sia dallo stile grassetto sia dal corsivo. Dall'output di programma risulta evidente che lo stile grassetto corsivo è disponibile per il gruppo Times anche se TimesBd.tff (Times New Roman, grassetto) è l'unico file Times nell'insieme.

        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

Compilazione del codice

L'esempio riportato in precedenza è stato creato per essere utilizzato con Windows Form e richiede PaintEventArgs e, un parametro di PaintEventHandler.

Vedere anche

Riferimenti

PrivateFontCollection

Altre risorse

Utilizzo di tipi di carattere e testo