override OnPaint for a Windows.Forms.Control Flickering?

Posted by Danpe on Stack Overflow See other posts from Stack Overflow or by Danpe
Published on 2011-11-30T09:13:12Z Indexed on 2011/11/30 9:51 UTC
Read the original article Hit count: 260

Filed under:
|
|
|

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 ?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about graphics