Comment : aligner le texte écrit

En effectuant un dessin personnalisé, il est probable que vous souhaitiez centrer le texte dessiné sur un formulaire ou un contrôle. Vous pouvez facilement aligner le texte dessiné avec les méthodes DrawString ou DrawText en créant l'objet de mise en forme voulu et en définissant les indicateurs de format appropriés.

Pour dessiner un texte centré avec GDI+ (DrawString)

  • Utilisez un StringFormat avec la méthode DrawString appropriée pour spécifier le texte centré.

    Dim text1 As String = "Use StringFormat and Rectangle objects to" & _
        " center text in a rectangle."
    Dim font1 As New Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point)
    Try
        Dim rect1 As New Rectangle(10, 10, 130, 140)
    
        ' Create a StringFormat object with the each line of text, and the block
        ' of text centered on the page.
        Dim stringFormat As New StringFormat()
        stringFormat.Alignment = StringAlignment.Center
        stringFormat.LineAlignment = StringAlignment.Center
    
        ' Draw the text and the surrounding rectangle.
        e.Graphics.DrawString(text1, font1, Brushes.Blue, rect1, stringFormat)
        e.Graphics.DrawRectangle(Pens.Black, rect1)
    Finally
        font1.Dispose()
    End Try
    
    string text1 = "Use StringFormat and Rectangle objects to" 
        + " center text in a rectangle.";
    using (Font font1 = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point))
    {
        Rectangle rect1 = new Rectangle(10, 10, 130, 140);
    
        // Create a StringFormat object with the each line of text, and the block
        // of text centered on the page.
        StringFormat stringFormat = new StringFormat();
        stringFormat.Alignment = StringAlignment.Center;
        stringFormat.LineAlignment = StringAlignment.Center;
    
        // Draw the text and the surrounding rectangle.
        e.Graphics.DrawString(text1, font1, Brushes.Blue, rect1, stringFormat);
        e.Graphics.DrawRectangle(Pens.Black, rect1);
    }
    

Pour dessiner un texte centré avec GDI (DrawText)

  • Utilisez l'énumération TextFormatFlags pour habiller ainsi que centrer verticalement et horizontalement le texte avec la méthode DrawText appropriée.

    Dim text2 As String = "Use TextFormatFlags and Rectangle objects to" & _
            " center text in a rectangle."
    
    Dim font2 As New Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point)
    Try
        Dim rect2 As New Rectangle(150, 10, 130, 140)
    
        ' Create a TextFormatFlags with word wrapping, horizontal center and
        ' vertical center specified.
        Dim flags As TextFormatFlags = TextFormatFlags.HorizontalCenter Or _
            TextFormatFlags.VerticalCenter Or TextFormatFlags.WordBreak
    
        ' Draw the text and the surrounding rectangle.
        TextRenderer.DrawText(e.Graphics, text2, font2, rect2, Color.Blue, flags)
        e.Graphics.DrawRectangle(Pens.Black, rect2)
    Finally
        font2.Dispose()
    End Try
    
    string text2 = "Use TextFormatFlags and Rectangle objects to"
     + " center text in a rectangle.";
    
    using (Font font2 = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point))
    {
        Rectangle rect2 = new Rectangle(150, 10, 130, 140);
    
        // Create a TextFormatFlags with word wrapping, horizontal center and
        // vertical center specified.
        TextFormatFlags flags = TextFormatFlags.HorizontalCenter |
            TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak;
    
        // Draw the text and the surrounding rectangle.
        TextRenderer.DrawText(e.Graphics, text2, font2, rect2, Color.Blue, flags);
        e.Graphics.DrawRectangle(Pens.Black, rect2);
    }
    

Compilation du code

Les exemples de code précédents sont conçus pour être utilisés avec Windows Forms, et ils nécessitent PaintEventArgs e, qui est un paramètre de PaintEventHandler.

Voir aussi

Tâches

Comment : écrire du texte avec GDI

Comment : construire des familles de polices et des polices

Autres ressources

Utilisation de polices et de texte