May 6th, 1999, 09:04 AM
Anybody have a good example on how to bitblt bitmaps transparently using GDI ?
|
Click to See Complete Forum and Search --> : How to bitblt transparently ?? May 6th, 1999, 09:04 AM Anybody have a good example on how to bitblt bitmaps transparently using GDI ? David Giovannini May 6th, 1999, 05:41 PM This came out of CMenuSpawn, available from this site. void CMenuSpawn::TransparentBlt(CDC * pDestDc, int x, int y, int w, int h, CBitmap * pBmp, int sx, int sy, COLORREF crTransparent) { CDC memDC, maskDC, tempDC; maskDC.CreateCompatibleDC(pDestDc); CBitmap maskBitmap; //add these to store return of SelectObject() calls CBitmap* pOldMemBmp = NULL; CBitmap* pOldMaskBmp = NULL; memDC.CreateCompatibleDC(pDestDc); tempDC.CreateCompatibleDC(pDestDc); CBitmap bmpImage; bmpImage.CreateCompatibleBitmap( pDestDc, w, h); pOldMemBmp = memDC.SelectObject( &bmpImage ); CBitmap * oldBmp = tempDC.SelectObject(pBmp); memDC.BitBlt( 0,0,w, h, &tempDC, sx, sy, SRCCOPY ); // Create monochrome bitmap for the mask maskBitmap.CreateBitmap(w, h, 1, 1, NULL); pOldMaskBmp = maskDC.SelectObject( &maskBitmap ); memDC.SetBkColor(crTransparent); // Create the mask from the memory DC maskDC.BitBlt(0, 0, w, h, &memDC, 0, 0, SRCCOPY); memDC.SetBkColor(RGB(0,0,0)); memDC.SetTextColor(RGB(255,255,255)); memDC.BitBlt(0, 0, w, h, &maskDC, 0, 0, SRCAND); // Set the foreground to black. See comment above. pDestDc->SetBkColor(RGB(255,255,255)); pDestDc->SetTextColor(RGB(0,0,0)); pDestDc->BitBlt(x, y, w, h, &maskDC, 0, 0, SRCAND); // Combine the foreground with the background pDestDc->BitBlt(x, y, w, h, &memDC, 0, 0, SRCPAINT); tempDC.SelectObject(oldBmp); if (pOldMaskBmp) maskDC.SelectObject( pOldMaskBmp ); if (pOldMemBmp) memDC.SelectObject( pOldMemBmp ); } // this variation does a color replace (useful for scrolling graphics) void CMenuSpawn::BackgroundBlt(CDC * pDestDc, int x, int y, int w, int h, CBitmap * pBmp, int sx, int sy, COLORREF crTransparent) { COLORREF backGround = ::GetSysColor(COLOR_BTNFACE); CDC memDC, maskDC, tempDC; maskDC.CreateCompatibleDC(pDestDc); CBitmap maskBitmap; //add these to store return of SelectObject() calls CBitmap* pOldMemBmp = NULL; CBitmap* pOldMaskBmp = NULL; memDC.CreateCompatibleDC(pDestDc); tempDC.CreateCompatibleDC(pDestDc); CBitmap bmpImage; bmpImage.CreateCompatibleBitmap( pDestDc, w, h); pOldMemBmp = memDC.SelectObject( &bmpImage ); CBitmap * oldBmp = tempDC.SelectObject(pBmp); memDC.BitBlt( 0,0,w, h, &tempDC, sx, sy, SRCCOPY ); // Create monochrome bitmap for the mask maskBitmap.CreateBitmap(w, h, 1, 1, NULL); pOldMaskBmp = maskDC.SelectObject( &maskBitmap ); memDC.SetBkColor(crTransparent); // Create the mask from the memory DC maskDC.BitBlt(0, 0, w, h, &memDC, 0, 0, SRCCOPY); memDC.SetBkColor(backGround); memDC.SetTextColor(RGB(255,255,255)); memDC.BitBlt(0, 0, w, h, &maskDC, 0, 0, SRCAND); // Set the foreground to black. See comment above. pDestDc->SetBkColor(RGB(255,255,255)); pDestDc->SetTextColor(RGB(0,0,0)); pDestDc->BitBlt(x, y, w, h, &maskDC, 0, 0, SRCAND); // Combine the foreground with the background pDestDc->BitBlt(x, y, w, h, &memDC, 0, 0, SRCPAINT); tempDC.SelectObject(oldBmp); if (pOldMaskBmp) maskDC.SelectObject( pOldMaskBmp ); if (pOldMemBmp) memDC.SelectObject( pOldMemBmp ); } codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |