创建一不规则窗口
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);
}
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);
}
相关文章推荐
- 用Win32 API创建不规则窗口
- 创建不规则窗口
- 用VB创建不规则窗口
- MFC创建不规则窗口
- 创建 WPF 不规则窗口 方法之二
- mfc创建不规则窗口
- C#创建不规则窗体(窗口)的几种方式
- SetLayeredWindowAttributes 创建不规则窗口(此处为椭圆)
- SetWindowRgn,可用于创建不规则窗口
- 用VB创建不规则窗口
- VC创建不规则窗口
- 以BMP图片为蓝本创建不规则窗口
- Duilib 创建不规则窗口(转载)
- wince下创建不规则窗口
- 【原】创建 WPF 不规则窗口
- 用Win32 API创建不规则窗口
- 如何创建不规则窗口
- 创建WPF不规则窗口
- 如何创建一个不规则形状的窗口收藏
- 使用Qt创建透明盒不规则窗口的方法