crColour是位图中的透明色/ void CChessView::DrawTransparent(CDC * pDC, int x, int y, COLORREF crColour) { COLORREF crOldBack,crOldText; CDC dcImage,dcMask; CBitmap bmp; crOldBack=pDC->SetBkColor(RGB(255,255,255));//RGB(0,0,0) 隐形色 crOldText=pDC->SetTextColor(RGB(0,0,0));//RGB(255,255,255) //IDB_BITMAP4为待显示的位图 BITMAP bm; bmp.GetBitmap(&bm); int nWidth=bm.bmWidth,nHeight=bm.bmHeight; //为图象及mask各创建一个DC dcImage.CreateCompatibleDC(pDC); dcMask.CreateCompatibleDC(pDC); //把图象装入imageDC CBitmap *pOldBitmapImage=dcImage.SelectObject(&bmp); //为“掩码”位图创建一个单色bitmap CBitmap bitmapMask; bitmapMask.CreateBitmap(nWidth,nHeight,1,1,NULL);//把mask位图装入maskDC CBitmap *pOldBitmapMask=dcMask.SelectObject(&bitmapMask);//用透明色创建“掩码”位图 dcImage.SetBkColor(crColour);//crColour是位图中的透明色 dcMask.BitBlt(0,0,nWidth,nHeight,&dcImage,0,0,SRCCOPY);//分3步进行实际的绘制 pDC->BitBlt(x,y,nWidth,nHeight,&dcImage,0,0,SRCINVERT); pDC->BitBlt(x,y,nWidth,nHeight,&dcMask,0,0,SRCAND); pDC->BitBlt(x,y,nWidth,nHeight,&dcImage,0,0,SRCINVERT);//恢复原先设置 dcImage.SelectObject(pOldBitmapImage); dcImage.SelectObject(pOldBitmapMask); pDC->SetBkColor(crOldBack); pDC->SetTextColor(crOldText); } //直接调用这个函数,CDC * pDC,我就不多介绍了,int x,int y是该位图的坐标,crColour是位图中的透明色。
转帖:http://zhidao.baidu.com/question/12378638.html?fr=qrl