Windows Forms Programming
How to: Draw Wrapped Text in a Rectangle

You can draw wrapped text in a rectangle by using the DrawString overloaded method of the Graphics class that takes a Rectangle or RectangleF parameter. You will also use a Brush and a Font.

You can also draw wrapped text in a rectangle by using the DrawText overloaded method of the TextRenderer that takes a Rectangle and a TextFormatFlags parameter. You will also use a Color and a Font.

The following illustration shows the output of text drawn in the rectangle when you use the DrawString method.

Fonts Text

To draw wrapped text in a rectangle with GDI+

  • Use the DrawString overloaded method, passing the text you want, Rectangle or RectangleF, Font and Brush.

    Visual Basic
    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
    
    C#
    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));
    }
    

To draw wrapped text in a rectangle with GDI

  • Use the TextFormatFlags enumeration value to specify the text should be wrapped with the DrawText overloaded method, passing the text you want, Rectangle, Font and Color.

    Visual Basic
    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
    
    C#
    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));
    
    }
    
Compiling the Code

The previous examples require:

See Also

Tasks

Other Resources

Tags :


Page view tracker