执行自定义绘图时,可以在从指定点开始的单个水平线中绘制文本。 可以使用DrawString类中接受Graphics或Point参数的PointF重载方法以这种方式绘制文本。 该方法 DrawString 同样需要 Brush 和 Font
可以使用DrawText的重载方法TextRenderer,该方法接受Point。 DrawText 还需要 a Color 和 a Font.
下图显示了使用 DrawString 重载方法时在指定点绘制的文本输出。
使用 GDI+ 绘制一行文字
使用该方法 DrawString ,传递所需的文本, Point 或者 PointF, Font以及 Brush。
using (Font font1 = new Font("Times New Roman", 24, FontStyle.Bold, GraphicsUnit.Pixel)){ PointF pointF1 = new PointF(30, 10); e.Graphics.DrawString("Hello", font1, Brushes.Blue, pointF1); }
Dim font1 As New Font("Times New Roman", 24, FontStyle.Bold, GraphicsUnit.Pixel) Try Dim pointF1 As New PointF(30, 10) e.Graphics.DrawString("Hello", font1, Brushes.Blue, pointF1) Finally font1.Dispose() End Try
使用 GDI 绘制一行文本
使用DrawText方法,传递您想要的文本、Point、Font以及Color。
using (Font font = new Font("Times New Roman", 24, FontStyle.Bold, GraphicsUnit.Pixel)) { Point point1 = new Point(30, 10); TextRenderer.DrawText(e.Graphics, "Hello", font, point1, Color.Blue); }
Dim font As New Font("Times New Roman", 24, FontStyle.Bold, GraphicsUnit.Pixel) Try Dim point1 As New Point(30, 10) TextRenderer.DrawText(e.Graphics, "Hello", font, point1, Color.Blue) Finally font.Dispose() End Try
编译代码
前面的示例需要:
-
PaintEventArgs
e
,它是 PaintEventHandler 的一个参数。