Observação
O acesso a essa página exige autorização. Você pode tentar entrar ou alterar diretórios.
O acesso a essa página exige autorização. Você pode tentar alterar os diretórios.
Você pode desenhar texto quebrado em uma caixa delimitadora usando o método sobrecarregado da classe que aceita um parâmetro DrawString ou Graphics. Você também usará um Brush e um Font.
Você também pode desenhar texto quebrado em um retângulo usando o método sobrecarregado de DrawText que usa um TextRenderer e um Rectangle parâmetro. Você também usará um Color e um Font.
A ilustração a seguir mostra a saída do texto desenhado no retângulo quando você usa o DrawString método:
Para desenhar texto encapsulado em um retângulo com GDI+
Use o DrawString método sobrecarregado, passando o 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 GDI
Use o valor de enumeração TextFormatFlags para especificar que o texto deve ser encapsulado com o método sobrecarregado DrawText, passando o texto desejado, assim como Rectangle, Font 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 exigem:
-
PaintEventArgs
e
, que é um parâmetro de PaintEventHandler.
Consulte também
.NET Desktop feedback