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

3

10

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?

Acuity answered 13/8, 2011 at 18:21 Comment(0)
L
9

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.

Limeade answered 13/8, 2011 at 18:29 Comment(3)
So the DC returned by BeginPaint may be different every time? Does that mean it's not really about redrawing certain part of screen as about validating that DC's handle? Anyway could you recommend some read on this please? I find MSDN unsatisfactory on this topic.Poncho
@stupid_idiot: BeginPaint doesn't validate a handle, but rather the invalid region (those areas marked as invalid by calling InvalidateRect/InvalidateRgn). This is to tell the system, that the invalid region has been repainted, and no more painting is required, until some part of the window is invalidated again. A WM_PAINT message is only generated, if the invalid region is non-empty. For reference, you should read About Painting and Drawing, and take it from there.Burrow
This answer is missing an important aspect: BeginPaint "sets the clipping region of the device context to exclude any area outside the update region." (as pointed out in elevener's answer).Burrow
C
6

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.

Convoluted answered 13/8, 2011 at 18:27 Comment(2)
Well, actually GetUpdateRgn can retrieve the current update region, and ValidateRect (or ValidateRgn) can be used to validate a rect/region. BeginPaint and EndPaint are just easier.Darcie
It's the BeginPaint API call, that validates the update region (see GetUpdateRgn: "The BeginPaint function automatically validates the update region, so any call to GetUpdateRgn made immediately after the call to BeginPaint retrieves an empty update region.").Burrow
P
5

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.

Plantar answered 13/8, 2011 at 18:26 Comment(1)
Thank you guys for answers, really hard to decide which one is best!Acuity

© 2022 - 2024 — McMap. All rights reserved.