ReleaseDC 和 DeleteDC 的区别

简单的说,GetDCReleaseDC 的调用配对,CreateDCDeleteDC 的调用配对。

GetDC 是从窗口获取现有的 DC,而 CreateDC 是创建 DC,所以 ReleaseDC 和 DeleteDC 的作用一个是释放,一个是销毁。

 

叁考:https://blog.csdn.net/business122/article/details/9612299

 

API原文

intReleaseDC(CDC*pDC);

Return Value

Nonzero if successful; otherwise 0.

Parameters

pDC

Identifies the device context to be released.

Remarks

Releases a device context, freeing it for use by other applications. The effect of the ReleaseDCmember function depends on the device-context type.

The application must call the ReleaseDCmember function for each call to the member function and for each call to the member function.

这里最后一句提到了

The application must call the ReleaseDCmember function for each call to the member function and for each call to the member function.

所有调用GetWindowDC和GetDC获取的DC对象必须使用ReleaseDC来释放

 

然后又查看了DeleteDC的API内容

CDC::DeleteDC

virtualBOOLDeleteDC();

Return Value

Nonzero if the function completed successfully; otherwise 0.

Remarks

In general, do not call this function; the destructor will do it for you. The DeleteDCmember function deletes the Windows device contexts that are associated with m_hDCin the current CDCobject. If this CDCobject is the last active device context for a given device, the device is notified and all storage and system resources used by the device are released.

An application should not call DeleteDCif objects have been selected into the device context. Objects must first be selected out of the device context before it it is deleted.

An application must not delete a device context whose handle was obtained by calling . Instead, it must call to free the device context. The and classes are provided to wrap this functionality.

The DeleteDCfunction is generally used to delete device contexts created with , , or .

 

看了最后一句就可以区分什么时候调用ReleaseDC了

就是 GetDC 或 GetWindowDC 获得的 DC 要使用 ReleaseDC 释放

Create 得到的 DC 要用 Delete 释放

这样就不会出现内存泄露事情了

posted @ 2021-01-11 10:17  personnel  阅读(631)  评论(0编辑  收藏  举报
友情链接:图片批量处理工具 gif动态图制作工具 制作电子相册 图片排版工具 制作淘宝主图视频 MKScript 鼠标键盘自动化脚本语言