0

我创建了一个新控件并覆盖了 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);
    }

在此处输入图像描述

问题是当我改变弹药然后所有的控制轻弹。

看起来不太好。

无论如何,让我在这条线上画的线:

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

只是在弹药变化时消失?

4

1 回答 1

3

您可以快速检查一件事情。

对于窗体,而不是控件,将DoubleBuffered属性设置为true.

这应该可以消除闪烁,但 Windows.Forms 通常不太适合用于游戏,所以不要指望帧速率很高。

于 2011-11-30T09:15:47.310 回答