在windows CE下运行通过的代码:
-
C/C++ code
-
void TransparentBltImage(HDC hdcDest, int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest,HDC hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc,UINT crTransparent)
{
HBITMAP hOldImageBMP,hImageBMP = CreateCompatibleBitmap(hdcDest,nWidthDest,nHeightDest); // 创建兼容位图
HBITMAP hOldMaskBMP,hMaskBMP = CreateBitmap(nWidthDest,nHeightDest, 1 , 1 ,NULL); // 创建单色掩码位图
HDC hImageDC = CreateCompatibleDC(hdcDest);
HDC hMaskDC = CreateCompatibleDC(hdcDest);
hOldImageBMP = (HBITMAP)SelectObject(hImageDC,hImageBMP);
hOldMaskBMP = (HBITMAP)SelectObject(hMaskDC,hMaskBMP);
// 将源DC中的位图拷贝到临时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);
// 设置透明色
SetBkColor(hImageDC,crTransparent);
// 生成透明区域为白色,其它区域为黑色的掩码位图
BitBlt(hMaskDC, 0 , 0 ,nWidthDest,nHeightDest,hImageDC, 0 , 0 ,SRCCOPY);
// 生成透明区域为黑色,其它区域保持不变的位图
SetBkColor(hImageDC,RGB( 0 , 0 , 0 ));
SetTextColor(hImageDC,RGB( 255 , 255 , 255 ));
BitBlt(hImageDC, 0 , 0 ,nWidthDest,nHeightDest,hMaskDC, 0 , 0 ,SRCAND);
// 透明部分保持屏幕不变,其它部分变成黑色
SetBkColor(hdcDest,RGB( 255 , 255 , 255 ));
SetTextColor(hdcDest,RGB( 0 , 0 , 0 ));
BitBlt(hdcDest,nXOriginDest,nYOriginDest,nWidthDest,nHeightDest,hMaskDC, 0 , 0 ,SRCAND);
// "或"运算,生成最终效果
BitBlt(hdcDest,nXOriginDest,nYOriginDest,nWidthDest,nHeightDest,hImageDC, 0 , 0 ,SRCPAINT);
// 清理、恢复
SelectObject(hImageDC,hOldImageBMP);
DeleteDC(hImageDC);
SelectObject(hMaskDC,hOldMaskBMP);
DeleteDC(hMaskDC);
DeleteObject(hImageBMP);
DeleteObject(hMaskBMP);
} -
-
-
调用实例:
-
C/C++ code
-
CFile file;
if ( ! file.Open(StrBmpName,CFile::modeRead | CFile::shareDenyWrite))
{
return ;
}
BITMAPFILEHEADER bfhHeader;
file.Read( & bfhHeader, sizeof (BITMAPFILEHEADER));
if (bfhHeader.bfType != ((WORD) ( ' M ' << 8 ) | ' B ' ))
return ;
if (bfhHeader.bfSize != file.GetLength())
return ;
UINT uBmpInfoLen = (UINT) bfhHeader.bfOffBits - sizeof (BITMAPFILEHEADER);
LPBITMAPINFO lpBitmap = (LPBITMAPINFO) new BYTE[uBmpInfoLen];
file.Read((LPVOID) lpBitmap,uBmpInfoLen);
if (( * (LPDWORD)(lpBitmap)) != sizeof (BITMAPINFOHEADER))
return ;
DWORD dwBitlen = bfhHeader.bfSize - bfhHeader.bfOffBits;
LPVOID lpBits = new BYTE[dwBitlen];
file.ReadHuge(lpBits,dwBitlen);
file.Close();
LPVOID lpBits_mem;
fhBitmap = ::CreateDIBSection(fShowDC -> m_hDC,lpBitmap,DIB_RGB_COLORS, & lpBits_mem,NULL, 0 );
if (fhBitmap == NULL)
return ;
hbmp.Attach(fhBitmap);
BITMAP bmpstruct;
hbmp.GetBitmap( & bmpstruct);
DWORD dwCount = (DWORD) bmpstruct.bmWidthBytes * bmpstruct.bmHeight;
memcpy(lpBits_mem,lpBits,dwCount);
poldBitmap = (CBitmap * )fInitShowDC.SelectObject(hbmp);
fWindow.bmpWidth = bmpstruct.bmWidth;
fWindow.bmpHeight = bmpstruct.bmHeight;
// 显示位图
TransparentBltImage(fShowDC -> m_hDC, 0 , 0 ,fWindow.ShowWidth,fWindow.ShowHeight,fInitShowDC.m_hDC, 0 , 0 ,fWindow.bmpWidth,fWindow.bmpHeight,BkColor);
fInitShowDC.SelectObject( & poldBitmap);
-