Current location - Training Enrollment Network - Books and materials - How to display a pair in MFC to make its background transparent
How to display a pair in MFC to make its background transparent
There are many ways to draw a bitmap with transparent colors. The easiest way is to call a ready-made function: TransparentBlt, or you can implement a function similar to TransparentBlt through your own code. There are two ways to realize it, one is to make the mask bitmap in advance, and the other is to generate the mask bitmap dynamically. This paper will introduce the method of dynamically generating mask bitmap to draw bitmap with transparent area.

First, use the TransparentBlt function

The TransparentBlt function runs in the version above Windows98/Windows2000, and it needs to include Msimg32.lib in the system, and it can be linked when used.

TransparentBlt under Windows98 will cause resource leakage, so it is not recommended to use this function under WIN98.

The TransparentBlt function prototype is as follows:

Booltransparent BLT (hdchhdcdest,//target DCint nXOriginDest,// target X offset int nYOriginDest,// target Y offset int nWidthDest, //target width int hheighdest, // target height hdchhdcsrc, //source DCint nXOriginSrc, // source X starting point int nYOriginSrc, // source Y starting point int nWidthSrc, // source width int nHeightSrc,//sourceheight uint Cr transparent//transparent color, COLORREF type); Use example:

CBitmap FootballBMP

Football BMP. load bitmap(IDB _ football BMP);

CDC ImageDC

ImageDC。 CreateCompatibleDC(pDC);

CBitmap *pOldImageBMP = ImageDC。 Select object (& soccer BMP);

Transparent BLT (PDC->; m_hDC,0,0,2 18, 199,ImageDC.m_hDC,0,0,2 18, 199,RGB(0,0,0x ff)); //RGB (0,0,0xff) changes to your background color.

ImageDC。 select object(pOldImageBMP);