views:

45

answers:

1

I am doing some GDI+ drawing in Visual C++ and noticed that my canvas doesn't look the best when scrolling, resizing, etc... due to lots of flickering and slow redraws. From what I've read, the solution is to double buffer but there are conflicting suggestions on how to implement.

A few sources suggest to use the existing .NET implementation, like so:

using namespace System::Windows::Forms;  
this->SetStyle(ControlStyles::AllPaintingInWmPaint |  
               ControlStyles::UserPaint |  
               ControlStyles::DoubleBuffer, true);

Many more sources recommend just drawing to your own bitmap and then throwing it onto the screen:

dbBitmap = gcnew Bitman(ClientRectangle.Width, ClientRectangle.Height);
dbGraphics = Graphics::FromImage(dbBitmap);

// use the graphics object to do everything you need to here

this->createGraphics()->DrawImageUnscaled(dbBitmap, 0, 0);

Is there a functional difference between these approaches, or would I just be bringing to the forefront what Microsoft does in the background? I'm concerned about both speed and efficiency, as well as possible pitfalls of using one approach over the other.

+1  A: 

Just set the form's DoubleBuffered property to true in the constructor or the Properties window. You have to draw in the Paint event, using CreateGraphics() is never correct. Call Invalidate() or Update() if something happens that would require the window to be repainted.

Hans Passant
I actually was drawing in the OnPaint event, I just didn't realize that the PaintEventArgs already came with an existing Graphics object. After that change, and just setting the DoubleBuffered property, my canvas still flickers, but it's significantly better now.
MikeD