程序代码:
class MemDC :
public CDC
{
CSize m_size;
public:
MemDC(void);
~MemDC(void);
//加载进程内位图的构造函数
MemDC(UINT nBitmap,CDC* pDC=NULL)
{
LoadBitmap( nBitmap,pDC);
}
//加载进程外位图的构造函数
MemDC(LPCWSTR szFile, CDC* pDC=NULL)
{
LoadBitmap( szFile, pDC);
}
//创建空白位图的构造函数
MemDC(int cx, int cy,CDC* pDC=NULL)
{
Create( cx, cy, pDC);
}
int GetWidth()
{return m_size.cx;}
int GetHeight()
{return m_size.cy;}
BOOL DeleteDC()
{
if (!GetSafeHdc())
return FALSE;
CBitmap *pBitmap=GetCurrentBitmap();
if(pBitmap)
pBitmap->DeleteObject();
return CDC::DeleteDC();
}
//创建空白位图
BOOL Create(int cx, int cy,CDC* pDC=NULL)
{
CBitmap bmp;
if (!bmp.CreateCompatibleBitmap(pDC,cx,cy))
return FALSE;
m_size.cx=cx;
m_size.cy=cy;
CreateCompatibleDC(pDC);
SelectObject(&bmp);
return TRUE;
}
//进程内资源加载
BOOL LoadBitmap(UINT nBitmap,CDC* pDC=NULL)
{
CBitmap bmp;
if (!bmp.LoadBitmap(nBitmap))
return FALSE;
BITMAP bm;
bmp.GetBitmap(&bm);
m_size.cx=bm.bmWidth;
m_size.cy=bm.bmHeight;
CreateCompatibleDC(pDC);
SelectObject(&bmp);
return TRUE;
}
//进程外位图图片加载
BOOL LoadBitmap(LPCWSTR szFile, CDC* pDC=NULL)
{
HBITMAP hBitmap = (HBITMAP)LoadImage(NULL,szFile,IMAGE_BITMAP,
0,0,LR_LOADFROMFILE);
if (!hBitmap)
return FALSE;
BITMAP bm;
GetObject(hBitmap,sizeof(bm),&bm);
m_size.cx=bm.bmWidth;
m_size.cy=bm.bmHeight;
CreateCompatibleDC(pDC);
SelectObject(hBitmap);
return TRUE;
}
};