C#中图形的重绘问题

来源:百度知道 编辑:UC知道 时间:2024/05/25 06:09:43
我用C#编写了一个windows窗体应用程序,画了一个简单的几何图形,但是当移动窗体,或者改变窗体大小,切换到其他窗口再回到我做的程序窗口时图形就会消失,请问各位高手,如何才能进行图形的重绘,非常感谢.
请你们说仔细点,比如举个例子来说明,我是编程初学者啊.麻烦各位了.

把窗体大小锁定就不会出现这个情况了

如果你是一启动就显示你画的那些图形就可以通过重绘这个方法:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
//在此添加你绘图的那些代码;
}
如果你是通过按了Button再显示画图的,那么你可以在你绘图代码的最后加上这句代码:this.Update();试试看.

重载OnPaint方法, 在这个方法内绘画

代码:
protected override void OnPaint(PaintEventArgs e)
{
// If there is an image and it has a location,
// paint it when the Form is repainted.
base.OnPaint(e);
if(this.picture != null && this.pictureLocation != Point.Empty)
{
e.Graphics.DrawImage(this.picture, this.pictureLocation);
}
}

1.在OnPaint事件里绘制
2.重载Paint函数,在里面绘制,

Paint事件中画的所有东西都是临时的一刷新就没有
建议你使用Bitmap对象
用Picturebox载入Bitmap就可以了

在msdn里面查询onpaint即可