Procedura: creare testo disposto su più righe in un rettangolo

È possibile disegnare testo di cui è stato eseguito il wrapping in un rettangolo usando il DrawString metodo di overload della Graphics classe che accetta un Rectangle parametro o RectangleF . Si useranno anche un Brush oggetto e un oggetto Font.

È anche possibile disegnare testo di cui è stato eseguito il wrapping in un rettangolo usando il DrawText metodo di overload di TextRenderer che accetta un Rectangle oggetto e un TextFormatFlags parametro . Si useranno anche un Color oggetto e un oggetto Font.

La figura seguente mostra l'output del testo disegnato nel rettangolo quando si usa il DrawString metodo :

Screenshot that shows the output when using DrawString method.

Per disegnare testo incapsulato in un rettangolo con GDI+

  1. Usare il DrawString metodo di overload, passando il testo desiderato, Rectangle o RectangleFFont 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
    

Per disegnare testo incapsulato in un rettangolo con GDI

  1. Usare il TextFormatFlags valore di enumerazione per specificare che il testo deve essere sottoposto a wrapping con il DrawText metodo di overload, passando il testo desiderato, 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
    

Compilazione del codice

Gli esempi precedenti richiedono:

Vedi anche