|
Give you a function of transparent bitmap
void TransparentBlt2 (HDC hdcDest, // target DC
int nXOriginDest, // target X offset
int nYOriginDest, // target Y offset
int nWidthDest, // target width
int nHeightDest, // target height
HDC hdcSrc, // source DC
int nXOriginSrc, // source X starting point
int nYOriginSrc, // source Y starting point
int nWidthSrc, // source width
int nHeightSrc, // source height
COLORREF crTransparent // transparent color, COLORREF type
)
{
HBITMAP hOldImageBMP, hImageBMP = CreateCompatibleBitmap (hdcDest, nWidthDest, nHeightDest); // create compatible bitmap
HBITMAP hOldMaskBMP, hMaskBMP = CreateBitmap (nWidthDest, nHeightDest, 1, 1, NULL); // create a monochrome mask bitmap
HDChImageDC = CreateCompatibleDC (hdcDest);
HDChMaskDC = CreateCompatibleDC (hdcDest);
hOldImageBMP = (HBITMAP) SelectObject (hImageDC, hImageBMP);
hOldMaskBMP = (HBITMAP) SelectObject (hMaskDC, hMaskBMP);
// Copy the bitmap from the source DC to the temporary DC
if (nWidthDest == nWidthSrc&&nHeightDest == nHeightSrc)
BitBlt (hImageDC, 0, 0, nWidthDest, nHeightDest, hdcSrc, nXOriginSrc, nYOriginSrc, SRCCOPY);
else
StretchBlt (hImageDC, 0, 0, nWidthDest, nHeightDest,
hdcSrc, nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc, SRCCOPY);
// set transparent color
SetBkColor (hImageDC, crTransparent);
// Generate a mask bitmap with transparent areas in white and other areas in black
BitBlt (hMaskDC, 0, 0, nWidthDest, nHeightDest, hImageDC, 0, 0, SRCCOPY);
// Generate a bitmap in which the transparent area is black and the other areas remain unchanged
SetBkColor (hImageDC, RGB (0,0,0));
SetTextColor (hImageDC, RGB (255,255,255));
BitBlt (hImageDC, 0, 0, nWidthDest, nHeightDest, hMaskDC, 0, 0, SRCAND);
// The transparent part keeps the screen unchanged, the other parts become black
SetBkColor (hdcDest, RGB (0xff, 0xff, 0xff));
SetTextColor (hdcDest, RGB (0,0,0));
BitBlt (hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hMaskDC, 0, 0, SRCAND);
// "OR" operation to generate the final effect
BitBlt (hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hImageDC, 0, 0, SRCPAINT);
SelectObject (hImageDC, hOldImageBMP);
DeleteDC (hImageDC);
SelectObject (hMaskDC, hOldMaskBMP);
DeleteDC (hMaskDC);
DeleteObject (hImageBMP);
DeleteObject (hMaskBMP);
} |
|