.net 是否覆盖Windows.Forms.Control的OnPaint闪烁?

.net 是否覆盖Windows.Forms.Control的OnPaint闪烁?,.net,winforms,graphics,controls,onpaint,.net,Winforms,Graphics,Controls,Onpaint,我创建了一个新控件并跳过了OnPaint事件: protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; _ammo = PitControl.Ammo; var AmmoSize = g.MeasureString(_ammo.ToString(), Properties.Settings.Default.AmmoFont).ToSiz

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

只要在弹药更换时消失就行了?

一件你可以快速检查的事情

对于窗体,而不是控件,将
双缓冲
属性设置为
true


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

一件你可以快速检查的事情

对于窗体,而不是控件,将
双缓冲
属性设置为
true


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

您确定是在实际窗体上设置的,而不是在某些组件上设置的吗?组件也具有双缓冲属性。我已经很久没有使用WinForms了,但我记得我遇到了完全相同的问题,Double Buffered(即使它对绘图性能有很大影响)是的,我在窗体上设置了它,但它不工作,但现在我在组件上也设置了它,它工作了!thx:)很高兴听到它最终成功了:)祝你的游戏好运。你确定你把它设置为实际的形式,而不是某个组件?组件也具有双缓冲属性。我已经很久没有使用WinForms了,但我记得我遇到了完全相同的问题,Double Buffered(即使它对绘图性能有很大影响)是的,我在窗体上设置了它,但它不工作,但现在我在组件上也设置了它,它工作了!thx:)很高兴听到它最终成功了:)祝你的比赛好运。