我有一個是被稱爲每500毫秒功能,應該刪除包含在PictureBox的舊圖紙,並通過一個新的分配繪製到圖片框
public override void onUpdate()
{
pictureBox.Image = null;
Graphics g = pictureBox.CreateGraphics();
Pen p = new Pen(System.Drawing.Color.Blue, 3);
Random rnd = new Random();
int randomInt = rnd.Next(0, 11);
g.DrawEllipse(p, new Rectangle(new Point(0,randomInt), pictureBox.Size));
p.Dispose();
g.Dispose();
return;
}
取代它不工作(沒有出現在屏幕),在調試時除外.. 而當我這樣做:
public override void onUpdate()
{
Graphics g = pictureBox.CreateGraphics();
Pen p = new Pen(System.Drawing.Color.Blue, 3);
Random rnd = new Random();
int randomInt = rnd.Next(0, 11);
g.DrawEllipse(p, new Rectangle(new Point(0,randomInt), pictureBox.Size));
p.Dispose();
g.Dispose();
System.Threading.Thread.Sleep(5000);
pictureBox.Image = null;
return;
}
圈正在繪製每5秒後,它消失了500ms的
第二個是我的邏輯,但我不明白爲什麼第一個不按我想要的方式工作..如果我刪除「pictureBox.Image = null;」行,舊的圓圈不會被刪除。
我該怎麼做,每次onUpdate()被調用時重繪圓,讓它保持直到下一次被調用?
您應該使用Paint事件中的Graphic對象,而不是CreateGraphics。你也應該使用一個計時器,而不是一個循環(我猜你就是這樣調用onUpdate的)。在tick事件中,調用'pictureBox.Invalidate();'並在繪製事件中繪製您的圖片。無需設置圖像屬性。 – LarsTech 2014-10-31 13:15:23