GDI+把窗口剪裁//是GDI函数行吗????
if (mask>0){
m_bMask.LoadBitmap(mask);//以mask图是,255,255,255的剪裁去
if (hClipRgn) DeleteObject(hClipRgn);
hClipRgn = CreateRgnFromBitmap(m_bMask,RGB(255,255,255));
if (hClipRgn){
SetWindowRgn(hClipRgn, TRUE);
CDC *pDC = GetDC();
if (pDC){
SelectClipRgn(pDC->GetSafeHdc(), hClipRgn);
ReleaseDC(pDC);
}
}
if (m_DrawMode==0){
SetWindowPos(NULL,0,0,GetBitmapWidth(m_bMask),
GetBitmapHeight(m_bMask),SWP_NOZORDER|SWP_NOMOVE);
}
}
HRGN CxSkinButton::CreateRgnFromBitmap(HBITMAP hBmp, COLORREF color)//创建一个剪裁区
{
if (!hBmp) return NULL;
BITMAP bm;
GetObject( hBmp, sizeof(BITMAP), &bm );// get bitmap attributes
CDC dcBmp;
CDC *pDC = GetDC();
if (!pDC) return NULL;
dcBmp.CreateCompatibleDC(pDC);//Creates a memory device context for the bitmap
ReleaseDC(pDC);
HBITMAP hOldBitmap = (HBITMAP)dcBmp.SelectObject(hBmp);//selects the bitmap in the device context
const DWORD RDHDR = sizeof(RGNDATAHEADER);
const DWORD MAXBUF = 40;// size of one block in RECTs
// (i.e. MAXBUF*sizeof(RECT) in bytes)
LPRECTpRects;
DWORDcBlocks = 0;// number of allocated blocks
INTi, j;// current position in mask image
INTfirst = 0;// left position of current scan line
// where mask was found
boolwasfirst = false;// set when if mask was found in current scan line
boolismask;// set when current color is mask color
// allocate memory for region data
RGNDATAHEADER* pRgnData = (RGNDATAHEADER*)new BYTE[ RDHDR + ++cBlocks * MAXBUF * sizeof(RECT) ];
memset( pRgnData, 0, RDHDR + cBlocks * MAXBUF * sizeof(RECT) );
// fill it by default
pRgnData->dwSize= RDHDR;
pRgnData->iType= RDH_RECTANGLES;
pRgnData->nCount= 0;
for ( i = 0; i < bm.bmHeight; i++ )
for ( j = 0; j < bm.bmWidth; j++ ){
// get color
ismask=(dcBmp.GetPixel(j,bm.bmHeight-i-1)!=color);
// place part of scan line as RECT region if transparent color found after mask color or
// mask color found at the end of mask image
if (wasfirst && ((ismask && (j==(bm.bmWidth-1)))||(ismask ^ (j<bm.bmWidth)))){
// get offset to RECT array if RGNDATA buffer
pRects = (LPRECT)((LPBYTE)pRgnData + RDHDR);
// save current RECT
pRects[ pRgnData->nCount++ ] = CRect( first, bm.bmHeight - i - 1, j+(j==(bm.bmWidth-1)), bm.bmHeight - i );
// if buffer full reallocate it
if ( pRgnData->nCount >= cBlocks * MAXBUF ){
LPBYTE pRgnDataNew = new BYTE[ RDHDR + ++cBlocks * MAXBUF * sizeof(RECT) ];
memcpy( pRgnDataNew, pRgnData, RDHDR + (cBlocks - 1) * MAXBUF * sizeof(RECT) );
delete pRgnData;
pRgnData = (RGNDATAHEADER*)pRgnDataNew;
}
wasfirst = false;
} else if ( !wasfirst && ismask ){// set wasfirst when mask is found
first = j;
wasfirst = true;
}
}
dcBmp.SelectObject( hOldBitmap );
dcBmp.DeleteDC();//release the bitmap
// create region
/* Under WinNT the ExtCreateRegion returns NULL (by Fable@aramszu.net) */
//HRGN hRgn = ExtCreateRegion( NULL, RDHDR + pRgnData->nCount * sizeof(RECT), (LPRGNDATA)pRgnData );
/* ExtCreateRegion replacement { */
HRGN hRgn=CreateRectRgn(0, 0, 0, 0);
ASSERT( hRgn!=NULL );
pRects = (LPRECT)((LPBYTE)pRgnData + RDHDR);
for(i=0;i<(int)pRgnData->nCount;i++)
{
HRGN hr=CreateRectRgn(pRects[i].left, pRects[i].top, pRects[i].right, pRects[i].bottom);
VERIFY(CombineRgn(hRgn, hRgn, hr, RGN_OR)!=ERROR);
if (hr) DeleteObject(hr);
}
ASSERT( hRgn!=NULL );
/* } ExtCreateRegion replacement */
delete pRgnData;
return hRgn;
}