void xxx::TransparentBitBlt(CDC *pDC , CBitmap &bitmap ,int x, int y, int cx , int cy , COLORREF crTrans)
{
//使用过滤色绘制图片
//pDC 绘制的目标DC
//bitmap 准备装载的位图
//crTrans 指定屏蔽颜色
CDC *pMaskDC = new CDC; // 装有掩码位图的DC
CDC *pSourceDC = new CDC; // 装载源图的DC
CBitmap bmpMask; // MaskDC上使用的位图
pMaskDC->CreateCompatibleDC(pDC);
pSourceDC->CreateCompatibleDC(pDC);
bmpMask.CreateBitmap(cx, cy, 1, 1, NULL); // create monochrome bitmap
CBitmap * pOldMaskBmp = pMaskDC->SelectObject(&bmpMask);
// Set the mask bitmap
CBitmap * pOldBmp = pSourceDC->SelectObject(&bitmap);
pSourceDC->SetBkColor(crTrans);
pMaskDC->BitBlt(x, y, cx, cy, pSourceDC,
0, 0, SRCCOPY);
// Do the painting
BitBlt(pDC->m_hDC ,x, y, cx, cy, pSourceDC->m_hDC ,
0, 0, SRCINVERT);
BitBlt(pDC->m_hDC ,x, y, cx, cy, pMaskDC->m_hDC ,
x, y, SRCAND);
BitBlt(pDC->m_hDC ,x, y, cx, cy, pSourceDC->m_hDC ,
0, 0, SRCINVERT);
// Omit the resting of destroying GDI object
bmpMask.DeleteObject();
pOldBmp->DeleteObject();
pOldMaskBmp->DeleteObject();
pMaskDC->DeleteDC();
pSourceDC->DeleteDC();
}
//调用,在OnPaint()中
CDC* memDC = new CDC;
CBitmap bgbmp;
memDC->CreateCompatibleDC(&dc);
bgbmp.CreateCompatibleBitmap(&dc,104,151);
bgbmp.m_hObject = (HBITMAP)::LoadImage(NULL,_T("man1.bmp"),IMAGE_BITMAP,0,0,LR_LOADFROMFILE | LR_CREATEDIBSECTION);
TransparentBitBlt(&dc,bgbmp,0,0,104,151,RGB(255,0,0));