简体   繁体   English

正确使用 Win32 设备上下文

[英]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.我已经与 GDI 合作了一段时间,我正在努力提高我对它的理解,以便能够为它创建一个包装类。

  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?我听到有人说 WM_SIZE 后上下文无效,这是真的吗? 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.这使得在构造函数中 createCompatibleDC()s 和 createCompatibleBitmaps()s 变得更加容易。 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.我还缓存这些创建的 DC 并使用它们来避免闪烁,对于局部边界绘制,缓存绘制操作,以防窗口最小化然后最大化。

  2. Why would an application ever fetch more than 1 window context at a time?为什么应用程序一次会获取 1 个以上的窗口上下文? 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.请记住,在您谈论的上下文中,“无效”意味着需要重新绘制窗口的某些部分,这并不意味着从 GetDC 检索到的句柄将无法使用。

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.具有 CS_HREDRAW 或 CS_VREDRAW 的窗口将需要在指示方向上的任何调整大小以及任何增加窗口大小的调整大小操作上至少部分重绘。

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).由于 GDI 对象数量的限制,正常模式是处理 WM_PAINT 消息并使用由 BeginPaint 返回的句柄(只有在不调用 EndPaint 时才会保持可用)。

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.并且正如一些程序员在评论中所说的那样,可以使用第二个 DC 进行无闪烁绘图,耗时的绘图是在屏幕外 DC 上完成,然后简单地复制到屏幕 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.即使这完全在 WM_PAINT 处理程序中完成,一旦绘图操作完成,屏幕外 DC 通常会被释放,以免占用 GDI 资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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