为什么画的矩形闪烁,如何解决
private void panel1_Paint(object sender, PaintEventArgs e)
{
Bitmap bmp = new Bitmap(300, 450);
Graphics gra = Graphics.FromImage(bmp);
// Graphics gra = this.panel1.CreateGraphics();
for (int i = 0; i < rectangles.Count; i++)
{
gra.DrawRectangle(new Pen(Color.Black, 1), rectangles[i]);
}
this.panel1.CreateGraphics().DrawImage(bmp, 0, 0);
}
private void timer1_Tick(object sender, EventArgs e)
{ j++;
y++;
Rectangle rec = new Rectangle(x, y, 30, 30);
rectangles.Add(rec);
if (rectangles.Count > j + 1)
{
rectangles.RemoveAt(j);
}
this.panel1.Invalidate();
}
如何解决?
------解决方案--------------------private void panel1_Paint(object sender, PaintEventArgs e)
///
不要用这个事件
重写OnPaint方法就可以的
C# 游戏基本是用重写的
不用调Invalidate(); 就会刷新