窗口类的 CS_OWNDC 类型

来源:互联网 发布:apache 下载windows10 编辑:程序博客网 时间:2024/06/11 01:27
MSDN上有这么一段:
Classes and Device Contexts
A device context is a special set of values that applications use for drawing in the client area of their windows. The system requires a device context for each window on the display but allows some flexibility in how the system stores and treats that device context.

If no device context style is explicitly given, the system assumes each window uses a device context retrieved from a pool of contexts maintained by the system. In such cases, each window must retrieve and initialize the device context before painting and free it after painting.

To avoid retrieving a device context each time it needs to paint inside a window, an application can specify the CS_OWNDC style for the window class. This class style directs the system to create a private device context — that is, to allocate a unique device context for each window in the class. The application need only retrieve the context once and then use it for all subsequent painting.


The ReleaseDC function releases a device context (DC), freeing it for use by other applications. The effect of the ReleaseDC function depends on the type of DC. It frees only common and window DCs. It has no effect on class or private DCs. 

对CS_OWNDC窗口风格的DC调用ReleaseDC实际上并不会清理DC,也不会释放其占据的内存。线程的DC cache会将标记CS_OWNDC的DC是特殊的DC,获取CS_OWNDC窗口的DC总是返回它的这个特殊DC,


而一般的窗口则返随意获取一个非CS_OWNDC的DC,并每次重置其属性。
“即使是使用了CS_OWNDC样式,设备环境句柄在退出窗口前也应该被释放。”的意图只是让你的代码在CS_OWNDC和非CS_OWNDC下保持一致。




GetDC函数返回指定窗口客户区域DC的句柄。至于返回的DC类型是common DC,class DC还是private DC取决于指定窗口的class style即窗口所属注册窗口类的style.
当WNDCLASS结构体中style包含CS_CLASSDC时GetDC返回的就是class DC的句柄。所有基于这个窗口类的窗口实例共享这个DC。
当WNDCLASS结构体中style包含CS_OWNDC时GetDC返回的就是private DC的句柄。每个基于这个窗口类的窗口实例都有唯一的属于它自己的这个DC。
当WNDCLASS结构体中style不包含CS_CLASSDC以及CS_OWNDC时GetDC返回的就是common DC。



Windows 95/98/Me: Although the CS_OWNDC style is convenient, use it carefully, because each device context uses a significant portion of 64K GDI heap. 
原来DC是很占内存的:64K!

0 0
原创粉丝点击