我在Windows窗体上绘制一个小迷宫,并在每次击键事件时重新绘制它。如何使它更平滑?该图的时间约为一秒钟,当您输入箭头按钮时,您会在表单上看到短暂的闪烁。
public void Draw_Labyrinth()
{
this.Update();
this.SuspendLayout();
Controls.Clear();
for (int i = 0; i < list_Elements_Y.Count; i++)
{
for (int j = 0; j < list_Elements_Y[i].list_Elements_X.Count; j++)
{
PictureBox pb = new PictureBox();
pb.Location = list_Elements_Y[i].list_Elements_X[j].position;
pb.Name = list_Elements_Y[i].list_Elements_X[j].name;
pb.Size = list_Elements_Y[i].list_Elements_X[j].size;
if (pb.Location == characterPosition)
{
pb.Image = Properties.Resources.character;
}
else if (pb.Name == "item")
{
pb.Image = Properties.Resources.item;
}
else if (pb.Name == "wall")
{
pb.Image = Properties.Resources.wall;
}
Controls.Add(pb);
Application.DoEvents();
this.Update();
}
}
Application.DoEvents();
this.ResumeLayout();
}
非常感谢!
通常可以通过将DoubleBuffered
设置为true
来解决(或大大改善)WinForms中的闪烁。
这可以在VS Designer或Form
构造函数中完成。
整整一秒钟的绘图操作似乎过多。因此,如果可以优化这一点,将是一个好主意。