winapidoublebuffered

What is the correct way to suppress WM_ERASEBKGND?


If I need to use double buffering, I need to suppress WM_ERASEBKGND message.

I can handle WM_ERASEBKGND and return immediately. But can I set the WNDCLASS/WNDCLASSEX's hbrBackground to NULL and not handle the WM_ERASEBKGND message? Is this a correct way?


Solution

  • Yes, setting hbrBackground to NULL is an appropriate way to avoid implementing a no-op WM_ERASEBKGND handler.

    When you pass WM_ERASEBKGND on to DefWindowProc, it checks the background brush in the window's class. If there is one, it fills the dirty region with it. If the background brush is null, it does nothing and returns. That's essentially the same as having your own do-nothing WM_ERASEBKGND handler.

    The return value from the WM_ERASEBKGND handler affects the fErase field of the PAINTSTRUCT you get when WM_PAINT calls BeginPaint. The WM_PAINT handler is supposed to check fErase to find out whether it needs to erase the background itself or if it was already done by WM_ERASEBKGND. (Though I've never actually seen anyone check it.) If you let DefWindowProc handle WM_ERASEBKGND it will return TRUE if it has a color number or brush and FALSE if the hbrBackground is NULL.