如何:在指定位置绘制文本

执行自定义绘制时,可以在从指定点开始的单条水平线中绘制文本。 可以使用 Graphics 类的 DrawString 重载方法以这种方式绘制文本,该类采用 PointPointF 参数。 DrawString 方法还需要 BrushFont

还可以使用采用 PointTextRendererDrawText 重载方法。 DrawText 还需要 ColorFont

下图显示了使用 DrawString 重载方法时在指定点绘制的文本输出。

Screenshot that shows the output of text at a specified point.

使用 GDI+ 绘制文本行

  1. 使用 DrawString 方法,传递所需的文本、PointPointFFontBrush

    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 绘制文本行

  1. 使用 DrawText 方法,传递所需的文本、PointFontColor

    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
    

编译代码

前面的示例需要:

另请参阅