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);
}
问题是,当我改变弹药,那么所有的控制笔触。
它看起来不太好。
总之,使我得出该线路上线:
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当弹药正在发生变化?
还在闪烁.. – Danpe
你确定你把它设置在实际的窗体上,而不是某个组件?组件也有一个DoubleBuffered属性。很长一段时间,我现在与WinForms合作,但我记得有完全相同的问题,DoubleBuffered来救援(即使它影响了绘图性能很多) –
是的,我将它设置在窗体上,它没有工作,但现在我也将它设置在组件上,它的工作原理! thx :) – Danpe