如何:在指定的位置繪製文字

當您執行自訂繪圖時,可以在從指定點開始的單一水平線中繪製文字。 您可以使用採用 PointPointF 參數之 類別的多 Graphics 載方法,以這種方式 DrawString 繪製文字。 方法 DrawString 也需要 BrushFont

您也可以使用 DrawText 採用 的多載方法 TextRendererPointDrawTextColor也需要 和 Font

下圖顯示當您使用多載方法時,于指定點繪製的 DrawString 文字輸出。

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

使用 GDI+ 繪製文字行

  1. DrawString使用 方法,傳遞您想要的文字,或 PointFPointFontBrush

    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
    

編譯程式碼

上述範例需要:

另請參閱