void CTransparentBmp::OnPaint()
{
CPaintDC dc(this); // device context for painting
CDC memDC;
memDC.CreateCompatibleDC(&dc);
CDC pMaskDC;
pMaskDC.CreateCompatibleDC(&dc);
CBitmap *oldBitmap1,*oldBitmap2;
BITMAP bm;
m_myBmp.GetBitmap(&bm); //更新了bm的数据
oldBitmap1 = memDC.SelectObject(&m_myBmp); //选入到DC
CBitmap bmpMask;
bmpMask.CreateBitmap(bm.bmWidth,bm.bmHeight,1,1,NULL);
oldBitmap2 = pMaskDC.SelectObject(&bmpMask);
memDC.SetBkColor(RGB(0,0,0)); //设为透明的部分颜色,此处为黑色
pMaskDC.BitBlt(0,0,bm.bmWidth,bm.bmHeight,&memDC,0,0,SRCCOPY);
dc.BitBlt(0, 0, bm.bmWidth, bm.bmHeight,&memDC,0,0,SRCINVERT);
dc.BitBlt(0, 0, bm.bmWidth, bm.bmHeight,&pMaskDC,0,0,SRCAND);
dc.BitBlt(0, 0, bm.bmWidth, bm.bmHeight,&memDC,0,0,SRCINVERT);
memDC.SelectObject(oldBitmap1);
pMaskDC.SelectObject(oldBitmap2);
}
m_myBmp 为CBitmap类型的变量;
CBitmap m_myBmp;
m_myBmp.LoadBitmap(IDB_MYBMP); //需要修改的bmp图片