Como desenhar texto com quebras automáticas de linha em um retângulo

Você pode desenhar texto quebrado em um retângulo usando o DrawString método sobrecarregado da Graphics classe que usa um Rectangle parâmetro ou RectangleF . Você também usará um e um BrushFont.

Você também pode desenhar texto quebrado em um retângulo usando o DrawTextTextRenderer método sobrecarregado do que usa um e um RectangleTextFormatFlags parâmetro. Você também usará um e um ColorFont.

A ilustração a seguir mostra a saída do texto desenhado no retângulo quando você usa o DrawString método:

Screenshot that shows the output when using DrawString method.

Para desenhar texto encapsulado em um retângulo com o GDI+

  1. Use o método sobrecarregado, passando o DrawString texto desejado Rectangle ou RectangleF, Font e Brush.

    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));
    }
    
    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
    

Para desenhar texto encapsulado em um retângulo com o GDI

  1. Use o valor de enumeração para especificar que o texto deve ser quebrado com o método sobrecarregado, passando o TextFormatFlagsDrawText texto desejado RectangleFont e Color.

    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));
    }
    
    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
    

Compilando o código

Os exemplos anteriores requerem:

Confira também