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.
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.
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.
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 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
.
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.
© 2022 - 2024 — McMap. All rights reserved.