I've never quite understood why erasing the background has a separate windows message. I looks a bit redundant to me. When I've created owner-drawn buttons, I've always ended up erasing the background from inside WM_PAINT. I have sometimes even done all the painting from inside WM_ERASEBKGND and left WM_PAINT empty. Both seem to work fine. Is there any advantage to separating the painting into 2 operations?
Asked
Active
Viewed 1,680 times
6
-
4As explained below this is a leftover from the old days. For a modern application you should always return 1 (TRUE) in response to `WM_ERASEBKGND` and perform the drawing in `WM_PAINT` (to avoid flicker). It is also important to only draw to the screen once to avoid flicker. If you need to draw something fancy use memory DCs to draw to first. Handy way to obtain one [here](http://blogs.msdn.com/b/oldnewthing/archive/2011/05/20/10166505.aspx) – demorge Feb 27 '12 at 16:44
-
@demorge, if `WM_PAINT` is expected to do its own erasing, then you should return `FALSE` from `WM_ERASEBKGND`. – finnw Jul 06 '12 at 01:27
1 Answers
4
This is entirely guesses:
Back in the olden days, filling a rectangle with colour was a relatively slow operation. But filling one big rectangle was still a lot quicker than filling lots of little rectangles.
I guess that if you had a window with a child window, and both had the same registered background brush, then Windows was smart enough to realise it didn't need to send a WM_ERASEBKGND to the child when it had already cleared the parent. With a moderately complex dialog box on a very slow PC, this might be a significant improvement.

arx
- 16,686
- 2
- 44
- 61