Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there something special about using BeginPaint/EndPain and not GetDC/ReleaseDC in response to WM_PAINT message?

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?

like image 607
Vadim Avatar asked Aug 13 '11 18:08

Vadim


3 Answers

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.

like image 167
Tobias Schlegel Avatar answered Oct 18 '22 19:10

Tobias Schlegel


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.

like image 32
Hans Passant Avatar answered Oct 18 '22 19:10

Hans Passant


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.

like image 29
elevener Avatar answered Oct 18 '22 21:10

elevener