方法: テキストを指定の位置に描画する

カスタムの描画を実行すると、指定した点から始まる 1 本の水平線にテキストを描画できます。 この方法でテキストを描画するには、Point または PointF のパラメーターを受け取る Graphics クラスの DrawString オーバーロード メソッドを使用します。 DrawString メソッドには BrushFont も必要です。

Point を受け取る TextRendererDrawText オーバーロード メソッドも使用できます。 DrawText には ColorFont も必要です。

次の図は、DrawString オーバーロード メソッドを使用した場合に、指定した点にテキストが描画された出力を示しています。

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

GDI+ を使用してテキストの直線を描画するには

  1. DrawString メソッドを使用し、目的のテキスト、Point または PointFFont、および 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 を使用してテキストの直線を描画するには

  1. DrawText メソッドを使用し、目的のテキスト、PointFont、および 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
    

コードのコンパイル

前の例では、以下が必要です。

関連項目