我画了一个矩形,休眠了几毫秒--然后我想清除这个矩形,但我不知道怎么做。(矩形位于图形上,所以我不能简单地用另一个矩形来覆盖它)
graphics.DrawRectangle(p, innerRectangle)
System.Threading.Thread.Sleep(75)
Next I want to clear the rectange...发布于 2009-06-11 19:30:30
您需要重新绘制图形(或至少在矩形下的部分)。如果这是一个图片框或类似的东西,使用Invaldiate()强制重绘。
发布于 2009-06-11 19:30:47
我猜在绘制矩形之前,将原始数据从表面复制到临时位图中,然后再将位图绘制回原位应该是可行的。
更新
已经有了一个公认的答案,但我想我可以分享一个代码样本。这个函数在给定的控件上用红色绘制给定的矩形,并在500ms后恢复该区域。
public void ShowRectangleBriefly(Control ctl, Rectangle rect)
{
Image toRestore = DrawRectangle(ctl, rect);
ThreadPool.QueueUserWorkItem((WaitCallback)delegate
{
Thread.Sleep(500);
this.Invoke(new Action<Control, Rectangle, Image>(RestoreBackground), ctl, rect, toRestore);
});
}
private void RestoreBackground(Control ctl, Rectangle rect, Image image)
{
using (Graphics g = ctl.CreateGraphics())
{
g.DrawImage(image, rect.Top, rect.Left, image.Width, image.Height);
}
image.Dispose();
}
private Image DrawRectangle(Control ctl, Rectangle rect)
{
Bitmap tempBmp = new Bitmap(rect.Width + 1, rect.Height + 1);
using (Graphics g = Graphics.FromImage(tempBmp))
{
g.CopyFromScreen(ctl.PointToScreen(new Point(rect.Top, rect.Left)), new Point(0, 0), tempBmp.Size);
}
using (Graphics g = this.CreateGraphics())
{
g.DrawRectangle(Pens.Red, rect);
}
return tempBmp;
}发布于 2009-06-11 19:31:13
如果矩形完全覆盖在图形上,您应该能够只重画或刷新底层图形。如果不是,则需要使用背景色重新绘制矩形,然后刷新基础图形。
https://stackoverflow.com/questions/983109
复制相似问题