va.
va.

Reputation: 780

Visual Studio fix PictureBox flickering (C++)

I am making a game and I have to redraw about 40 objects on each Timer tick. I have about 7 classes with different Draw(Picturebox ^ pictureBox) methods. As you see I pass pictureBox pointer for each object draw method. As the objects are so many and maybe will be a little more, the pictureBox is flickering, because it draws each object after object. Is there a simple way to fix the flickering? Maybe somehow prepare the image and then show it on the PictureBox?

Upvotes: 0

Views: 806

Answers (1)

Hans Passant
Hans Passant

Reputation: 941545

7 classes with different Draw(Picturebox ^ pictureBox) methods

That signature is very likely to create flicker. Because in order to take advantage of the double-buffering built into PictureBox, you have to also pass a Graphics object. The one you got from the Paint event. You are probably using CreateGraphics() now, a serious flicker bug.

The proper signature is Draw(Graphics^ graphics) and used like this:

private: 
    void pictureBox1_Paint(Object^ sender, PaintEventArgs^ e) {
        for each (GameObject^ obj in gameObjects) {
            obj->Draw(e->Graphics);
        }
    }

    void timer1_Tick(Object^sender, EventArgs^ e) {
        updateGame();               // move stuff around
        pictureBox1->Invalidate();  // redraw scene
    }

With the assumption that you added the event handlers for the PictureBox and Timer control.

Upvotes: 1

Related Questions