您的位置:首页 > 其它

创建一不规则窗口

2007-01-07 22:19 417 查看
原方法:


HRGN CRegionCreator::CreateRegionFromBitmap(HBITMAP hBitmap, COLORREF transparentColor)




...{


HRGN hRgn = NULL;


HRGN hRgn1 = NULL;




// Check for valid bitmap handle


if ( hBitmap != NULL )




...{


// Get bitmap object information


BITMAP bitmap;


GetObject(hBitmap, sizeof(BITMAP), &bitmap);


DWORD dwSize = bitmap.bmHeight * bitmap.bmWidthBytes;


int bitsPixel = bitmap.bmBitsPixel / 8;




// Check bitmap color depth (only 24 or 32 bits per pixel allowed)


if ( ( bitsPixel == 3 ) || ( bitsPixel == 4 ) )




...{


// Get bitmap bits


unsigned char* pBits = new unsigned char[dwSize];


GetBitmapBits(hBitmap, dwSize, pBits);




// Create region from bitmap


unsigned char red, green, blue;


for ( int y=0; y<bitmap.bmHeight; y++ )




...{


for ( int x=0; x<bitmap.bmWidth; x++ )




...{


// Get pixel color


blue = pBits[y*bitmap.bmWidthBytes + bitsPixel*x];


green = pBits[y*bitmap.bmWidthBytes + bitsPixel*x+1];


red = pBits[y*bitmap.bmWidthBytes + bitsPixel*x+2];




// Check transparent color


if ( RGB(red,green,blue) != transparentColor )




...{


// Combine regions


if ( hRgn == NULL )


hRgn = CreateRectRgn(x, y, x+1, y+1);


else




...{


// Delete temporary region


if ( hRgn1 != NULL )


DeleteObject(hRgn1);




// Create temporary region


hRgn1 = CreateRectRgn(x, y, x+1, y+1);




// Combine regions


CombineRgn(hRgn, hRgn, hRgn1, RGN_OR);


}


}


}


}




// Free bitmap bits


delete pBits;


}


}




// Delete temporary region


if ( hRgn1 != NULL )


DeleteObject(hRgn1);




return hRgn;


}



改进方法:


void SetupRegion(HWND hWnd, HINSTANCE hInstance, UINT idBmp, COLORREF transCol)




{


COLORREF col;


int cx, cy;


CRgn wndRgn, rgnTemp;




CImage image;


//将模板位图选进设备场景中


image.LoadFromResource(hInstance, idBmp);


cx = image.GetWidth();


cy = image.GetHeight();




//创建模板形状的不规则区域


wndRgn.CreateRectRgn(0, 0, cx, cy);


for(int x=0; x<cx; x++)




{


for(int y=0; y<cy; y++)




{


//将位图中对应的透明色区域抠掉


col = image.GetPixel(x, y);


if(col == transCol)




{


rgnTemp.CreateRectRgn(x, y, x+1, y+1);


wndRgn.CombineRgn(&wndRgn, &rgnTemp, RGN_XOR);


rgnTemp.DeleteObject();


}


}


}


//将不规则区域分配给窗体,也就是创建不规则的窗体


SetWindowRgn(hWnd, wndRgn, TRUE);


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: