1

考虑以下绘制函数(缩写):

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 对象,闪烁会消失。事实上,它没有。起初,我认为paint-method 会花费很长时间,但可能不会(我测量了每次调用需要 4-7 毫秒)。如果我设置cond为 true,它仍然会闪烁,尽管绘制方法几乎不需要时间。绘画方法将在面板上绘画并且我使用计时器大约每 50 毫秒使面板无效可能很重要。我怎样才能最终消除闪烁?

4

1 回答 1

2

只需尝试在构造函数中设置属性:

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);
    }                                          
  }
}
于 2013-03-03T13:34:23.867 回答