Double buffering C #

I am trying to implement the following method: void Ball :: DrawOn (Graphics g);

The method should display all previous locations (stored in the queue) of the ball and, finally, the current location. I don't know if this matters, but I print the previous locations using g.DrawEllipse (...) and the current location using g.FillEllipse (...).

The question is, as you might imagine, there is a lot of drawing, and thus the display starts to flicker. I was looking for a way to double the buffer, but all I could find were two ways:

1) System.Windows.Forms.Control.DoubleBuffered = true;

2) SetStyle (ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);

when I try to use the first one, I get an error message explaining that the DoubleBuffered property is not available from this method due to its level of protection. Although I can’t figure out how to use the SetStyle method.

Is it possible to use a double buffer at all , and I have all the access to the graphic object that I get as input in the method?

Thanks Advance,

Edit: I created the following class

namespace doubleBuffer
{
    class BufferedBall : System.Windows.Forms.Form{
        private Ball ball;
        public BufferedBall(Ball ball)
        {
            this.ball = ball;
        }

    public void DrawOn(Graphics g){
        this.DoubleBuffered = true;
        int num = 0;
        Rectangle drawArea1 = new Rectangle(5, 35, 30, 100);
        LinearGradientBrush linearBrush1 =
        new LinearGradientBrush(drawArea1, Color.Green, Color.Orange, LinearGradientMode.Horizontal);
        Rectangle drawArea2 = new Rectangle(5, 35, 30, 100);
        LinearGradientBrush linearBrush2 =
           new LinearGradientBrush(drawArea2, Color.Black, Color.Red, LinearGradientMode.Vertical);
        foreach (PointD point in ball.previousLocations)
        {
            Pen myPen1;
            if (num % 3 == 0)
                myPen1 = new Pen(Color.Yellow, 1F);
            else if (num % 3 == 1)
                myPen1 = new Pen(Color.Green, 2F);
            else
                myPen1 = new Pen(Color.Red, 3F);
            num++;
            myPen1.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
            myPen1.StartCap = System.Drawing.Drawing2D.LineCap.RoundAnchor;
            myPen1.EndCap = System.Drawing.Drawing2D.LineCap.AnchorMask;
            g.DrawEllipse(myPen1, (float)(point.X - ball.radius), (float)(point.Y + ball.radius), (float)(2 * ball.radius), (float)(2 * ball.radius));
        }
        if ((ball.Host.ElapsedTime * ball.Host.FPS * 10) % 2 == 0){
            g.FillEllipse(linearBrush1, (float)(ball.Location.X - ball.radius), (float)(ball.Location.Y + ball.radius), (float)(2 * ball.radius), (float)(2 * ball.radius));
        }else{
            g.FillEllipse(linearBrush2, (float)(ball.Location.X - ball.radius), (float)(ball.Location.Y + ball.radius), (float)(2 * ball.radius), (float)(2 * ball.radius));
        }
    }
}

}

and the drawOn ball looks like this:

new BufferedBall(this).DrawOn(g);

Is that what you meant? because it still flickers?

+5
source share
5 answers
this.DoubleBuffered = true;

, . , Windows Forms , . .

+2

DoubleBuffered true . . DrawOn Forms Designer . false true.

, , DoubleBuffered true:

private int yDir = 1,xDir=1;
int step = 1;

private void timer1_Tick(object sender, EventArgs e)
{
    if (ball.Location.Y >= this.Height-50)
        yDir =   -1 ;
    if (ball.Location.X >= this.Width-50) 
        xDir= -1 ;

    ball.MoveBy(xDir*step,yDir*step);
    ball.Host.ElapsedTime++;
    this.Invalidate();
 }

private void DoubleBufferedBall_Paint(object sender, PaintEventArgs e)
{                      
        DrawOn(e.Graphics);
}
+1

, , , Bitmap, OnPaint, .

, . .

You may also want to take a look at XNA - it may be redundant for your project here, but you can use XNA in WinForms as a rendering mechanism.

0
source

All Articles