What does the CS_CLASSDC & CS_OWNDC class style do?

关于这两个类型的解释可以参考这两个地方:

 

(1)两篇出自The Old New Thing的很有意思的文章 CS_CLASSDCCS_OWNDC

 

(2)Feng Yuan的Windows Graphics Programming Win32 GDI and DirectDraw® 第五章第二节

Class Device Context

The CS_CLASSDC flag in WNDCLASS's style field tells the window management module to create a device context shareable by all windows of the particular class. Such a device context is called class device context. The actual device context is created when the first window instance of that class is created, and initialized once to its default values.

When GetDC, GetWindowDC, or BeginPaint is called on a window belonging to such a class, the device context attached with the window class is returned, with an updated display rectangle, visible region, and empty clipping region. All other attributes in the class device context are kept the same—for example, logical pen, text color, mapping mode, etc. After finishing drawing, ReleaseDC or EndPaint returns the device context to the window class, without destroying it or resetting its attributes. A class device context is destroyed only when the last window of the class is destroyed.

If you have ever heard that ReleaseDC and EndPaint can be omitted for class device context because they do nothing, forget about it. This kind of suggestion should be considered harmful, because it can cause big trouble for a small gain. For one thing, EndPaint restores the caret turned off by BeginPaint.

Class device context is useful for control windows that are drawn using the same attribute values, because it minimizes the time required to prepare a device context for drawing and releasing afterward. Another benefit of class device context is its minimum memory usage.

Class device context is provided only for backward compatibility. Its advantage is diminished by today's larger RAM and faster CPU, and by the protected address space design of Win32 processes. Class device context is not recommended in Win32 programming.

Private Device Context

The CS_OWNDC flag in WNDCLASS's style field tells the window management module to create one device context for each window created using this class. So every window will have a dedicated device context during the lifetime of the window—that is, a private device context.

A private device context gets initialized once to its default values. Each call to GetDC, GetWindowDC, or BeginPaint retrieves a window's private device context, with a new display rectangle and visible region. The application can then set device context's attribute and issue drawing commands. ReleaseDC or EndPaint returns the device context to the window, without changing the device context, so the next time a device context is requested, attributes like pen and brush are still the same.

The MSDN documentation on private device context is not clear (check the Private Display Device Contexts portion). It mentions that the application must retrieve the handle only once, and then it goes on to say the application could call BeginPaint to incorporate the update region.

Private device context goes to an extreme to improve performance by sacrificing memory resource. A device context uses three types of resource: a GDI handle, memory in the user application's address space, and memory in system kernel address space. Private device context is useful only for windows with complicated settings that take a long time to prepare, and for windows that need frequent updating. It's recommended only when performance considerations are much more important than memory and GDI handle resource usage.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值