1. 程式人生 > >VC透掉背景色

VC透掉背景色

//透掉背景色
 //CBitmap   m_bit;
 //m_bit.LoadBitmap(strBmpPath);
 //HBITMAP bBit = (HBITMAP)m_bit.m_hObject;
 //if (GetObjectType(bBit) == OBJ_BITMAP)
 //{
 // CSize csBitmap = GetBitmapSize(bBit);
 // m_Bitmap.Attach(bBit);
 // CClientDC dc(this);
 // SetupRegion(&dc,m_Bitmap,RGB(255,0,255)); 
 // //調整到背景圖片大小
 // CSize size = GetImageSize(BmpMain);
 // SetWindowPos(NULL,0,0,size.cx,size.cy,SWP_NOZORDER);
 //}
 
 
HBITMAP LoadResImageBMP(LPCTSTR bmpName)
{
 HBITMAP hBitmap = NULL;
 int tries = 3;
 
 //2009-12-05 by zhuhaiyan
 do 
 {
  hBitmap = (HBITMAP)LoadImage(
   NULL,  
   GetResourcePath() + bmpName, 
   IMAGE_BITMAP, 
   0, 
   0, 
   (LR_CREATEDIBSECTION | LR_LOADFROMFILE)
   );
 } while (hBitmap ==  NULL && (tries--) > 0);
 return hBitmap;
}
 
CSize GetBitmapSize(HBITMAP hBitmap)
{
 if (hBitmap == NULL)
 {
  return CSize(0, 0);
 }
 PBITMAPINFO pBtimapInfo;
 if (!GetBitmapHeaderInfo(hBitmap, pBtimapInfo))
 {
  return 0;
 }
 UINT nWidth = pBtimapInfo->bmiHeader.biWidth;
 UINT nHeight = pBtimapInfo->bmiHeader.biHeight;
 CSize csBitmap(nWidth, nHeight);
 if (pBtimapInfo)
 {
  free(pBtimapInfo);
 }
 return csBitmap;
}
void SetupRegion(CDC *pDC, CBitmap &cBitmap, COLORREF TransColor)
{
 CDC memDC;
 //建立與傳入DC相容的臨時DC
 memDC.CreateCompatibleDC(pDC);
 CBitmap *pOldMemBmp=NULL;
 //將點陣圖選入臨時DC
 pOldMemBmp=memDC.SelectObject(&cBitmap);
 CRgn wndRgn;
 //建立總的窗體區域,初始region為0
 wndRgn.CreateRectRgn(0,0,0,0);


 BITMAP bit;   
 cBitmap.GetBitmap (&bit);//取得點陣圖引數,這裡要用到點陣圖的長和寬    
 int y;
 for(y=0;y<=bit.bmHeight  ;y++)
 {
  CRgn rgnTemp; //儲存臨時region
  int iX = 0;
  do
  {
   //跳過透明色找到下一個非透明色的點.
   while (iX <= bit.bmWidth  && memDC.GetPixel(iX, y) == TransColor)
    iX++;
   //記住這個起始點
   int iLeftX = iX;
   //尋找下個透明色的點
   while (iX <= bit.bmWidth  && memDC.GetPixel(iX, y) != TransColor)
    ++iX;
   //建立一個包含起點與重點間高為1畫素的臨時“region”
   rgnTemp.CreateRectRgn(iLeftX, y, iX, y+1);
   //合併到主"region".
   wndRgn.CombineRgn(&wndRgn, &rgnTemp, RGN_OR);
   //刪除臨時"region",否則下次建立時和出錯
   rgnTemp.DeleteObject();
  }while(iX <bit.bmWidth );
  iX = 0;
 }
 if(pOldMemBmp)
  memDC.SelectObject(pOldMemBmp);
 CWnd * pWnd = pDC->GetWindow();
 pWnd->SetWindowRgn(wndRgn,TRUE);    
 pWnd->SetForegroundWindow();   
}