Sdílet prostřednictvím


Postupy: Kreslení zalomeného textu do obdélníku

Zalomený text v obdélníku můžete nakreslit pomocí DrawString přetížené metody Graphics třídy, která přebírá Rectangle nebo RectangleF parametr. Použijete také a BrushFont.

Zalomený text v obdélníku můžete také nakreslit pomocí DrawText přetížené metody TextRenderer , která přebírá Rectangle a TextFormatFlags parametr. Použijete také a ColorFont.

Následující obrázek znázorňuje výstup textu nakresleného v obdélníku při použití DrawString metody:

Snímek obrazovky znázorňující výstup při použití metody DrawString

Kreslení zalomeného textu v obdélníku pomocí GDI+

  1. Použijte přetíženou metoduDrawString, předáte požadovaný Rectangle text nebo RectangleFFonta 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
    

Kreslení zalomeného textu v obdélníku s GDI

  1. Pomocí hodnoty výčtu TextFormatFlags zadejte text by měl být zalomen DrawText přetíženou metodou, předáním požadovaného RectangleFont textu , a 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
    

Kompilace kódu

Předchozí příklady vyžadují:

Viz také