必须把画图实现代码放在窗体的Paint的事件中,或者将那些代码放在一个方法中,然后Paint事件调用那个方法。如下
private void Form1_Paint(object sender, PaintEventArgs e)
{
Pen pen;
Point p1, p2;
Graphics gdi;
pen = Pens.Red;
gdi = this.CreateGraphics();
p1 = new Point(100, 100);
p2 = new Point(500, 100);
gdi.DrawLine(pen, p1, p2);
}
或者下述方法也能实现该功能:
private void Form1_Paint(object sender, PaintEventArgs e)
{this.ExecDrawLines();
}
public void ExecDrawLines()
{Pen pen;
Point p1, p2;
Graphics gdi;
pen = Pens.Red;
gdi = this.CreateGraphics();
p1 = new Point(100, 100);
p2 = new Point(500, 100);
gdi.DrawLine(pen, p1, p2);
}
window窗体中画图形
最新推荐文章于 2022-06-25 10:10:26 发布