Using WM_SETREDRAW to prevent window flickering

Flickering is a common issue with Windows applications. Different techniques exist to solve it, but they are more or less intrusive, and more or less efficient. Double-buffering for example is probably the best solution, but hard to put in place in a legacy code (even impossible sometimes). Using the WM_SETREDRAWmessage can be a light and efficient solution.

The captures below show the flickering of an application with a lot of controls, before and after using WM_SETREDRAW :

RH-flickeringBefore using WM_SETREDRAW
RH-not-flickeringAfter using WM_SETREDRAW

And the change is quite simple : disable controls drawing, then update all controls, and finally reenable controls drawing and force a repaint, like this :

    // Stop drawing controls
    SendMessage(Handle, WM_SETREDRAW, WPARAM(False), 0);

    // Update all controls...

    // Restart drawing controls
    SendMessage(Handle, WM_SETREDRAW, WPARAM(True), 0);
    // Force a repaint of the window and all of its child controls
    RedrawWindow(Handle, NULL, NULL,  RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);

Leave a Reply

Your email address will not be published. Required fields are marked *