gdi+与兼容dc 的问题
这是一个按钮类,是用gdi+写的,
m_btninfoNor ,m_btninfoHot , m_btninfoPre ,m_btninfoDis 这4个对象是类的对象,类定义为:
typedef struct _TRATRAPNGINFO_
{
int nWidth;
int nHeight;
CImage* pImg;
}TRAPNGINFO;
void Load(UINT IDBkGroup,int width=0, int height=0, const CString& resourceType = _T("PNG"))
实现代码为:
void CMyButton::Load(UINT IDBkGroup,int width, int height, const CString& resourceType)
{
CImage orgImg;
HINSTANCE hInst = AfxGetResourceHandle();
HRSRC hRsrc = ::FindResource(hInst,MAKEINTRESOURCE(IDBkGroup),resourceType);
if (hRsrc == NULL)
return;
//讲资源加载到内存中
DWORD len = SizeofResource(hInst, hRsrc);
BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);
if (lpRsrc == NULL)
return;
//为流申请资源
HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, len);
BYTE* pmem = (BYTE*)GlobalLock(m_hMem);
memcpy(pmem,lpRsrc,len);
IStream* pstm;
CreateStreamOnHGlobal(m_hMem,FALSE,&pstm);
//加载流
orgImg.Load(pstm);
//释放资源
GlobalUnlock(m_hMem);
GlobalFree(m_hMem);
pstm->Release();
FreeResource(lpRsrc);
if (resourceType == _T("PNG"))
{
if (orgImg.GetBPP() == 32)
{
//PNG透明
for(int i = 0; i < orgImg.GetWidth(); i++)
{
for(int j = 0; j < orgImg.GetHeight(); j++)
{
unsigned char* pucColor = reinterpret_cast<unsigned char *>(orgImg.GetPixelAddress(i , j));
pucColor[0] = pucColor[0] * pucColor[3] / 255;
pucColor[1] = pucColor[1] * pucColor[3] / 255;
pucColor[2] = pucColor[2] * pucColor[3] / 255;
}
}
}
}
if (width==0 && height==0)
{
width=orgImg.GetHeight();//方形
}
if (height==0)
{
height = orgImg.GetHeight();
}
m_btninfoNor.nWidth = width;
m_btninfoNor.nHeight = height;
m_btninfoHot.nWidth = width;
m_btninfoHot.nHeight = height;
m_btninfoPre.nWidth = width;
m_btninfoPre.nHeight = height;
m_btninfoDis.nWidth = width;
m_btninfoDis.nHeight = height;
CImage** imgs[]={&m_btninfoNor.pImg,&m_btninfoHot.pImg,&m_btninfoPre.pImg,&m_btninfoDis.pImg};
int posX=0;
for (int i=0;i<4 && posX<=(orgImg.GetWidth()-width);i++,posX+=width)
{
CImage* pMap=new CImage();
if (*imgs[i] != NULL)
{
delete *imgs[i];
*imgs[i] = NULL;
}
*imgs[i]=pMap;
if (resourceType == _T("PNG"))
{
BOOL bStat = FALSE;
if (orgImg.GetBPP() == 32)
{
bStat = pMap->CreateEx(width, height,orgImg.GetBPP(),BI_RGB,NULL, CImage::createAlphaChannel);
}else
{
bStat = pMap->CreateEx(width, height,orgImg.GetBPP(),BI_RGB,NULL);
}
ASSERT(bStat);
}else
{
BOOL bStat = pMap->CreateEx(width, height,orgImg.GetBPP(),BI_RGB,NULL);
ASSERT(bStat);
}
//这是创建兼容dc吗 ? 还是干什么,这2句代码没看懂,但是注释掉后,又不管用
CImageDC imageDC(*pMap);
orgImg.Draw(imageDC,0,0,width,height,posX,0,width,height);
}
}