三个实现透明效果的方法(我还没有试过,不保证质量)

 方法一:

    void CChessBoard::FillMarkedChess(CDC *pDC,CRect rect, COLORREF color)
    {
    //color为半透明的底色
   COLORREF    bkcolor;
   int R1,G1,B1,R2,G2,B2;
   R1=color&255;
   G1=(color>>8)&255;
   B1=(color>>16)&255;
   //标记块的左边与右边各多显示一点
   for(int x=rect.left-1;x<=rect.right;x++)
   {
       for(int y=rect.top-1;y<=rect.bottom;y++)
       {
           //nRatio为透光率
           bkcolor=pDC->GetPixel(x,y);
           R2=bkcolor&255;
           G2=(bkcolor>>8)&255;
           B2=(bkcolor>>16)&255;
           R2=R1+(R2-R1)*m_nRatio/100;
           G2=G1+(G2-G1)*m_nRatio/100;
           B2=B1+(B2-B1)*m_nRatio/100;
           pDC->SetPixel(x,y,RGB(R2,G2,B2));
       }
   }
   }  

方法二:

/*
这是一个用来填充透明位图的函数
CDC *pDC 需要画位图的CDC指针
HBITMAP hBitmap 位图's m_hObject
CRect &rect 指定位图在pDC中的位置
COLORREF rgbMask 位图的透明色
*/
void FillTransparentHBitmap(CDC *pDC, HBITMAP hBitmap,CRect &rect, COLORREF rgbMask)
{
   CDC ImageDC,MaskDC;
   CBitmap Image,*pOldImage=NULL;
   CBrush *oldBrush=NULL;
   CBitmap maskBitmap,*pOldMaskDCBitmap ;

   //Image.LoadBitmap(IDImage);
   Image.Attach(hBitmap);

   ImageDC.CreateCompatibleDC(pDC);
   //CBrush br(&Image);
   pOldImage=ImageDC.SelectObject(&Image);
   //ImageDC.FillRect(rect,&br);
   BITMAP bm;
   Image.GetBitmap(&bm);

   MaskDC.CreateCompatibleDC(pDC);
   maskBitmap.CreateBitmap( rect.Width(), rect.Height(), 1, 1, NULL );
   pOldMaskDCBitmap = MaskDC.SelectObject( &maskBitmap );

   ImageDC.SetBkColor(rgbMask);
   int ix=0,iy=0;
   for(iy=0;iy   {
       for(ix=0;ix       {
           MaskDC.BitBlt( ix, iy, bm.bmWidth, bm.bmHeight, &ImageDC, 0, 0, SRCCOPY );
       }
   }

   ImageDC.SetBkColor(RGB(0,0,0));
   ImageDC.SetTextColor(RGB(255,255,255));
   ImageDC.BitBlt(0, 0, rect.Width(), rect.Height(), &MaskDC, 0, 0, SRCAND);

   pDC->BitBlt(rect.left,rect.top,rect.Width(), rect.Height(), &MaskDC, 0, 0, SRCAND);
   for(iy=rect.top;iy   {
       for(ix=rect.left;ix       {
           //MaskDC.BitBlt( ix, iy, bm.bmWidth, bm.bmHeight, &ImageDC, 0, 0, SRCCOPY );
           pDC->BitBlt(ix,iy,bm.bmWidth, bm.bmHeight, &ImageDC, 0, 0,SRCPAINT);
       }
   }
   //pDC->BitBlt(rect.left,rect.top,rect.Width(), rect.Height(), &ImageDC, 0, 0,SRCPAINT);

   MaskDC.SelectObject(pOldMaskDCBitmap);
   ImageDC.SelectObject(pOldImage);
   //ImageDC.SelectObject(oldBrush);

   Image.Detach();
}
 

方法三:

BOOL BitBlt_HT(
HDC hdc,//目標DC
RECT rect,//目標區域
COLORREF maskColor,//半透明前景
UINT maskRatio//半透明前景比重 ( 0 - 100 )
)
{
   int x,y;
   COLORREF bColor;
   BYTE b_R,b_G,b_B,f_R,f_G,f_B;
   
   maskRatio = maskRatio % 100;
   f_R = GetRvalues(maskColor);f_G = GetGvalues(maskColor);f_B = GetBvalues(maskColor);

   HDC hMemDC = ::CreateCompatibleDC(hdc);
   if (hMemDC == NULL) return 0;
   HBITMAP hOldBmp,hNewBmp = ::CreateCompatibleBitmap(hdc,rect.right - rect.left ,rect.bottom - rect.top );
   if (hNewBmp == NULL)
   {
       ::DeleteDC (hMemDC);
       return 0;
   }
   hOldBmp = (HBITMAP)::SelectObject(hMemDC,hNewBmp);
   ::BitBlt (hMemDC,0,0 ,rect.right-rect.left ,rect.bottom - rect.top ,hdc,rect.left ,rect.top,SRCCOPY);
   
   for (y = 0 ; y    {
       for (x = 0 ; x <= rect.right - rect.top; x++)
       {
           bColor = GetPixel(hMemDC,x,y);
           b_R = GetRvalues(bColor);b_G = GetGvalues(bColor);b_B = GetBvalues(bColor);
           bColor = RGB(f_R*maskRatio/100+b_R*(100-maskRatio)/100,f_G*maskRatio/100+b_G*(100-maskRatio)/100,f_B*maskRatio/100+b_B*(100-maskRatio)/100);
           SetPixel(hMemDC,x,y,bColor);
       }
   }
   ::BitBlt (hdc,rect.left ,rect.top ,rect.right-rect.left ,rect.bottom - rect.top ,hMemDC,0,0,SRCCOPY);
   ::SelectObject(hMemDC,hOldBmp);
   ::DeleteObject (hNewBmp);
   ::DeleteDC (hMemDC);
   return 1;
}

你可能感兴趣的:(VC++技术)