How can I stop flickering when updating a Win32 window?

0
0
Asked By MellowKite42 On

I'm building a Win32 application and need to redraw the window in response to user input. When I call UpdateWindow() or RedrawWindow(), the contents sometimes flicker for about a second, as if the background is being cleared before the new drawing appears. I'm still learning Win32 and would appreciate advice on the correct way to prevent this and keep the UI smooth.

3 Answers

Answered By QuietFalcon_8 On

For custom drawing, double-buffering is usually the most reliable solution. Create a memory device context and compatible bitmap, draw the complete frame there, and then copy it to the window with BitBlt in one operation. Make sure to restore the previous bitmap and delete the temporary bitmap and device context afterward, otherwise each repaint can leak GDI handles.

MellowKite42 -

That makes sense—I hadn’t realized the background erase happened separately from WM_PAINT. I’ll start by preventing the erase and use an off-screen buffer if the flicker remains.

Answered By CopperMango7 On

The flicker is probably caused by the system erasing the window background before your paint code runs. Handle WM_ERASEBKGND yourself and return 1, then paint the entire background during WM_PAINT. Also avoid requesting background erasure when invalidating the window—for example, use InvalidateRect(hwnd, NULL, FALSE), or leave RDW_ERASE out of RedrawWindow().

Answered By SilverWalrus31 On

This is the classic erase-then-repaint gap. Suppressing WM_ERASEBKGND can be enough if your WM_PAINT handler redraws every part of the client area. If some areas are left untouched, double-buffer the complete drawing instead so the user only sees the finished frame.

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.