CRgn类成员函数
CreateRectRgn |
创建一个矩形区域来初始化对象 |
CreateRectRgnIndirect |
创建一个矩形区域来初始化对象参数是一个RECT结构地址 |
CreateEllipticRgn |
创建一个椭圆区域来初始化对象 |
CreateEllipticRgnIndirect |
创建一个椭圆区域来初始化对象参数是一个RECT结构地址 |
CreatePolygonRgn |
创建一个多边形区域来初始化对象 |
CreatePolyPolygonRgn |
创建一个多个多边形区域来初始化对象 |
CreateRoundRectRgn |
创建一个圆角矩形区域来初始化对象 |
CombineRgn |
合并2个区域 |
CDC::FillRgn
BOOL FillRgn( CRgn* pRgn, CBrush* pBrush ); //用指定画刷填充一个区域
CRgn rgn1,rgn2,rgn3;
rgn1.CreateEllipticRgnIndirect(CRect(0,0,100,150));
rgn2.CreateRectRgnIndirect(CRect(0,22,150,222));
rgn3.CreateEllipticRgnIndirect(CRect(120,199,220,150));
rgn1.CombineRgn(&rgn2,&rgn3,RGN_OR ); //rgn1=rgn2+rgn3;
//为了便于观察
CDC *pdc=GetDC();
CBrush bsh(0xff);
pdc->FillRgn(&rgn1,&bsh);
rgn1.DeleteObject();
rgn2.DeleteObject();
rgn3.DeleteObject();
ReleaseDC(pdc);
CRgn::CreatePolygonRgn
BOOL CreatePolygonRgn( LPPOINT lpPoints, int nCount, int nMode );
lpPoints //坐标点数组地址
nCount //多边形顶点数量,或者理解为坐标数组大小。
nMode //模式ALTERNATE 或者WINDING 之一
CRgn rgn;
POINT p[22]={140,49,262,14,346,92,287,186,153,165/* */,129,140,76,39,153,47,63,45,20};
rgn.CreatePolygonRgn(p,5,1 );//WINDING
CDC *pdc=GetDC();
CBrush bsh(0xFF000);
pdc->FillRgn(&rgn,&bsh);;
rgn.DeleteObject();
ReleaseDC(pdc);
CRgn::CreatePolyPolygonRgn
BOOL CreatePolyPolygonRgn(LPPOINTlpPoints, LPINTlpPolyCounts, int nCount, int nPolyFillMode);
lpPoints //坐标数组
lpPolyCounts //多边形数组
nCount //多边形数量
nPolyFillMode //绘制模式 ,相交点是否包含 WINDING包含相交区域ALTERNATE不包含
CRgn rgn;
POINT p[22]={154,43,90,91,143,143,233,142,214,39,166,95,170,172,243,195,303,153,279,103};//顶点坐标
INT pn[2]={5,5};//几边形,几个
rgn.CreatePolyPolygonRgn(p,pn,2,WINDING );//WINDING
CDC *pdc=GetDC();
CBrush bsh(0xFF000);
pdc->FillRgn(&rgn,&bsh);;
rgn.DeleteObject();
ReleaseDC(pdc);
CRgn::CombineRgn
int CombineRgn( CRgn* pRgn1, CRgn* pRgn2, intnCombineMode );
作用合并pRgn1和pRgn2至 CRgn对象;
//合并风格
nCombineMode
RGN_AND
RGN_COPY
RGN_DIFF
RGN_OR
RGN_XOR
CRgn r1,r2,r3,rgn;
r1.CreateRectRgn(43,30,100,54);
r2.CreateRoundRectRgn(103,34,145,89,22,11);
r3.CreateEllipticRgn(115,79,195,112);
rgn.CreateRectRgn(43,30,100,54);
//合并
rgn.CombineRgn(&r1,&r2,RGN_OR);//rgn=r1+r2
rgn.CombineRgn(&rgn,&r3,RGN_OR);//rgn=rgn+r3=r1+r2+r3
CDC *pdc=GetDC();
CBrush bsh(0xFF00);
pdc->FillRgn(&rgn,&bsh);
rgn.DeleteObject();
r1.DeleteObject();
r2.DeleteObject();
r3.DeleteObject();
ReleaseDC(pdc);
API版:
int SetWindowRgn(
HWND hWnd, //窗口句柄
HRGN hRgn, //区域句柄
BOOL bRedraw // 重绘否
);
CWnd版:
CWnd::SetWindowRgn
int SetWindowRgn( HRGN hRgn, BOOL bRedraw );
r1.CreateEllipticRgn(0,0,100,54);
SetWindowRgn(r1,true);//CWnd版
::SetWindowRgn(m_hWnd,r1,true);//API版
总的过程:加载图片,创建区域,对图片进行处理(去白色、区域合并等),异形窗口
注意窗口和图片的大小。
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP5);
CDC bmpdc;
CDC *pdc=GetWindowDC();//包含了标题栏
bmpdc.CreateCompatibleDC(pdc);
bmpdc.SelectObject(&bmp);
CRgn r1,r2;
CRect rw;
GetClientRect(&rw);//当前窗口大小
r1.CreateRectRgnIndirect(&rw);
//bmpdc.GetPixel()
for (int x=0;xFillRgn(&r1,&bsh);
SetWindowRgn(r1,1);
每次窗口异形都会较慢,此时可在初次使用时就把相关的数据保存下来,下次运行时,可直接调用,加快程序运行速度。
保存:
LPRGNDATA pRgnData=NULL;
int rSize=0;
{
..........
//保存区域信息
//获取区域数据的大小
rSize=r1.GetRegionData(NULL,0);
//
char* rdata=new char[rSize];
pRgnData=(LPRGNDATA) rdata;
//取得区域数据存放到pRgnData处
r1.GetRegionData(pRgnData,rSize);
delete[] rdata;
}
读取:
CRgn r;
r.CreateFromData(NULL,rSize,pRgnData);
SetWindowRgn(r,true);