您的位置:首页 > 其它

如何在CDC上绘制背景透明的图片

2014-06-09 16:59 471 查看
//i give some code,Hope it helps

void CBarcodeBox::DrawTransparentBitmap(CDC* pDC,CBitmap* pBitmap,

short xStart,short yStart,COLORREF cTransparentColor)

{

BITMAP bm;

COLORREF cColor;

CBitmap bmAndBack, bmAndObject, bmAndMem, bmSave;

CBitmap *pbmBackOld, *pbmObjectOld, *pbmMemOld, *pbmSaveOld;

CDC dcMem, dcBack, dcObject, dcTemp, dcSave;

CPoint ptSize;

dcTemp.CreateCompatibleDC(pDC);

dcTemp.SelectObject(pBitmap); // Select the bitmap

pBitmap->GetObject(sizeof(BITMAP),(LPSTR)&bm);

ptSize.x = bm.bmWidth; // Get width of bitmap

ptSize.y = bm.bmHeight; // Get height of bitmap

dcTemp.DPtoLP(&ptSize,1); // Convert from device to logical points

// Create some DCs to hold temporary data.

dcBack.CreateCompatibleDC(pDC);

dcObject.CreateCompatibleDC(pDC);

dcMem.CreateCompatibleDC(pDC);

dcSave.CreateCompatibleDC(pDC);

// Create a bitmap for each DC. DCs are required for a number of

// GDI functions.

// Monochrome DC

bmAndBack.CreateBitmap(ptSize.x,ptSize.y,1,1,NULL);

// Monochrome DC

bmAndObject.CreateBitmap(ptSize.x,ptSize.y,1,1,NULL);

bmAndMem.CreateCompatibleBitmap(pDC,ptSize.x,ptSize.y);

bmSave.CreateCompatibleBitmap(pDC,ptSize.x,ptSize.y);

// Each DC must select a bitmap object to store pixel data.

pbmBackOld = dcBack.SelectObject(&bmAndBack);

pbmObjectOld = dcObject.SelectObject(&bmAndObject);

pbmMemOld = dcMem.SelectObject(&bmAndMem);

pbmSaveOld = dcSave.SelectObject(&bmSave);

// Set proper mapping mode.

dcTemp.SetMapMode(pDC->GetMapMode());

// Save the bitmap sent here, because it will be overwritten.

dcSave.BitBlt(0,0,ptSize.x,ptSize.y,&dcTemp,0,0,SRCCOPY);

// Set the background color of the source DC to the color.

// contained in the parts of the bitmap that should be transparent

cColor = dcTemp.SetBkColor(cTransparentColor);

// Create the object mask for the bitmap by performing a BitBlt()

// from the source bitmap to a monochrome bitmap.

dcObject.BitBlt(0,0,ptSize.x,ptSize.y,&dcTemp,0,0,SRCCOPY);

// Set the background color of the source DC back to the original color.

dcTemp.SetBkColor(cColor);

// Create the inverse of the object mask.

dcBack.BitBlt(0,0,ptSize.x,ptSize.y,&dcObject,0,0,NOTSRCCOPY);

// Copy the background of the main DC to the destination.

dcMem.BitBlt(0,0,ptSize.x,ptSize.y,pDC,xStart,yStart,SRCCOPY);

// Mask out the places where the bitmap will be placed.

dcMem.BitBlt(0,0,ptSize.x,ptSize.y,&dcObject,0,0,SRCAND);

// Mask out the transparent colored pixels on the bitmap.

dcTemp.BitBlt(0,0,ptSize.x,ptSize.y,&dcBack,0,0,SRCAND);

// XOR the bitmap with the background on the destination DC.

dcMem.BitBlt(0,0,ptSize.x,ptSize.y,&dcTemp,0,0,SRCPAINT);

// Copy the destination to the screen.

pDC->BitBlt(xStart,yStart,ptSize.x,ptSize.y,&dcMem,0,0,SRCCOPY);

// Place the original bitmap back into the bitmap sent here.

dcTemp.BitBlt(0,0,ptSize.x,ptSize.y,&dcSave,0,0,SRCCOPY);

// Delete the memory bitmaps.

(dcBack.SelectObject(pbmBackOld))->DeleteObject();

(dcObject.SelectObject(pbmObjectOld))->DeleteObject();

(dcMem.SelectObject(pbmMemOld))->DeleteObject();

(dcSave.SelectObject(pbmSaveOld))->DeleteObject();

// Delete the memory DCs.

dcMem.DeleteDC();

dcBack.DeleteDC();

dcObject.DeleteDC();

dcSave.DeleteDC();

dcTemp.DeleteDC();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: