矩形和区域

下面有三个绘图函数需要一个指向矩形结构的指针:

FillRect(hdc,&rect,hbursh);//填充

FrameRect(hdc,&rect,hbrush);//掏空

InvertRect(hdc,&rect);//内部取反

  1. case WM_PAINT:  
  2.             hdc=BeginPaint(hwnd,&ps);  
  3.             //GetClientRect(hwnd,&rect);  
  4.               
  5.             //hrgn=CreateRectRgn(100,100,300,300);  
  6.             //hbrush=CreateSolidBrush(RGB(255,0,0));  
  7.             //FillRgn(hdc,hrgn,hbrush);  
  8.             //InvertRgn(hdc,hrgn);//取反  
  9.             //FrameRgn(hdc,hrgn,hbrush,0,0);  
  10.   
  11.               
  12.             r1.left=100;  
  13.             r1.top=100;  
  14.             r1.right=200;  
  15.             r1.bottom=200;  
  16.             hbrush=CreateSolidBrush(RGB(255,0,0));//创建画刷  
  17.             FillRect(hdc,&r1,hbrush);  
  18.   
  19.             r2.left=200;  
  20.             r2.top=100;  
  21.             r2.right=300;  
  22.             r2.bottom=200;  
  23.             FrameRect(hdc,&r2,hbrush);  
  24.   
  25.             r3.left=300;  
  26.             r3.top=100;  
  27.             r3.right=400;  
  28.             r3.bottom=200;  
  29.             InvertRect(hdc,&r3);  
  30.   
  31.             EndPaint(hwnd,&ps);  
  32.             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) 判断点是否在矩形内部

 

区域跟矩形一样,有以下四个绘图函数

  1. FillRgn(hdc,hrgn,hbrush);  
  2. FrameRgn(hdc,hbrush,xFrame,yFrame);  
  3. InvertRgn(hdc,hrgn);  
  4. PaintRgn(hdc,hrgn);  
  1. DeleteObject(hrgn);  
  1. SelectObject(hdc,hrgn)=SelectClipRgn(hdc,hrgn);  

使得矩形,区域有效和无效的函数

  1. InvalidateRect       ValidateRect  
  2. InvalidateRgn        ValidateRgn  

下面是简单的例子:

  1. case WM_PAINT:  
  2.             hdc=BeginPaint(hwnd,&ps);  
  3.             //GetClientRect(hwnd,&rect);  
  4.               
  5.             //hrgn=CreateRectRgn(100,100,300,300);  
  6.             //hbrush=CreateSolidBrush(RGB(255,0,0));  
  7.             //FillRgn(hdc,hrgn,hbrush);  
  8.             //InvertRgn(hdc,hrgn);//取反  
  9.             //FrameRgn(hdc,hrgn,hbrush,0,0);  
  10.   
  11.               
  12.             r1.left=100;  
  13.             r1.top=100;  
  14.             r1.right=200;  
  15.             r1.bottom=200;  
  16.             hbrush=CreateSolidBrush(RGB(255,0,0));//创建画刷  
  17.             //FillRect(hdc,&r1,hbrush);  
  18.   
  19.             r2.left=150;  
  20.             r2.top=100;  
  21.             r2.right=250;  
  22.             r2.bottom=200;  
  23.             //FrameRect(hdc,&r2,hbrush);  
  24.   
  25.             IntersectRect(&r3,&r1,&r2);//r1,r2矩形取交集,存放在r3  
  26.             FillRect(hdc,&r3,hbrush);  
  27.             flag=(int)IsRectEmpty(&r3);//判断矩形是否为空  
  28.             TextOut(hdc,300,300,szBuffer,wsprintf(szBuffer,TEXT("%d"),flag));  
  29.   
  30.             EndPaint(hwnd,&ps);  
  31.             return 0;  


  1. hrgn=CreateRectRgn(xleft,ytop,xright,ybottom);  
  2. hrgn=CreateRectRgnIndirect(&rect);  
  3. hrgn=CreateEllipticRgn(xleft,ytop,xright,ybottom);  
  4. hrgn=CreatePolygonRgn(&point,icount,ipolyFillMode);  
原文地址:https://www.cnblogs.com/lidabo/p/3439153.html