Flicker-free drawing

I have a win32 application that was developed in C ++. The application draws some things in a window using basic shapes (rectangles). Windows are repainted every 20 ms (50 Hz) using InvalidateRect. Everything works well, but the pattern flickers. How can I prevent flicker? In C #, I usually use a double buffer component (like pictureBox, for example), how can I get rid of this in C ++ using win32?

+4
source share
3 answers

You can easily implement double buffering in Win32. Assuming you are making your picture directly in the window using the context of your device, do this instead:

Create a β€œmemory” device context and make the whole picture in the context of this device, then copy the invalid portions of the window into the context of the actual device, if necessary, using BitBlt()

Here's a pretty good (albeit high level) review here.

+4
source

You can create a device context in memory, draw these shapes (as in the context of a window device), and then move from it to the window device context when the window is invalid.

You also need to disable background cleanup (process the WM_ERASEBKGND message accordingly ) before the rally occurs.

Edit: I came across a pretty comprehensive flicker-free drawing tutorial in GDI that explains all aspects of drawing in Windows and comes with examples.

+5
source

You can also use double buffer in C ++.

When you get a DC for drawing, you create a screen bitmap (CreateCompatibleBitmap) and a memory DC (CreateCompatibleDC). Do all your painting in this DC. Finally, make the Blt bit from DC memory to actual DC.

For better performance, you may need to cache the bitmap off-screen and DC, but remember to recreate them when you resize the window.

+3
source

All Articles