Comment : définir des taquets de tabulation dans du texte dessiné

Vous pouvez définir des taquets de tabulation pour le texte en appelant la méthode SetTabStops d'un objet StringFormat et en passant ensuite cet objet StringFormat à la méthode DrawString de la classe Graphics.

Notes

Le System.Windows.Forms.TextRenderer ne prend pas en charge l'ajout de taquets de tabulation au texte dessiné, bien que vous puissiez développer les taquets de tabulation existants à l'aide de l'indicateur TextFormatFlags.ExpandTabs.

Exemple

L'exemple suivant définit des taquets de tabulation à 150, 250 et 350. Le code affiche ensuite une liste d'onglets de noms et de notes d'examens.

L'illustration suivante montre le texte tabulé.

Polices du texte

Le code suivant passe deux arguments à la méthode SetTabStops. Le deuxième argument est un tableau qui contient les offsets de tabulation. Le premier argument passé à SetTabStops est 0, ce qui indique que le premier offset dans le tableau est mesuré à partir de la position 0, le bord gauche du rectangle englobant.

        Dim myText As String = _
           "Name" & ControlChars.Tab & _
           "Test 1" & ControlChars.Tab & _
           "Test 2" & ControlChars.Tab & _
           "Test 3" & ControlChars.Cr

        myText = myText & "Joe" & ControlChars.Tab & _
                          "95" & ControlChars.Tab & _
                          "88" & ControlChars.Tab & _
                          "91" & ControlChars.Cr
        myText = myText & "Mary" & ControlChars.Tab & _
                          "98" & ControlChars.Tab & _
                          "84" & ControlChars.Tab & _
                          "90" & ControlChars.Cr
        myText = myText & "Sam" & ControlChars.Tab & _
                          "42" & ControlChars.Tab & _
                          "76" & ControlChars.Tab & _
                          "98" & ControlChars.Cr
        myText = myText & "Jane" & ControlChars.Tab & _
                          "65" & ControlChars.Tab & _
                          "73" & ControlChars.Tab & _
                          "92" & ControlChars.Cr

        Dim fontFamily As New FontFamily("Courier New")
        Dim font As New Font( _
           fontFamily, _
           12, _
           FontStyle.Regular, _
           GraphicsUnit.Point)
        Dim rect As New Rectangle(10, 10, 450, 100)
        Dim stringFormat As New StringFormat()
        Dim solidBrush As New SolidBrush(Color.FromArgb(255, 0, 0, 255))
        Dim tabs As Single() = {150, 100, 100, 100}

        stringFormat.SetTabStops(0, tabs)

        e.Graphics.DrawString(myText, font, solidBrush, RectangleF.op_implicit(rect), stringFormat)

        Dim pen As Pen = Pens.Black
        e.Graphics.DrawRectangle(pen, rect)

string text = "Name\tTest 1\tTest 2\tTest 3\n";
text = text + "Joe\t95\t88\t91\n";
text = text + "Mary\t98\t84\t90\n";
text = text + "Sam\t42\t76\t98\n";
text = text + "Jane\t65\t73\t92\n";

FontFamily fontFamily = new FontFamily("Courier New");
Font font = new Font(
   fontFamily,
   12,
   FontStyle.Regular,
   GraphicsUnit.Point);
Rectangle rect = new Rectangle(10, 10, 450, 100);
StringFormat stringFormat = new StringFormat();
SolidBrush solidBrush = new SolidBrush(Color.FromArgb(255, 0, 0, 255));
float[] tabs = { 150, 100, 100, 100 };

stringFormat.SetTabStops(0, tabs);

e.Graphics.DrawString(text, font, solidBrush, rect, stringFormat);

Pen pen = Pens.Black;
e.Graphics.DrawRectangle(pen, rect);

Compilation du code

Voir aussi

Tâches

Comment : écrire du texte avec GDI

Autres ressources

Utilisation de polices et de texte