Poznámka:
Přístup k této stránce vyžaduje autorizaci. Můžete se zkusit přihlásit nebo změnit adresáře.
Přístup k této stránce vyžaduje autorizaci. Můžete zkusit změnit adresáře.
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:
Kreslení zalomeného textu v obdélníku pomocí GDI+
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
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í:
-
PaintEventArgs
e, což je parametr PaintEventHandler.
Viz také
.NET Desktop feedback