How can I prevent flicker when repainting a Win32 window?

0
1
Asked By MellowPine42 On

I'm building a Win32 application and need to update the window in response to user input. Whether I use UpdateWindow() or RedrawWindow(), the contents sometimes flicker for about a second, which makes the interface feel rough. What is the proper way to repaint the window without showing that brief background erase or redraw? I'm still learning Win32, so practical guidance would be appreciated.

2 Answers

Answered By RiverQuartz18 On

For more complex drawing, use double buffering. Create a memory device context and compatible bitmap, draw the complete frame off screen, and then copy it to the window with BitBlt in one operation. Afterward, restore the original bitmap, delete the temporary bitmap, and release the memory DC so you don’t leak GDI handles. This prevents the user from seeing the intermediate erase and drawing steps.

SunnyVale56 -

Double buffering is usually the most reliable long-term solution for custom Win32 drawing, especially when several controls or graphics are updated together.

Answered By CopperLark7 On

The flicker is often caused by Windows erasing the background before your painting code runs. Handle WM_ERASEBKGND yourself and return 1, then paint the entire background during WM_PAINT. You can also avoid requesting an erase: use InvalidateRect(hwnd, NULL, FALSE), or leave RDW_ERASE out when calling RedrawWindow().

QuietOrbit3 -

This works well when your WM_PAINT handler redraws every part of the client area, since you’re taking responsibility for the background yourself.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.