Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing without flickering

I have a win32 application which was developed in c++. The application draws some stuff on the window using basic shapes (rectangles). The windows is repainted every 20ms (50hz) using InvalidateRect. All works well but the drawing is flickering. How can i prevent the flickering? In c# i normally use a double buffered component (such as pictureBox), how could i get rid of this in c++ using win32?

like image 931
blejzz Avatar asked Sep 21 '11 15:09

blejzz


2 Answers

You can create an in-memory device context, draw those shapes to it (just like you would to the window's device context) and then blit from it to window's device context when the window is invalidated.

You also need to disable background clearing (handle WM_ERASEBKGND window message appropriately) before the draw happens.

Edit: I stumbled upon a pretty exhaustive tutorial on flicker-free drawing in GDI, which explains all aspects of drawing in Windows and comes with examples.

like image 126
macbirdie Avatar answered Sep 20 '22 03:09

macbirdie


You can easily implement double buffering in Win32 as well. Assuming you are doing your painting directly on the Window using its device context, do this instead:

Create a "memory" device context and do all your drawing on that device context, then copy the invalidated portions of the window to the actual device context when appropriate, using the BitBlt() function

There's a pretty good (albeit high level) overview here.

like image 36
Chad Avatar answered Sep 22 '22 03:09

Chad