下面有三个绘图函数需要一个指向矩形结构的指针:
FillRect(hdc,&rect,hbursh);//填充
FrameRect(hdc,&rect,hbrush);//掏空
InvertRect(hdc,&rect);//内部取反
case WM_PAINT: hdc=BeginPaint(hwnd,&ps); //GetClientRect(hwnd,&rect); //hrgn=CreateRectRgn(100,100,300,300); //hbrush=CreateSolidBrush(RGB(255,0,0)); //FillRgn(hdc,hrgn,hbrush); //InvertRgn(hdc,hrgn);//取反 //FrameRgn(hdc,hrgn,hbrush,0,0); r1.left=100; r1.top=100; r1.right=200; r1.bottom=200; hbrush=CreateSolidBrush(RGB(255,0,0));//创建画刷 FillRect(hdc,&r1,hbrush); r2.left=200; r2.top=100; r2.right=300; r2.bottom=200; FrameRect(hdc,&r2,hbrush); r3.left=300; r3.top=100; r3.right=400; r3.bottom=200; InvertRect(hdc,&r3); EndPaint(hwnd,&ps); return 0;
下面还有八个关于矩形的操作:
OffsetRect(&rect,x,y) | 将矩形移动 |
InflateRect(&rect,x,y) | 增大缩小矩形的尺寸 |
SetRectEmpty(&rect) | 把矩形结构各字段设置为0 |
CopyRect(&DestRect,&ScrRect) | 将一个矩形结构复制到另一个矩形结构 |
IntersectRect(&destRect,&SrcRect1,&SrcRect2) | 获取两个矩形的交集 |
UnionRect(&DestRect,&SrcRect1,&SrcRect2) | 获取两个矩形的并集 |
bEmpty=IsRectEmpty(&Rect) | 判断矩形是否为空 |
bInRect=PtInRect(&rect,point) | 判断点是否在矩形内部 |
区域跟矩形一样,有以下四个绘图函数
FillRgn(hdc,hrgn,hbrush); FrameRgn(hdc,hbrush,xFrame,yFrame); InvertRgn(hdc,hrgn); PaintRgn(hdc,hrgn);
DeleteObject(hrgn);
SelectObject(hdc,hrgn)=SelectClipRgn(hdc,hrgn);
使得矩形,区域有效和无效的函数
InvalidateRect ValidateRect InvalidateRgn ValidateRgn
下面是简单的例子:
case WM_PAINT: hdc=BeginPaint(hwnd,&ps); //GetClientRect(hwnd,&rect); //hrgn=CreateRectRgn(100,100,300,300); //hbrush=CreateSolidBrush(RGB(255,0,0)); //FillRgn(hdc,hrgn,hbrush); //InvertRgn(hdc,hrgn);//取反 //FrameRgn(hdc,hrgn,hbrush,0,0); r1.left=100; r1.top=100; r1.right=200; r1.bottom=200; hbrush=CreateSolidBrush(RGB(255,0,0));//创建画刷 //FillRect(hdc,&r1,hbrush); r2.left=150; r2.top=100; r2.right=250; r2.bottom=200; //FrameRect(hdc,&r2,hbrush); IntersectRect(&r3,&r1,&r2);//r1,r2矩形取交集,存放在r3 FillRect(hdc,&r3,hbrush); flag=(int)IsRectEmpty(&r3);//判断矩形是否为空 TextOut(hdc,300,300,szBuffer,wsprintf(szBuffer,TEXT("%d"),flag)); EndPaint(hwnd,&ps); return 0;
hrgn=CreateRectRgn(xleft,ytop,xright,ybottom); hrgn=CreateRectRgnIndirect(&rect); hrgn=CreateEllipticRgn(xleft,ytop,xright,ybottom); hrgn=CreatePolygonRgn(&point,icount,ipolyFillMode);