半透明显示图片

 /*功能说明:将前景图和背景图的颜色混合处理后,处理的公式是;前景图=前景图*(1-radio)+背景图
  参数说明: CDC *pdc--要贴图的设备
            CRect rect--贴图的矩形区域
            int xStart,int yStart--前景图的左上角的坐标
            HBITMAP hBgBMP--背景图的句柄
            HBITMAP hForBMP--前景图的句柄
            float radio--透明程度,默认为0.7;0--为前景图完全透明;
*/
HalfTransparent(CDC *pdc, CRect rect, int xStart, int yStart, HBITMAP hBgBMP, HBITMAP hForBMP,float radio)
{
 CDC memdc;
 BITMAP bmBG,bmFor;
 unsigned char *px1,*px2;
 memdc.CreateCompatibleDC(pdc);
 
 CBitmap* bmpBG=CBitmap::FromHandle(hBgBMP);           //获得背景图片的信息
    bmpBG->GetBitmap(&bmBG);
    px1=new unsigned char[bmBG.bmHeight*bmBG.bmWidthBytes];
 bmpBG->GetBitmapBits(bmBG.bmHeight*bmBG.bmWidthBytes,px1);
   
 CBitmap* bmpFor=CBitmap::FromHandle(hForBMP);       //获得前景图片的信息
 bmpFor->GetBitmap(&bmFor);
 px2=new unsigned char[bmFor.bmHeight*bmFor.bmWidthBytes];
 bmpFor->GetBitmapBits(bmFor.bmHeight*bmFor.bmWidthBytes,px2);

 int xEnd,yEnd,i,j;
 xEnd=xStart+bmFor.bmWidth;
 yEnd=yStart+bmFor.bmHeight;
   
 int pxBits=bmBG.bmBitsPixel/8;
    int rgb_b;
 for(i=yStart;i<yEnd;i++)
 {
  for(j=xStart;j<xEnd;j++)
  {
            rgb_b=j*pxBits+i*bmBG.bmWidthBytes;
            px1[rgb_b]*=radio;
   px1[rgb_b+1]*=radio;
   px1[rgb_b+2]*=radio;
  }
 }
   
 for(i=0;i<bmFor.bmHeight;i++)
 {
  for(j=0;j<bmFor.bmWidth;j++)
  {
   rgb_b=j*pxBits+i*bmFor.bmWidthBytes;
   int m=(i+yStart)*bmBG.bmWidthBytes+(j+xStart)*pxBits;
   px2[rgb_b]=px2[rgb_b]*(1-radio)+px1[m];
            px2[rgb_b+1]=px2[rgb_b+1]*(1-radio)+px1[m+1];
   px2[rgb_b+2]=px2[rgb_b+2]*(1-radio)+px1[m+2];
  }
 }
 bmpFor->SetBitmapBits(bmFor.bmWidthBytes*bmFor.bmHeight,px2);
 memdc.SelectObject(bmpBG);
 pdc->BitBlt(0,0,rect.Width(),rect.Height(),&memdc,0,0,SRCCOPY);
 memdc.SelectObject(bmpFor);
 pdc->BitBlt(xStart,yStart,bmFor.bmWidth,bmFor.bmHeight,&memdc,0,0,SRCCOPY);
 memdc.DeleteDC();
 bmpFor->DeleteObject();
 bmpBG->DeleteObject();
 delete []px1;
 delete []px2;
}
函数声明:
void HalfTransparent(CDC *pdc,CRect rect,int xStart,int yStart,HBITMAP hBgBMP,HBITMAP  hForBMP,float radio=0.7);

posted @ 2006-11-21 21:23  巨巨  阅读(289)  评论(0编辑  收藏  举报