c++windowswinapiwm-paint

WinAPI: Omitting BeginPaint & EndPaint in WM_PAINT causes 100% CPU usage


When handling the WM_PAINT message, I omitted the BeginPaint and EndPaint calls, and the CPU usage shot up to 100%. Why is this?

I'm also using worker threads... but they do something different and seem to have no influence on this matter.

Also, can I use the device context from GetDC() rather than BeginPaint? They seem to have different values so I thought they had different jobs.

Sorry if I sound like an idiot - I'm new to WinAPI, C++ and just the world of logic in general...

Thanks


Solution

  • This is entirely normal. Windows generates the WM_PAINT message when your window's update region in not empty. What you are supposed to do is mark it empty again. You do so, for example, by calling Begin/EndPaint().

    If you don't then Windows immediately generates yet another WM_PAINT message, still trying to get the update region emptied. Your thread will burn 100% core, idly processing WM_PAINT messages and not actually getting the job done. Maybe you are actually painting, Windows just doesn't know what you painted and doesn't try to guess at it.

    Using Begin/EndPaint() is very much the sane way to get that job done. It isn't the only way, you could also call ValidateRect() or ValidateRgn(). As long as you are "new to winapi", I'd very strongly recommend you do this the normal way.