考虑下面的paint函数(简化):
public void paint(object sender, PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
BufferedGraphicsContext context = BufferedGraphicsManager.Current;
BufferedGraphics buffer = context.Allocate(g, e.ClipRectangle);
buffer.Graphics.Clear(Color.PaleVioletRed);
// skip drawing if cond is true (condition is not relevant)
if(!cond)
{
try
{
// l is some List<p>
foreach(Point p in l)
{
// ...calculate X and Y... (not relevant)
buffer.Graphics.FillEllipse(new SolidBrush(Color.Blue), p.X,p.Y, Point.SIZE,Point.SIZE);
}
}
catch {} // some exception handling (not relevant)
finally{
buffer.Render(g);
}
}
buffer.Render(g);
}请注意,上面的代码或多或少是伪代码。我希望使用BufferedGraphics对象,闪烁就会消失。一开始,我认为这个画图方法会花很长的时间,但事实大概并非如此(每次调用我都测量了4-7毫秒)。如果我将cond设置为true,它仍然闪烁,尽管画图方法几乎不需要时间。很重要的一点是,paint-method将在面板上绘制,并且我使用计时器使面板大约每50ms无效一次。我如何才能最终消除闪烁?
发布于 2013-03-03 21:34:24
只需尝试在构造函数中设置该属性:
this.DoubleBuffered = true;那么你就不需要BufferedGraphics的东西了:
public void paint(object sender, PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
g.Clear(Color.PaleVioletRed);
// skip drawing if cond is true (condition is not relevant)
if(!cond)
{
// l is some List<p>
foreach(Point p in l)
{
// ...calculate X and Y... (not relevant)
g.FillEllipse(new SolidBrush(Color.Blue), p.X,p.Y, Point.SIZE,Point.SIZE);
}
}
}https://stackoverflow.com/questions/15186014
复制相似问题