In the previous article, we talked about how to make transparent textures. In fact, they are mainly used in the creation of some irregular control UIS. The method used is to create a mask bitmap and then use some grating operations provided by bitblt for transparent rendering.
Sometimes people may only provide a bitmap that requires transparent display, but do not provide a mask bitmap synchronously. What should I do? Do we have to provide or make one by ourselves? Too much trouble. Is there a simple program implementation method? This article will briefly introduce how to use a program to create a mask bitmap for a given bitmap and then draw a transparent effect.
In fact, when using imagelist, we should know that if imagelist is an icon, the icon itself can be transparent, so no transparent color needs to be specified; however, if the imagelist item is bitmap and we want to draw it transparently, we need to define a transparent color, that is, the bitmap mask color. Similarly, for a given bitmap, you must specify a mask color maskcolor for it.
The following describes the entire process.
Assume that a bitmap with the resource ID idb_bitmap1 needs to be drawn in the preparation window. The method is as follows:
1. Create compatible DC, load the specified bitmap resource, and select it to the compatible DC created earlier
Hbitmap hbmp =: loadbitmap (hinst, makeintresource (idb_bitmap1 ));
Bitmap BMP Info;
: GetObject (hgdiobj) hbmp, sizeof (Bitmap), & BMP info );
Hwnd hdesktop =: getdomaintopwindow ();
HDC hsf-topdc =: getdc (hdesktop );
HDC hsrcdc =: createcompatibledc (hsf-topdc );
Hbitmap hsrcbitmap =: createcompatiblebitmap (hsf-topdc, BMP info. bmwidth, BMP info. bmheight );
: SelectObject (hsrcdc, hbmp );
2. Create a monochrome mask Bitmap (the normal size of the source image, where the mask color of the source image is white and the others are black)
: Setbkcolor (hsrcdc, RGB (0xff, 0, 0xff); // set the transparent color to the background color.
HDC hmaskdc =: createcompatibledc (hsf-topdc );
Hbitmap hmaskbmp =: createbitmap (BMP info. bmwidth, BMP info. bmheight, 1, 1, 0 );
Hbitmap holdmaskbmp = (hbitmap): SelectObject (hmaskdc, hmaskbmp );
: Bitblt (hmaskdc, 0, 0, BMP info. bmwidth, BMP info. bmheight, hsrcdc, 0, 0, srccopy );
3. Modify the source image to make its color part black.
: Setbkcolor (hsrcdc, RGB (0, 0, 0 ));
: Settextcolor (hsrcdc, RGB (0xff, 0xff, 0xff ));
: Bitblt (hsrcdc, 0, 0, BMP info. bmwidth, BMP info. bmheight, hmaskdc, 0, 0, srcand );
4. Obtain the DC of the window to be drawn and draw it transparently.
HDC hdcmainwnd =: getdc (g_hmainwnd); // The DC of the window to be drawn
: Setbkcolor (hdcmainwnd, RGB (0xff, 0xff, 0xff ));
: Settextcolor (hdcmainwnd, RGB (0, 0, 0 ));
: Bitblt (hdcmainwnd, 0, 0, BMP info. bmwidth, BMP info. bmheight, hmaskdc, 0, 0, srcand );
: Bitblt (hdcmainwnd, 0, 0, BMP info. bmwidth, BMP info. bmheight, hsrcdc, 0, 0, srcpaint );
This is also the four steps above. Here we mainly use several bitblt operations. Note that in bitblt operations, if the bitmap formats of srcdc and dstdc are different, the corresponding format conversion is required. That is, the conversion rules from a monochrome bitmap to a color bitmap. Let's take a look at the description of bitblt on msdn to make full use of the foreground color and background color of dstdc. This is why setbkcolor and settextcolor are frequently called in the above Code. Almost all resources are not deleted (releasedc) or restored (SelectObject, etc.) because it is only used to explain the drawing principle ).
From http://blog.csdn.net/doudouhuy/article/details/4218548