Partager via


Comment : écrire du texte renvoyé à la ligne dans un rectangle

Mise à jour : novembre 2007

Vous pouvez dessiner du texte renvoyé à la ligne dans un rectangle en utilisant la méthode surchargée DrawString de la classe Graphics qui utilise un paramètre Rectangle ou RectangleF. Vous pouvez également utiliser Brush et Font.

Vous pouvez aussi dessiner du texte renvoyé à la ligne dans un rectangle en utilisant la méthode surchargée DrawText de TextRenderer qui utilise les paramètres Rectangle et TextFormatFlags. Vous pouvez également utiliser Color et Font.

L'illustration suivante affiche la sortie du texte dessiné dans le rectangle lorsque vous utilisez la méthode DrawString.

Polices du texte

Pour dessiner du texte renvoyé à la ligne dans un rectangle avec GDI+

  • Utilisez la méthode surchargée DrawString, en passant le texte que vous voulez, Rectangle ou RectangleF, Font et Brush.

    Dim text1 As String = "Draw text in a rectangle by passing a RectF to the DrawString method."
    Dim font1 As New Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point)
    Try
        Dim rectF1 As New RectangleF(30, 10, 100, 122)
        e.Graphics.DrawString(text1, font1, Brushes.Blue, rectF1)
        e.Graphics.DrawRectangle(Pens.Black, Rectangle.Round(rectF1))
    Finally
        font1.Dispose()
    End Try
    
    string text1 = "Draw text in a rectangle by passing a RectF to the DrawString method.";
    using (Font font1 = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point))
    {
        RectangleF rectF1 = new RectangleF(30, 10, 100, 122);
        e.Graphics.DrawString(text1, font1, Brushes.Blue, rectF1);
        e.Graphics.DrawRectangle(Pens.Black, Rectangle.Round(rectF1));
    }
    

Pour dessiner du texte renvoyé à la ligne dans un rectangle avec GDI

  • Utilisez la valeur d'énumération TextFormatFlags pour spécifier que le texte doit être renvoyé à la ligne avec la méthode surchargée DrawText, en passant le texte que vous voulez, Rectangle, Font et Color.

    Dim text2 As String = _
        "Draw text in a rectangle by passing a RectF to the DrawString method."
    Dim font2 As New Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point)
    Try
        Dim rect2 As New Rectangle(30, 10, 100, 122)
    
        ' Specify the text is wrapped.
        Dim flags As TextFormatFlags = TextFormatFlags.WordBreak
        TextRenderer.DrawText(e.Graphics, text2, font2, rect2, Color.Blue, flags)
        e.Graphics.DrawRectangle(Pens.Black, Rectangle.Round(rect2))
    Finally
        font2.Dispose()
    End Try
    
    string text2 = "Draw text in a rectangle by passing a RectF to the DrawString method.";
    using (Font font2 = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point))
    {
        Rectangle rect2 = new Rectangle(30, 10, 100, 122);
    
        // Specify the text is wrapped.
        TextFormatFlags flags = TextFormatFlags.WordBreak;
        TextRenderer.DrawText(e.Graphics, text2, font2, rect2, Color.Blue, flags);
        e.Graphics.DrawRectangle(Pens.Black, Rectangle.Round(rect2));
    
    }
    

Compilation du code

Les exemples précédents nécessitent :

Voir aussi

Tâches

Comment : écrire du texte avec GDI

Comment : construire des familles de polices et des polices

Comment : écrire du texte à un emplacement spécifié

Autres ressources

Utilisation de polices et de texte