9
votes

One can use GetDC/ReleaseDC to draw in client area of window. But in response to WM_PAINT message one have to use BeginPaint/EndPaint. Is there something special about this?

3

3 Answers

9
votes

A WM_PAINT message is issued when a part of the window needs to be updated. By specifying BeginPaint/EndPaint() you are telling gdi that you are actually doing that job. If you don't call BeginPaint() for the specified region, WM_PAINT messages will be generated for as long until someone actually updates it. The function gives you a DC just because it's convenient. Internally BeginPaint()/EndPaint() probably call GetDC()/ReleaseDC().

In contrast with GetDC and ReleaseDC you are telling GDI that you are now about to paint something onto the DC, without gdi requesting that you must.

6
votes

Yes, sure. BeginPaint() retrieves the update region and automatically takes care of emptying it again. If you use GetDC() then you'll notice your program burning 100% cpu core, running the WM_PAINT handler over and over again because the update region was never cleared. You'd have to call ValidateRect() to avoid this.

5
votes

BeginPaint function automatically sets the clipping region of the device context so if only part of your window have to be redrawn it wouldn't redraw the whole window.