WEB开发网
开发学院软件开发VC 透明位图的显示 阅读

透明位图的显示

 2008-02-26 20:26:57 来源:WEB开发网   
核心提示:2.2 TransparentBlt2函数全部实现代码void TransparentBlt2( HDC hdcDest,// 目标DCint nXOriginDest, // 目标X偏移int nYOriginDest, // 目标Y偏移int nWidthDest, // 目标宽度int nHeightDest,/

2.2 TransparentBlt2函数全部实现代码

void TransparentBlt2( HDC hdcDest,   // 目标DC
          int nXOriginDest,  // 目标X偏移
          int nYOriginDest,  // 目标Y偏移
          int nWidthDest,   // 目标宽度
          int nHeightDest,  // 目标高度
          HDC hdcSrc,     // 源DC
          int nXOriginSrc,  // 源X起点
          int nYOriginSrc,  // 源Y起点
          int nWidthSrc,   // 源宽度
          int nHeightSrc,   // 源高度
          UINT crTransparent // 透明色,COLORREF类型
          )
{
  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);
}

2.3 TransparentBlt的另外一个版本:TransparentBltU

TransparentBltU是Christian Graus 在WinDEV发表的一个函数,功能与TransparentBlt一致,以下是全部实现代码:

bool TransparentBltU(
   HDC dcDest,     // handle to Dest DC
   int nXOriginDest,  // x-coord of destination upper-left corner
   int nYOriginDest,  // y-coord of destination upper-left corner
   int nWidthDest,   // width of destination rectangle
   int nHeightDest,  // height of destination rectangle
   HDC dcSrc,     // handle to source DC
   int nXOriginSrc,  // x-coord of source upper-left corner
   int nYOriginSrc,  // y-coord of source upper-left corner
   int nWidthSrc,   // width of source rectangle
   int nHeightSrc,   // height of source rectangle
   UINT crTransparent // color to make transparent
 )
{
   if (nWidthDest < 1) return false;
   if (nWidthSrc < 1) return false;
   if (nHeightDest < 1) return false;
   if (nHeightSrc < 1) return false;
   HDC dc = CreateCompatibleDC(NULL);
   HBITMAP bitmap = CreateBitmap(nWidthSrc, nHeightSrc, 1, GetDeviceCaps(dc,
                               BITSPIXEL), NULL);
   if (bitmap == NULL)
   {
     DeleteDC(dc);  
     return false;
   }
   HBITMAP oldBitmap = (HBITMAP)SelectObject(dc, bitmap);
   if (!BitBlt(dc, 0, 0, nWidthSrc, nHeightSrc, dcSrc, nXOriginSrc,
                             nYOriginSrc, SRCCOPY))
   {
     SelectObject(dc, oldBitmap);
     DeleteObject(bitmap);    
     DeleteDC(dc);        
     return false;
   }
   HDC maskDC = CreateCompatibleDC(NULL);
   HBITMAP maskBitmap = CreateBitmap(nWidthSrc, nHeightSrc, 1, 1, NULL);
   if (maskBitmap == NULL)
   {
     SelectObject(dc, oldBitmap);
     DeleteObject(bitmap);    
     DeleteDC(dc);        
     DeleteDC(maskDC);      
     return false;
   }
   HBITMAP oldMask = (HBITMAP)SelectObject(maskDC, maskBitmap);
   SetBkColor(maskDC, RGB(0,0,0));
   SetTextColor(maskDC, RGB(255,255,255));
   if (!BitBlt(maskDC, 0,0,nWidthSrc,nHeightSrc,NULL,0,0,BLACKNESS))
   {
     SelectObject(maskDC, oldMask);
     DeleteObject(maskBitmap);   
     DeleteDC(maskDC);       
     SelectObject(dc, oldBitmap); 
     DeleteObject(bitmap);     
     DeleteDC(dc);         
     return false;
   }
   SetBkColor(dc, crTransparent);
   BitBlt(maskDC, 0,0,nWidthSrc,nHeightSrc,dc,0,0,SRCINVERT);
   SetBkColor(dc, RGB(0,0,0));
   SetTextColor(dc, RGB(255,255,255));
   BitBlt(dc, 0,0,nWidthSrc,nHeightSrc,maskDC,0,0,SRCAND);
   HDC newMaskDC = CreateCompatibleDC(NULL);
   HBITMAP newMask;
   newMask = CreateBitmap(nWidthDest, nHeightDest, 1,
                  GetDeviceCaps(newMaskDC, BITSPIXEL), NULL);
   if (newMask == NULL)
   {
     SelectObject(dc, oldBitmap);
     DeleteDC(dc);
     SelectObject(maskDC, oldMask);
     DeleteDC(maskDC);
     DeleteDC(newMaskDC);
     DeleteObject(bitmap);  
     DeleteObject(maskBitmap);
     return false;
   }
   SetStretchBltMode(newMaskDC, COLORONCOLOR);
   HBITMAP oldNewMask = (HBITMAP) SelectObject(newMaskDC, newMask);
   StretchBlt(newMaskDC, 0, 0, nWidthDest, nHeightDest, maskDC, 0, 0,
                        nWidthSrc, nHeightSrc, SRCCOPY);
   SelectObject(maskDC, oldMask);
   DeleteDC(maskDC);
   DeleteObject(maskBitmap);
   HDC newImageDC = CreateCompatibleDC(NULL);
   HBITMAP newImage = CreateBitmap(nWidthDest, nHeightDest, 1,
                  GetDeviceCaps(newMaskDC, BITSPIXEL), NULL);
   if (newImage == NULL)
   {
     SelectObject(dc, oldBitmap);
     DeleteDC(dc);
     DeleteDC(newMaskDC);
     DeleteObject(bitmap);  
     return false;
   }
   HBITMAP oldNewImage = (HBITMAP)SelectObject(newImageDC, newImage);
   StretchBlt(newImageDC, 0, 0, nWidthDest, nHeightDest, dc, 0, 0, nWidthSrc,
                             nHeightSrc, SRCCOPY);
   SelectObject(dc, oldBitmap);
   DeleteDC(dc);
   DeleteObject(bitmap);  
   BitBlt( dcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest,
                           newMaskDC, 0, 0, SRCAND);
   BitBlt( dcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest,
                          newImageDC, 0, 0, SRCPAINT);
   SelectObject(newImageDC, oldNewImage);
   DeleteDC(newImageDC);
   SelectObject(newMaskDC, oldNewMask);
   DeleteDC(newMaskDC);
   DeleteObject(newImage); 
   DeleteObject(newMask);  
   return true;
}

说明:本文提供的TransparentBlt2函数旨在说明透明位图的显示原理,在Windows2000以上环境实际运用中建议使用现成的TransparentBlt函数来绘制透明位图。

上一页  1 2 

Tags:透明 位图 显示

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接