2011-11-30 77 views
0

我创建了一个新的控制和overided OnPaint事件:重写OnPaint for Windows.Forms.Control闪烁?

protected override void OnPaint(PaintEventArgs e) 
    { 
     Graphics g = e.Graphics; 
     _ammo = PitControl.Ammo; 
     var AmmoSize = g.MeasureString(_ammo.ToString(), Properties.Settings.Default.AmmoFont).ToSize(); 
     g.DrawString(_ammo.ToString(), Properties.Settings.Default.AmmoFont, Brushes.WhiteSmoke, Ammo.Location.X - 1, Ammo.Location.Y + Ammo.Height/2 - AmmoSize.Height/2 + 1); 
     Rectangle DrawAmmo = new Rectangle(this.Width - Ammo.Height - _margin, Ammo.Location.Y, Ammo.Height, Ammo.Height); 
     for (int i = _ammo; i > 0; i--) 
      if (i % 2 == 0) 
       g.DrawLine(_ammoPen, Ammo.Location.X + Ammo.Width - i - 1, Ammo.Location.Y + 3, Ammo.Location.X + Ammo.Width - i - 1, Ammo.Location.Y + Ammo.Height - 3); 
     g.DrawRectangle(Pens.Orange, Ammo); 
     g.DrawImage(Properties.Resources.ammunition, DrawAmmo.Location.X, DrawAmmo.Location.Y, DrawAmmo.Height, DrawAmmo.Height); 
    } 

enter image description here

问题是,当我改变弹药,那么所有的控制笔触。

它看起来不太好。

总之,使我得出该线路上线:

g.DrawLine(_ammoPen, Ammo.Location.X + Ammo.Width - i - 1, Ammo.Location.Y + 3, Ammo.Location.X + Ammo.Width - i - 1, Ammo.Location.Y + Ammo.Height - 3); 

就disapeare当弹药正在发生变化?

回答

3

您可以快速检查一下。

对于表单而不是控件,请将DoubleBuffered属性设置为true

这应该消除闪烁,但Windows.Forms不是很好用于一般的游戏,所以不要指望和伟大的帧率。

+0

还在闪烁.. – Danpe

+0

你确定你把它设置在实际的窗体上,而不是某个组件?组件也有一个DoubleBuffered属性。很长一段时间,我现在与WinForms合作,但我记得有完全相同的问题,DoubleBuffered来救援(即使它影响了绘图性能很多) –

+0

是的,我将它设置在窗体上,它没有工作,但现在我也将它设置在组件上,它的工作原理! thx :) – Danpe