1. 程式人生 > >【win32】最簡單的異形視窗實現

【win32】最簡單的異形視窗實現

void CTransparentWnd::SetupRegion(CDC *pDC, unsigned short MaskID)
{
 CDC     memDC;
 CBitmap   cBitmap;
 CBitmap*  pOldMemBmp = NULL;
 COLORREF  col;
 CRect    cRect;
 int     x, y;
 CRgn    wndRgn, rgnTemp;

 GetWindowRect(&cRect);

 cBitmap.LoadBitmap(MaskID);
 memDC.CreateCompatibleDC(pDC);
 pOldMemBmp = memDC.SelectObject(&cBitmap);

 wndRgn.CreateRectRgn(0, 0, cRect.Width(), cRect.Height());
 for(x=0; x<=cRect.Width(); x++)
 {
  for(y=0; y<=cRect.Height(); y++)
  {
   col = memDC.GetPixel(x, y);
   if(col == 0)
   {
    rgnTemp.CreateRectRgn(x, y, x+1, y+1);
    wndRgn.CombineRgn(&wndRgn, &rgnTemp, RGN_XOR);
    rgnTemp.DeleteObject(); 
   }
  }
 }

 if (pOldMemBmp) memDC.SelectObject(pOldMemBmp);
 SetWindowRgn((HRGN)wndRgn, TRUE);
}
void CTransparentWnd::OnPaint()
{
 CPaintDC dc(this);

 // Add your drawing code here!
 CRect rect;
 GetWindowRect(&rect);
 
 CDC memDC;
 CBitmap   cBitmap;
 CBitmap*  pOldMemBmp = NULL;
 
 cBitmap.LoadBitmap(m_BitmapID);
 memDC.CreateCompatibleDC(pDC);
 pOldMemBmp = memDC.SelectObject(&cBitmap);
 pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &memDC, 0, 0, SRCCOPY);
 
 if (pOldMemBmp) memDC.SelectObject( pOldMemBmp );
}