Question

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

Was it helpful?

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.

OTHER TIPS

Not sure if this is the case, but beginpaint and endpaint also validates drawn region of window, if you dont use them then windows is not aware that you have redrawn this region.You can call ValidateRect function to inform of the fact that window was redrawn.

Not sure if this will help in your case, you can read more on tjis in following so

Difference between GetDC() and BeginPaint()

GDI issues the WM_PAINT message to update a part of the window. BeginPaint/EndPaint() informs gdi that the handler is doing that job. In absence of a BeginPaint() for the specified region, until the window is updated (by someone) WM_PAINT messages will be generated. This is the reason you need BeginPaint/EndPaint() in a WM_PAINT handler and in absence of which you see a high CPU utilization.

GetDC() is not a substitute for Begin+EndPaint() because of the reason mentioned in my previous paragraph.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top