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

本文介绍了一种在C++中实现透明位图绘制的方法。通过使用CDC和CBitmap类,结合BitBlt等函数,实现了将指定颜色作为透明色进行位图绘制的功能。此方法适用于需要透明效果的位图显示场景。
//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();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值