请教 C# 高手 关于 画图 刷新 的问题

2025-02-22 15:39:51
推荐回答(3个)
回答1:

不要直接在窗体上绘制,这样效果不好,最好是绘制在PictureBox上,尽量较少绘制次数闪烁效果会好很多,代码如下 10*10个格 100毫秒的速度 无闪烁

PictureBox pictureBox;
Rectangle rect = new Rectangle(0, 0, 30, 30);
Bitmap bit = new Bitmap(300, 300);
int recs = 1;
private void button1_Click(object sender, EventArgs e)
{
pictureBox = new PictureBox();
pictureBox.Size = new Size(320,320);
pictureBox.Location = new Point(30,30);
this.Controls.Add(pictureBox);
pictureBox.Paint += new PaintEventHandler(pictureBox_Paint);
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
timer.Interval = 100;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
void pictureBox_Paint(object sender, PaintEventArgs e)
{
for (int i = 0; i <= 10; i++)
{
e.Graphics.DrawLine(Pens.Black, 0, i * 30, 300, i * 30);
e.Graphics.DrawLine(Pens.Black, i * 30, 0, i * 30, 300);
}
e.Graphics.DrawImage(bit, 0, 0);
}
void timer_Tick(object sender, EventArgs e)
{
DrawImage();
pictureBox.Invalidate();
recs++;
}
void DrawImage()
{
Graphics g = Graphics.FromImage(bit);
g.Clear(Color.Transparent);
rect = new Rectangle(0, 0, 30, 30);
for (int i = 0; i < recs; i++)
{
g.FillRectangle(Brushes.Black, rect);

if (i % 10 == 0 && i != 0)
{
rect.Y += 30;
rect.X = 0;
}
else
rect.X = i % 10 * 30;
}
g.Dispose();
}

回答2:

"注意不要每填一个格都是将先前填好的重画,最好不要总是刷新"关于这个问题很容易解决。使用重绘的标准方法Control.Invalidate(Region)而不是用Control.Invalidate(),因为第二个会导致整个画面重绘,第一个只会重绘你定义的region(既是每一个空格)

我上面的做法就是可以做到 第一次,画1格
第二次,画2格
第三次,画3格
你体会一下。

回答3:

其实重画很快的,应该不会出现你说的闪烁很厉害的情况,可以参考一下我空间里的几个例子,应该和你的差不多