Compartilhar via


Como: Desenhar texto disposto em um retângulo

Você pode desenhar texto disposto em um retângulo usando o DrawString método sobrecarregado o Graphics classe que leva uma Rectangle ou RectangleF parâmetro. Você também usará um Brush e um Font.

Você também pode desenhar texto disposto em um retângulo usando o DrawText método sobrecarregado o TextRenderer que leva uma Rectangle e um TextFormatFlags parâmetro. Você também usará um Color e um Font.

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

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

  • Use o DrawString sobrecarga de método, passando o texto desejado, Rectangle ou RectangleF, Font e 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));
    }
    

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

  • Use o TextFormatFlags valor de enumeração para especificar o texto deve ser empacotado com o DrawText sobrecarga de método, passando o texto desejado, Rectangle, Font e 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));
    
    }
    

Compilando o código

Os exemplos anteriores requerem:

Consulte também

Tarefas

Como: Desenhar texto com GDI

Como: Construir fonte famílias e fonte s

Como: Desenhar texto em um local especificado

Outros recursos

Usando fontes e texto