Drawing the "transparent" bitmap refers to the rest of the specified color in a bitmap. We call this color "transparent color." By specifying the background color of the bitmap to "Transparent Color", the image is not drawn when drawing, and only the image is drawn, so that the image can be transparent to the window in the bitmap.
The key to draw the "transparent" bitmap is to create a "Mask Bitmap)," Mask "bitmap is a monochrome bitmap, which is a monochrome silhouette of the image in the bitmap.
In Windows programming, the drawing is used to use the device descriptor, we need to create two memory device descriptors: Bitmap Device Description Table (Image DC) and "Mask" bitmap device description table (Mask DC). The bitmap device description table is used to load the bitmap, and the "Mask" bitmap device descriptor is used to load the "Mask" bitmap. The way to create a "mask" bitmap in the "Mask" bitmap device description table is: Create a monochrome Bitmap, load the Mask DC, then, the bitmap is installed in the way "srccopy". Figure Device Description Draw (Bitblt) to Mask DC. Thus, the bitmap in the Mask DC display plane is a "mask" bitmap.
Draw the actual steps of the "transparent" bitmap:
1. Draw a bitmap device description table to draw (bitblt) to the display device descriptor in "SRCINVERT";
2. Draw the "Mask" bitmap device description table with "srcand" to the display device description table;
3. Draw a bitmap device description table to draw (bitblt) to the display device descriptor in the manner of "SRCINVERT". In addition, except "transparent color" is drawn to the window.
The implementation code is as follows:
Void DrawTransparent (CDC * PDC, INT X, INT Y, ColorRef Crcolour)
{ColorRef croldback = PDC-> SetBkcolor (RGB (255, 255, 255));
ColorRef croldtext = PDC-> setTextColor (RGB (0,0,0));
CDC DCIMAGE, DCMASK;
CBITMAP BMP;
Bmp.LoadBitmap (IDB-Trans);
// IDB-TRANS is the resource ID of the bitmap to be displayed
Bitmap Bm; Bmp.GetBitmap (& BM);
INT NWIDTH = BM.BMWIDTH, NHEIGHT = BM.BmHeight;
/ / Create a DC for images and Mask
DCIMAGE.CREATECOMPALEDC (PDC);
DCMask.createCompatiPLEDC (PDC);
// put the image into the image DC
CBITMAP * PoldbitMapImage = DCIMAGE.SELECTOBJECT (& BMP);
/ / Create a monochrome Bitmap for the "Mask" bitmap
CBITMAP Bitmapmask;
Bitmapmask.createBitmap (NWidth, NHEIGHT, 1, 1, NULL); // put the Mask bitmap into Mask DC
CBitmap * PoldbitMapmask = DCMASK.SelectObject (& Bitmapmask); // Create a "Mask" bitmap with transparent color
DCImage.setBkcolor (crcolour); // crcolor is transparent color in a bitmap
DCMask.bitblt (0, 0, NWIDTH, NHEIGHT, & DCIMAGE, 0, 0, SRCCPY); // division 3 steps for practical drawing
PDC-> Bitblt (x, y, nwidth, nheight, & dcimage, 0, 0, srcinvert);
PDC-> Bitblt (x, y, nwidth, nheight, & dcmask, 0, 0, srcand); PDC-> Bitblt (x, y, nwidth, nHEight, & DCImage, 0, 0, srcinvert); // Restore original settings
DCImage.selectObject (PoldbitmapImage);
DCMASK.SelectObject (Poldbitmapmask);
PDC-> setbkcolor (croldback);
PDC-> SetTextColor (croldtext);
The above-described "transparent" bitmap drawing can also be used in animation techniques, such as changing characters or animals drawn in a scene, can draw "transparent" in the background by using the character or animal bitmap " achieve.