Share via


方法 : Windows フォームに直線を描画する

更新 : 2007 年 11 月

この例は、フォームに直線を描画します。

使用例

Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red)
Dim formGraphics As System.Drawing.Graphics
formGraphics = Me.CreateGraphics()
formGraphics.DrawLine(myPen, 0, 0, 200, 200)
myPen.Dispose()
formGraphics.Dispose()
System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.DrawLine(myPen, 0, 0, 200, 200);
myPen.Dispose();
formGraphics.Dispose();
System::Drawing::Pen^ myPen =
    gcnew System::Drawing::Pen(System::Drawing::Color::Red);
System::Drawing::Graphics^ formGraphics;
formGraphics = this->CreateGraphics();
formGraphics->DrawLine(myPen, 0, 0, 200, 200);
delete myPen;
delete formGraphics;

コードのコンパイル方法

Load イベント ハンドラでこのメソッドを呼び出すことはできません。フォームがサイズ変更された場合、または別のフォームによって隠れている場合、描画済みのコンテンツは再描画されません。コンテンツを自動的に再描画するには、OnPaint メソッドをオーバーライドする必要があります。

堅牢性の高いプログラム

システム リソースを消費するオブジェクト (Pen オブジェクトや Graphics オブジェクトなど) では、必ず Dispose を呼び出す必要があります。

参照

参照

DrawLine

OnPaint

その他の技術情報

グラフィックス プログラミングについて

ペンを使用した直線と図形の描画

Windows フォームにおけるグラフィックスと描画