说到不规则界面,其实就是使用SetWindowRgn来实现,而重点就是如何形成那个hrgn,这里与大家分享两个自己用的方法,方法也是借鉴网上。
HRGN BitmapToRgn(HBITMAP hBitmap,COLORREF col)
{
HDC hMemDc;
HBITMAP hOld;
COLORREF CPixel;
HRGN rTemp,hGoal;
BITMAP Bmp;
GetObject(hBitmap,sizeof(BITMAP),&Bmp);
hMemDc=CreateCompatibleDC(NULL);
hOld=(HBITMAP)SelectObject(hMemDc,hBitmap);
hGoal=CreateRectRgn(0,0,Bmp.bmWidth,Bmp.bmHeight);
for(int x=0;x<=Bmp.bmWidth;x++)
{
for(int y=0;y<=Bmp.bmHeight;y++)
{
CPixel=GetPixel(hMemDc,x,y);
if(CPixel==col)
{
rTemp=CreateRectRgn(x,y,x+1,y+1);
CombineRgn(hGoal,hGoal,rTemp,RGN_XOR);
DeleteObject(rTemp);
}
}
}
SelectObject(hMemDc,hOld);
DeleteDC(hMemDc);
return hGoal;
}
这种方法可以更灵活,你可以绘制任意形状,只要先把背景填充为透明色col
HRGN dcToRgn(HDC hDc,COLORREF col)
{
HDC hMemDc=hDc;
COLORREF CPixel;
HRGN rTemp,hGoal;
BITMAP Bmp;
Bmp.bmWidth = WIDTH;
Bmp.bmHeight = HEIGHT;
hGoal=CreateRectRgn(0,0,Bmp.bmWidth,Bmp.bmHeight);
for(int x=0;x<=Bmp.bmWidth;x++)
{
for(int y=0;y<=Bmp.bmHeight;y++)
{
CPixel=GetPixel(hMemDc,x,y);
if(CPixel==col)
{
rTemp=CreateRectRgn(x,y,x+1,y+1);
CombineRgn(hGoal,hGoal,rTemp,RGN_XOR);
DeleteObject(rTemp);
}
}
}
return hGoal;
}