Difference between GetDC() and BeginPaint()
Asked Answered
B

4

13

I am working on Win32 UI. I want to know the difference Between GetDC and BeginPaint. When to Use which API and when not to use which API.

Bongbongo answered 30/4, 2011 at 10:56 Comment(0)
B
18

GetDC simply returns the handle to the device context, which can be used any time anywhere to do your own drawing. BeginPaint on the other hand prepares the window for painting, and also provides information on what should be painted (such as whether the background needs repainting and the rect that needs to be painted).

Examples of when to use each? BeginPaint is most commonly seen inside WM_PAINT handlers (MSDN: An application should not call BeginPaint except in response to a WM_PAINT message. Each call to BeginPaint must have a corresponding call to the EndPaint function.). GetDC can be used anywhere, so if you want to draw on an external window. Basically anytime thats not in a WM_PAINT handler. BeginPaint and EndPaint also have some affect on the caret. Read msdn for more details.

Byblow answered 30/4, 2011 at 11:4 Comment(2)
Thanks for the valuable input. If i use GetDC inside WM_PAINT instead of BeginPaint will it give any problemBongbongo
Read the reply from Hans Passant for details on why that's not a good idea. When you create a new visual studio project you get the BeginPaint and EndPaint already in the WM_PAINT handler, Why would you want to use GetDC?Byblow
B
14

GetDC() is not a substitute for Begin+EndPaint(). If you try, you'll find that your UI thread starts to burn 100% cpu core and your WM_PAINT handler getting called over and over again.

The big one is BeginPaint(), it clears the update region of the window. The value of PAINTSTRUCT.rcPaint. WM_PAINT is generated as long as the window has a dirty rectangle, created by an InvalidateRect() call by either the window manager or your program explicitly calling it. BeginPaint() clears it.

Barabarabarabas answered 30/4, 2011 at 13:7 Comment(3)
Thanks, that explains the CPU utilization I was getting without EndPaint.Tridentum
True. EndPaint essentially calls ValidateRect. You can either defer to DefWindowProc (which validates the rect) or just call ValidateRect yourself.Inglorious
BeginPaint() also sets the clipping region for the device context to the update region. GetDC() does not. @DwayneRobinson Somewhat surprising, BeginPaint() validates the update region, not EndPaint().Toscano
W
12

BeginPaint is intended to be called only in response to WM_PAINT message. The device context obtained by it points to the invalidated (to-be-redrawn) area of the window. It should be then released using EndPaint.

GetDC can be called at any time. The device context obtained by it points to the whole client area of the window. To release it, you should call ReleaseDC.

Wethington answered 30/4, 2011 at 11:0 Comment(0)
V
0

If we use hdc outside of wm_paint, we use get_dc/relese_dc, and if we use hdc inside of wm_paint, we use begin_paint/end_paint.

Vibraculum answered 17/4 at 12:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.