Originally posted by: Paul Reynolds
CImageList method:
The CImageList method is rather fixed requiring you to have a predefined bitmap....and is nothing to do with the method actually listed....If you really want to draw an image easily using a predefined image, you would use an Icon anyway...
Another Method:
If this is another method to that which has already be described, why not submit it separately?!
Originally posted by: wubin huzheng
CDC* pwholeWndDC;
CImageList imageList;
There's a simple method to draw a transparent bitmap by calling class
CImageList.
CBitmap bitmapImage;
//load bitmap from resource
bitmapImage.LoadBitmap(IDB_BITMAP);
BITMAP bmpInfo;
CSize bitSize;
//get size of bitmap
bitmapImage.GetBitmap(&bmpInfo);
bitSize = CSize(bmpInfo.bmWidth, bmpInfo.bmHeight);
pwholeWndDC=GetDC();
//create ImageList
imageList.Create(bitSize.cx, bitSize.cy, ILC_COLOR|ILC_MASK, 1, 1);
//select color white is transparent color
imageList.Add(&bitmapImage, RGB(0,0,0));
CPoint ptLocation(0,0);
//drawing the first bitmap of imagelist Transparently
bitmapImage.Draw(pwholeWndDC, 0, ptLocation, ILD_TRANSPARENT);
Originally posted by: Shaun Cooley
// This is by Jeff Prosise:
CPoint org (0, 0);
// Create a memory DC (dcImage) and select the bitmap into it
// Create a second memory DC (dcAnd) and in it create an AND mask
CBitmap bitmapAnd;
dcImage.SetBkColor (crColor);
// Create a third memory DC (dcXor) and in it create an XOR mask
CBitmap bitmapXor;
dcXor.BitBlt (org.x, org.y, size.x, size.y, &dcImage, org.x, org.y, SRCCOPY);
dcXor.BitBlt (org.x, org.y, size.x, size.y, &dcAnd, org.x, org.y, 0x220326);
// Copy the pixels in the destination rectangle to a temporary
CBitmap bitmapTemp;
dcTemp.BitBlt (org.x, org.y, size.x, size.y, pDC, x, y, SRCCOPY);
// Generate the final image by applying the AND and XOR masks to
dcTemp.BitBlt (org.x, org.y, size.x, size.y, &dcXor, org.x, org.y, SRCINVERT);
// Blit the resulting image to the screen
// Restore the default bitmaps
Here is another method, the advantage to this message
is that the bitmap is only drawn on the visible DC once.
This reduces flicker on slower systems.
void CMaskedBitmap::DrawTransparent (CDC* pDC, int x, int y, COLORREF crColor)
{
BITMAP bm;
GetObject (sizeof (BITMAP), &bm);
CPoint size (bm.bmWidth, bm.bmHeight);
pDC->DPtoLP (&size);
pDC->DPtoLP (&org);
CDC dcImage;
dcImage.CreateCompatibleDC (pDC);
CBitmap* pOldBitmapImage = dcImage.SelectObject (this);
dcImage.SetMapMode (pDC->GetMapMode ());
CDC dcAnd;
dcAnd.CreateCompatibleDC (pDC);
dcAnd.SetMapMode (pDC->GetMapMode ());
bitmapAnd.CreateBitmap (bm.bmWidth, bm.bmHeight, 1, 1, NULL);
CBitmap* pOldBitmapAnd = dcAnd.SelectObject (&bitmapAnd);
dcAnd.BitBlt (org.x, org.y, size.x, size.y, &dcImage, org.x, org.y,
SRCCOPY);
CDC dcXor;
dcXor.CreateCompatibleDC (pDC);
dcXor.SetMapMode (pDC->GetMapMode ());
bitmapXor.CreateCompatibleBitmap (&dcImage, bm.bmWidth, bm.bmHeight);
CBitmap* pOldBitmapXor = dcXor.SelectObject (&bitmapXor);
// memory DC (dcTemp)
CDC dcTemp;
dcTemp.CreateCompatibleDC (pDC);
dcTemp.SetMapMode (pDC->GetMapMode ());
bitmapTemp.CreateCompatibleBitmap (&dcImage, bm.bmWidth, bm.bmHeight);
CBitmap* pOldBitmapTemp = dcTemp.SelectObject (&bitmapTemp);
// the image in the temporary memory DC
dcTemp.BitBlt (org.x, org.y, size.x, size.y, &dcAnd, org.x, org.y, SRCAND);
pDC->BitBlt (x, y, size.x, size.y, &dcTemp, org.x, org.y, SRCCOPY);
dcTemp.SelectObject (pOldBitmapTemp);
dcXor.SelectObject (pOldBitmapXor);
dcAnd.SelectObject (pOldBitmapAnd);
dcImage.SelectObject (pOldBitmapImage);
}