简体   繁体   中英

Proper use of Win32 Device Context

I have been working with GDI for a time now and I am trying to up my understanding of it to be able to create a wrapper class for it.

  1. Why cant I hold on to a window device context until the app ends? Does it get invalidated upon any of the events? I heard someone say after WM_SIZE the context gets invalidated, is this true? I never experienced this even though I always cache the window context. It makes it much easier to createCompatibleDC()s and createCompatibleBitmaps()s in the constructor. I also cache these created DC and use them to avoid flickering, for local bounds drawing, caching the draw operation incase window is minimized then maximized.

  2. Why would an application ever fetch more than 1 window context at a time? As the documemtation complains about running out the window context limit.

Remember, in the context you are talking about 'invalidate' means to require some portion of the window to be redrawn, it does not mean that a handle retrieved from GetDC will be rendered unusable.

A window with CS_HREDRAW or CS_VREDRAW will require at least partial redrawing on any resize in the indicated direction as well as any sizing operation that increases the window size.

Because of the limitations on the number of GDI objects the normal pattern is to handle WM_PAINT messages and use the handle returned by BeginPaint (which will only remain usable so long as EndPaint is not called).

And as Some programmer dude said in a comment, a second DC can be used to perform flicker-free drawing, the time consuming drawing is done to an off-screen DC and then simply copied onto the screen DC. And even if that is done entirely within a WM_PAINT handler the off-screen DC will normally be released once the drawing operation completes so as to not hog GDI resources.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM