为什么将窗口样式设定为CS_OWNDC,在GetDC后还要ReleaseDC

HisinWang 2011-06-28 10:52:57
Windows程序设计中指出:
在注册窗口类时将CS_OWNDC标志作为窗口类样式的一部分,那么每个基于这个窗口类创建的窗口都有它的私有设备环境。
但又说:即使是使用了CS_OWNDC样式,设备环境句柄在退出窗口前也应该被释放。
这里我始终都想不通,既然用GetDC获得的HDC指向窗口的私有设备环境,那怎么释放呢,释放掉那这个私有设备环境及即释放了其占据的内存,不可能啊,那之后再GetDCz又是怎么回事呢?
ReleaseDC到底做了什么啊?

如果没有将窗口样式设定为CS_OWNDC,那么GetDC就是创建一个DC对象,并返回指向这个对象的指针,当然必须配对的使用ReleaseDC以释放内存。但对于上面提到的CS_OWNDC样式我就没有办法理解了!
望高手指点!
感觉DC很玄啊,很多东西都没法理解!

THKS......
...全文
815 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wocow3 2011-06-30
  • 打赏
  • 举报
回复
uses a significant portion of 64K GDI heap
说的是GDI heap是64K
HisinWang 2011-06-30
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wocow3 的回复:]
uses a significant portion of 64K GDI heap
说的是GDI heap是64K
[/Quote]
正解!谢谢!
没看仔细!
wocow3 2011-06-29
  • 打赏
  • 举报
回复
对CS_OWNDC窗口风格的DC调用ReleaseDC实际上并不会清理DC,也不会释放其占据的内存。线程的DC cache会将标记CS_OWNDC的DC是特殊的DC,获取CS_OWNDC窗口的DC总是返回它的这个特殊DC,而一般的窗口则返随意获取一个非CS_OWNDC的DC,并每次重置其属性。
“即使是使用了CS_OWNDC样式,设备环境句柄在退出窗口前也应该被释放。”的意图只是让你的代码在CS_OWNDC和非CS_OWNDC下保持一致。
HisinWang 2011-06-29
  • 打赏
  • 举报
回复
我一直在想:为什么对于common DC要retrieved from a pool of contexts maintained by the system。用的时候新建不就可以了吗。为什么还要限定个数!难道它很占内存!
终于在MSDN上找到答案了:
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!
HisinWang 2011-06-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wocow3 的回复:]
对CS_OWNDC窗口风格的DC调用ReleaseDC实际上并不会清理DC,也不会释放其占据的内存。线程的DC cache会将标记CS_OWNDC的DC是特殊的DC,获取CS_OWNDC窗口的DC总是返回它的这个特殊DC,而一般的窗口则返随意获取一个非CS_OWNDC的DC,并每次重置其属性。
“即使是使用了CS_OWNDC样式,设备环境句柄在退出窗口前也应该被释放。”的意图只是让你的代码在CS……
[/Quote]
我查看MSDN上关于ReleaseDC的说明:
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.
红色部分所说也就是3楼这位仁兄的见解!
HisinWang 2011-06-29
  • 打赏
  • 举报
回复
受益匪浅!
我查看CSDN上的GetDC函数的说明:
Remarks:
The GetDC function retrieves a common, class, or private DC depending on the class style of the specified window. For class and private DCs, GetDC leaves the previously assigned attributes unchanged. However, for common DCs, GetDC assigns default attributes to the DC each time it is retrieved. For example, the default font is System, which is a bitmap font. Because of this, the handle for a common DC returned by GetDC does not tell you what font, color, or brush was used when the window was drawn.

Note that the handle to the DC can only be used by a single thread at any one time.

After painting with a common DC, the ReleaseDC function must be called to release the DC. Class and private DCs do not have to be released. ReleaseDC must be called from the same thread that called GetDC. The number of DCs is limited only by available memory.

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。
Eleven 2011-06-28
  • 打赏
  • 举报
回复
注意看最后一段话~
Eleven 2011-06-28
  • 打赏
  • 举报
回复
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.

15,976

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧