What's the story behind the WM_SYNCPAINT message?

Raymond Chen

Danail wants to know the story behind the WM_SYNC­PAINT message. The documentation pretty much tells the story. When a window has been hidden, shown, moved or sized, the system may determine that it needs to send a WM_SYNC­PAINT message to the windows of other threads. This message must be passed to Def­Window­Proc, which will send the WM_NCPAINT and WM_ERASE­BKGND messages to the window as necessary. When you call the Set­Window­Pos function, the window manager updates the window size, position, whatever, and then it goes around repainting the windows that were affected by the operation. By default, the Set­Window­Pos function does a quick-repaint of the windows before returning. After the function returns, the normal WM_PAINT message does the real work of painting the window. The quick-repaint is done so that there is immediate feedback that the window did change its size, position, whatever. This quick-repaint is done by sending a WM_NCPAINT and WM_ERASE­BKGND message to the windows that were affected by the Set­Window­Pos operation. This normally happens without incident, but if one of the windows affected by the Set­Window­Pos operation belongs to another thread, the window manager needs to get into the context of that other thread to finish the job. That’s where WM_SYNC­PAINT comes in. The WM_SYNC­PAINT message means, “Hey, I was going around quick-painting a bunch of windows, but I couldn’t quick-paint you (or any other windows on your thread) because I was on the wrong thread. Could you finish quick-painting yourself (and all the other windows that need quick-painting)? Thanks.” Another way of looking at this is that it is a way for the window manager to teleport itself into another thread so it can finish its work. “Lah di dah, quick-painting all the windows, oh crap, I can’t quick-paint that window because it’s on the wrong thread. Let me inject myself into that other process [trivial, since I’m the window manager, I’M IN YR PROCESS REEDING YR MSGS], and now I can send a message to myself [WM_SYNCPAINT], and when that other copy of me receives it, he’ll finish where I left off.” If you don’t like any of this teleportation or multiple-copies-of-yourself imagery, you can say that the WM_SYNC­PAINT message means, “Quick-paint this window as part of a quick-paint operation begun on another thread.”

If you don’t want this quick-paint to take place, you can follow the instructions in the documentation and pass the SWP_DEFER­ERASE flag to suppress the WM_SYNC­PAINT message.

0 comments

Discussion is closed.

Feedback usabilla icon