DeleteDC 与 ReleaseDC的区别

本文深入解析Windows编程中DC对象的管理原则,包括CreateDC与DeleteDC,GetDC与ReleaseDC的配对使用。特别讨论了MFC中CPaintDC和CClientDC对象的自动资源管理机制,无需手动调用ReleaseDC。强调了用过之后,不留痕迹的编程理念。

DeleteDC 与 CreateDC 对应
ReleaseDC 与 GetDC 对应

借过来的(GetDC),就要放还回去(ReleaseDC)
自已创建的(CreateDC),要自已删除(DeleteDC)

总之,要“用过之后, 不留痕迹”


还有以下的特殊情况,如CClientDC   dc(this)或CPaintDC dc(this);
对于这样的dc到最后是否需要ReleaseDC()?

答案是:不需要。
因为,在MFC中,WM_PAINT之类的消息在销毁时会自动调用
CPaintDC的析构函数,对对像dc进行销毁,如下:

CPaintDC::~CPaintDC()
{
ASSERT(m_hDC   !=   NULL);
ASSERT(::IsWindow(m_hWnd));

::EndPaint(m_hWnd,   &m_ps);//在EndPaint函数中释放dc
Detach();
}

而CClientDC的对像dc则会在CClientDC的析构函数中被释放

CClientDC::~CClientDC()
{
ASSERT(m_hDC   !=   NULL);
::ReleaseDC(m_hWnd,   Detach());
}
————————————————
版权声明:本文为优快云博主「cabinriver」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/cabinriver/article/details/6581154

#include <windows.h>#include <iostream>#include <gdiplus.h> #pragma comment(lib, "gdiplus.lib") // 屏幕捕获函数bool CaptureScreen(const std::wstring& filePath){ // 初始化GDI+ Gdiplus::GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; if (Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL) != Gdiplus::Ok) { std::cerr << "GDI+ initialization failed!" << std::endl; return false; } // 获取屏幕的设备上下文(DC) HDC hdcScreen = GetDC(NULL); // 获取整个屏幕的DC if (hdcScreen == NULL) { std::cerr << "Failed to get screen DC!" << std::endl; Gdiplus::GdiplusShutdown(gdiplusToken); return false; } // 创建内存设备上下文(DC) HDC hdcMemory = CreateCompatibleDC(hdcScreen); if (hdcMemory == NULL) { std::cerr << "Failed to create memory DC!" << std::endl; ReleaseDC(NULL, hdcScreen); Gdiplus::GdiplusShutdown(gdiplusToken); return false; } // 获取屏幕宽高 int screenWidth = GetSystemMetrics(SM_CXSCREEN); int screenHeight = GetSystemMetrics(SM_CYSCREEN); // 创建屏幕尺寸兼容的位图 HBITMAP hBitmap = CreateCompatibleBitmap(hdcScreen, screenWidth, screenHeight); if (hBitmap == NULL) { std::cerr << "Failed to create bitmap!" << std::endl; DeleteDC(hdcMemory); ReleaseDC(NULL, hdcScreen); Gdiplus::GdiplusShutdown(gdiplusToken); return false; } // 将屏幕内容复制到内存中的位图 SelectObject(hdcMemory, hBitmap); if (!BitBlt(hdcMemory, 0, 0, screenWidth, screenHeight, hdcScreen, 0, 0, SRCCOPY)) { std::cerr << "Failed to capture screen!" << std::endl; DeleteObject(hBitmap); DeleteDC(hdcMemory); ReleaseDC(NULL, hdcScreen); Gdiplus::GdiplusShutdown(gdiplusToken); return false; } // 保存捕获的位图为文件 Gdiplus::Bitmap bitmap(hBitmap, NULL); Gdiplus::Status status = bitmap.Save(filePath.c_str(), &Gdiplus::ImageFormatBMP); if (status != Gdiplus::Ok) { std::cerr << "Failed to save image!" << std::endl; DeleteObject(hBitmap); DeleteDC(hdcMemory); ReleaseDC(NULL, hdcScreen); Gdiplus::GdiplusShutdown(gdiplusToken); return false; } // 清理资源 DeleteObject(hBitmap); DeleteDC(hdcMemory); ReleaseDC(NULL, hdcScreen); Gdiplus::GdiplusShutdown(gdiplusToken); std::cout << "Screen capture saved to " << filePath << std::endl; return true;} int main(){ std::wstring filePath = L"screen_capture.bmp"; if (CaptureScreen(filePath)) { std::cout << "Capture successful!" << std::endl; } else { std::cerr << "Capture failed!" << std::endl; } return 0;}AI写代码cpp运行
最新发布
08-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值