如何:在矩形中绘制换行文本
1个回答
展开全部
Visual Basic Dim text1 AsString = "Draw text in a rectangle by passing a RectF to the DrawString method."Dim font1 AsNew Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point) TryDim rectF1 AsNew RectangleF(30, 10, 100, 122) e.Graphics.DrawString(text1, font1, Brushes.Blue, rectF1) e.Graphics.DrawRectangle(Pens.Black, Rectangle.Round(rectF1)) Finally font1.Dispose() EndTry C# 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)); } 用GDI 在矩形中绘制换行文本使用TextFormatFlags 枚举值指定应通过 DrawText 重载方法换行的文本,使用时传入您需要的文本、Rectangle、Font 以及Color。 Visual Basic Dim text2 AsString = _ "Draw text in a rectangle by passing a RectF to the DrawString method."Dim font2 AsNew Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point) TryDim rect2 AsNew 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() EndTry C# 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)); } 编译代码前面的示例需要:PaintEventArgse,它是 PaintEventHandler 的一个参数。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询