Danpe
Danpe

Reputation: 19047

override OnPaint for a Windows.Forms.Control Flickering?

I create a new Control and overided the OnPaint event:

    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

The problem is when i'm changing the Ammo then all the control flicks.

It doesn't look good.

Anyway to make the lines that i draw on this line:

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

Just disapeare when ammo is changing ?

Upvotes: 0

Views: 1185

Answers (1)

Øyvind Bråthen
Øyvind Bråthen

Reputation: 60694

One quick thing you can check.

For the form, not the control, set the DoubleBuffered property to true.

That should remove the flickering, but Windows.Forms is not very good to use for games in general, so don't expect and great frame rates.

Upvotes: 3

Related Questions