覆盖OnPaint for Windows.Forms.Control闪烁?

时间:2011-11-30 09:13:13

标签: .net winforms graphics controls onpaint

我创建了一个新的Control并覆盖了OnPaint事件:

    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

问题是我正在改变Ammo然后所有的控制动作。

看起来不太好。

无论如何要画出我在这一行画的线条:

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);

当弹药发生变化时,不应该消失吗?

1 个答案:

答案 0 :(得分:3)

你可以检查一下。

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

这应该可以消除闪烁,但Windows.Forms一般不适合用于游戏,所以不要期望和高帧率。