1.CBitmap-------------------------------------------------------------------------------------
1、类层次
CObject
CGdiObject
CBitmap
BOOL CGdiObject::~CGdiObject()
{
if (m_hObject == NULL)
return FALSE;
return ::DeleteObject(Detach());
}
从CGdiObject的析构函数中,可以看出有先Detach再Delete
2、BOOL CBitmap::LoadBitmap(LPCTSTR lpszResourceName);
BOOL CBitmap::LoadBitmap(UINT nIDResource);
对应Win32 SDK中的LoadBitmap
HBITMAP LoadBitmap(
HINSTANCE hInstance, // handle to application instance
LPCTSTR lpBitmapName // name of bitmap resource
);
注意:由LoadBitmap
2.CDC-----------------------------------------------------------------------------------------------
class CDC : public CObject
{
public:
static CDC* PASCAL FromHandle(HDC hDC);
// Attach与Detach函数
BOOL Attach(HDC hDC);
HDC Detach();
// 构造与析构函数
CDC();
virtual ~CDC();
}
1、CDC的构造函数和析构函数
CDC::CDC()
{
m_hDC = NULL;
}
CDC::~CDC()
{
if (m_hDC != NULL)
::DeleteDC(Detach());
}
从析构函数中可以看出,释放了关联的HDC
并且在Delete之前还Detach了
2、Attach用于将CDC的对象和HDC的句柄关联在一起
Detach则相反
BOOL CDC::Attach(HDC hDC)
{
if (hDC == NULL)
{
return FALSE;
}
m_hDC = hDC;
CHandleMap* pMap = afxMapHDC(TRUE); // create map if not exist
pMap->SetPermanent(m_hDC, this);
return TRUE;
}
HDC CDC::Detach()
{
HDC hDC = m_hDC;
if (hDC != NULL)
{
CHandleMap* pMap = afxMapHDC(); // don't create if not exist
if (pMap != NULL)
pMap->RemoveHandle(m_hDC);
}
m_hDC = NULL;
return hDC;
}
3、FromHandle从映射表中查找有无hDC对应的CDC
有则返回其指针;没有则先new一个临时的CDC的对象,将其和hDC关联再返回其指针
注意:这个临时的CDC对象不用由用户delete,系统的空闲线程会处理
CDC* PASCAL CDC::FromHandle(HDC hDC)
{
CHandleMap* pMap = afxMapHDC(TRUE); //create map if not exist
CDC* pDC = (CDC*)pMap->FromHandle(hDC); // 如果pMap中不存在hDC关联的CDC对象,则new一CDC对象并和hDC关联
return pDC;
}
3.CDialog----------------------------------------------------------------------------------
1、CDialog析构时销毁了与之关联的窗口
CDialog::~CDialog()
{
if (m_hWnd != NULL)
{
DestroyWindow();
}
}
4.CWnd---------------------------------------------------------------------------------------
1、CWnd析构时销毁了与之关联的窗口
CWnd::~CWnd()
{
if (m_hWnd != NULL)
{
DestroyWindow();
}
}
2、获取桌面窗口句柄
static CWnd* PASCAL CWnd::GetDesktopWindow();
对应Win32 SDK中的为
HWND GetDesktopWindow(VOID);