创建一不规则窗口

原方法:

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(00, 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);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值