使CBitmap位图对象背景透明显示

void  DrawTransparentBitmap(HDC hdc, HBITMAP hBitmap,  short  xStart,  short  yStart, COLORREF cTransparentColor) 

    BITMAP bm; 
    COLORREF cColor; 
    HBITMAP bmAndBack, bmAndObject, bmAndMem, bmSave; 
    HBITMAP bmBackOld, bmObjectOld, bmMemOld, bmSaveOld; 
    HDC hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave; 
    POINT ptSize; 
    
    hdcTemp 
= CreateCompatibleDC(hdc); 
    SelectObject(hdcTemp, hBitmap); 
// Select the bitmap 
    
    GetObject(hBitmap, 
sizeof(BITMAP), (LPSTR)&bm); 
    ptSize.x 
= bm.bmWidth; // Get width of bitmap 
    ptSize.y = bm.bmHeight; // Get height of bitmap 
    DPtoLP(hdcTemp, &ptSize, 1); // Convert from device 
    
    
// to logical points 
    
    
// Create some DCs to hold temporary data. 
    hdcBack = CreateCompatibleDC(hdc); 
    hdcObject 
= CreateCompatibleDC(hdc); 
    hdcMem 
= CreateCompatibleDC(hdc); 
    hdcSave 
= CreateCompatibleDC(hdc); 
    
    
// Create a bitmap for each DC. DCs are required for a number of 
    
// GDI functions. 
    
    
// Monochrome DC 
    bmAndBack = CreateBitmap(ptSize.x, ptSize.y, 11, NULL); 
    
    
// Monochrome DC 
    bmAndObject = CreateBitmap(ptSize.x, ptSize.y, 11, NULL); 
    
    bmAndMem 
= CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y); 
    bmSave 
= CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y); 
    
    
// Each DC must select a bitmap object to store pixel data. 
    bmBackOld = (HBITMAP)SelectObject(hdcBack, bmAndBack); 
    bmObjectOld 
=(HBITMAP)SelectObject(hdcObject, bmAndObject); 
    bmMemOld 
= (HBITMAP)SelectObject(hdcMem, bmAndMem); 
    bmSaveOld 
= (HBITMAP)SelectObject(hdcSave, bmSave); 
    
    
// Set proper mapping mode. 
    SetMapMode(hdcTemp, GetMapMode(hdc)); 
    
    
// Save the bitmap sent here, because it will be overwritten. 
    BitBlt(hdcSave, 00, ptSize.x, ptSize.y, hdcTemp, 00, SRCCOPY); 
    
    
// Set the background color of the source DC to the color. 
    
// contained in the parts of the bitmap that should be transparent 
    cColor = SetBkColor(hdcTemp, cTransparentColor); 
    
    
// Create the object mask for the bitmap by performing a BitBlt 
    
// from the source bitmap to a monochrome bitmap. 
    BitBlt(hdcObject, 00, ptSize.x, ptSize.y, hdcTemp, 00
        SRCCOPY); 
    
    
// Set the background color of the source DC back to the original 
    
// color. 
    SetBkColor(hdcTemp, cColor); 
    
    
// Create the inverse of the object mask. 
    BitBlt(hdcBack, 00, ptSize.x, ptSize.y, hdcObject, 00
        NOTSRCCOPY); 
    
    
// Copy the background of the main DC to the destination. 
    BitBlt(hdcMem, 00, ptSize.x, ptSize.y, hdc, xStart, yStart, 
        SRCCOPY); 
    
    
// Mask out the places where the bitmap will be placed. 
    BitBlt(hdcMem, 00, ptSize.x, ptSize.y, hdcObject, 00, SRCAND); 
    
    
// Mask out the transparent colored pixels on the bitmap. 
    BitBlt(hdcTemp, 00, ptSize.x, ptSize.y, hdcBack, 00, SRCAND); 
    
    
// XOR the bitmap with the background on the destination DC. 
    BitBlt(hdcMem, 00, ptSize.x, ptSize.y, hdcTemp, 00, SRCPAINT); 
    
    
// Copy the destination to the screen. 
    BitBlt(hdc, xStart, yStart, ptSize.x, ptSize.y, hdcMem, 00
        SRCCOPY); 
    
    
// Place the original bitmap back into the bitmap sent here. 
    BitBlt(hdcTemp, 00, ptSize.x, ptSize.y, hdcSave, 00, SRCCOPY); 
    
    
// Delete the memory bitmaps. 
    DeleteObject(SelectObject(hdcBack, bmBackOld)); 
    DeleteObject(SelectObject(hdcObject, bmObjectOld)); 
    DeleteObject(SelectObject(hdcMem, bmMemOld)); 
    DeleteObject(SelectObject(hdcSave, bmSaveOld)); 
    
    
// Delete the memory DCs. 
    DeleteDC(hdcMem); 
    DeleteDC(hdcBack); 
    DeleteDC(hdcObject); 
    DeleteDC(hdcSave); 
    DeleteDC(hdcTemp); 
}
 
 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值