窗口重画问题求教(invalidate函数)
在button2 事件中,怎么不能用invalidate()啊,用了,在画别的就画不出来了;
请同志们帮帮忙啊,
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g=e.Graphics;
g.FillRectangle(Brushes.GreenYellow,60,60,100,100);
}
private void button1_Click(object sender, System.EventArgs e)
{
Graphics g1;
g1=this.CreateGraphics();
g1.FillRectangle(Brushes.Brown,70,70,20,20);
}
private void button2_Click(object sender, System.EventArgs e)
{
this.Invalidate();//////???????加上为什么旧不显示下边的内容了啊
Graphics g;
g=this.CreateGraphics();
g.FillRectangle(Brushes.Blue,100,100,20,20);
}
------解决方案--------------------Invalidate并不是重绘,而是发了消息通知窗体需要重绘,等你当前函数执行完毕,才会去响应这个重绘的请求的。比较好的方法是手工调用OnPaint,自己构造一个PaintEventArg对象。最后记得Dispose