CLKing31----------------------------Introduction to CRect class usage

Introduction to CRect class usage

Xin Chen  2013-10-08 21:15:37   13216   Collection 14

Classification column:  Control  MFC  C++

MFC

[Turn] The CRect class is a common class of MFC, very basic, so it is sorted out for reference by novices.

  I used Jinshan Express to check the word "rect". It is probably the abbreviation of rectangle, which means rectangle or rectangle.
  Default coordinate system: the origin is the upper left corner, the right is the positive direction of the x-axis, and the bottom is the positive direction of the y-axis.
  The constructor (CRect) has five prototypes, the second prototype and the third prototype are copy constructors. Now I use the other three constructors to construct a
  rectangle with the upper left corner coordinates (10,20), the x-direction side length is 100, and the y-direction side   is 200 long.
  Prototype 1:
  CRect r1(10,20,110,220);
  Prototype 4:
  POINT pt = {10,20};
  SIZE size = {100,200};
  CRect r2(pt,size);
  Prototype 5:
  POINT topLeft = {10,20};
  POINT bottomRight = {110,220};
  CRect r3(topLeft,bottomRight);
  The following code can check the size of a rectangle, create a single document project Sdi, modify the OnDraw function:
  void CSdiView::OnDraw(CDC* pDC)
  {   CSdiDoc* pDoc = GetDocument();   ASSERT_VALID(pDoc);   CRect r1(10,20,110,220);   pDC->Rectangle(&r1);




  }
  int Width() const;
  Get the width, int iWidth = r1.Width(); iWidth will be 100 at the meeting.
  int Height() const;
  Get the height, int iHeight = r1.Height(); the result of iHeight is 200.
  CSize Size() const;
  Get width and height, CSize size = r1.Size(); size.cx is 100, size.cy is 200.
  CPoint& TopLeft( );
  Get the coordinates of the upper left corner. Since the return value is a reference, you can modify the upper left corner through the return value of this function.
  CRect r1(10,20,110,220);
  r1.TopLeft().x = 0;
  r1.TopLeft().y = 0;
  The coordinates of the upper left corner of r1 become (0,0), and the upper left corner can also be obtained through this function coordinate.
  CRect r1(10,20,110,220);
  CPoint TopLeft = r1.TopLeft();
  The value of TopLeft.x is 10, and the value of TopLeft.y is 20.
  BottomRight obtains the coordinates of the lower right corner
  CPoint CenterPoint() const; obtains the coordinates of the center, CPoint pt = r1.CenterPoint(); pt is (60,120).
  BOOL IsRectEmpty() const; If the long seat or width is 0 or illegal, return true; otherwise, return false.
  CRect r1(210,20,110,220);
  bool bEmpty = r1.IsRectEmpty(); The
  result is true because the left side is larger than the right side.
  CRect::IsRectNull, the coordinates of the four sides are all 0, the result is true, otherwise it is false.
  BOOL PtInRect( POINT point) const; Check whether a point is inside the rectangle.
  CRect r1(10,20,110,220);
  POINT pt1={10,10};
  POINT pt2={10,30};
  bool bIn1 = r1.PtInRect(pt1);
  bool bIn2 = r1.PtInRect(pt2);
  bIn1 is false, bIn2 is true.
  CRect::SetRect, set the values ​​of the four sides, the usage is similar to the prototype one of the constructor.
  CRect::SetRectEmpty, set the coordinates of the four sides to 0.
  void CopyRect( LPCRECT lpSrcRect ); Copy.
  CRect r2;
  r2.CopyRect(&r1);
  The value of r2 is the same as the value of r1.
  CRect::EqualRect, whether the two rectangles are the same, the coordinates of the four sides must be the same.
  CRect r1(10,20,110,220);
  CRect r2(110,220,10,20);
  bool bEqual = r1.EqualRect(r2);
  The value of bEqual is false, because their top, bottom, left, and right sides are different, which is the other way around.
  CRect r1(110,220,10,20);
  CRect r2(110,220,10,20);
  bool bEqual = r1.EqualRect(r2);
  bEqual is true because the four sides are the same.
  CRect::InflateRect, increase the width and height.
  CRect r(0,0,0,0);
  r.InflateRect(2,3);//The result is (-2,-3,2,3);
  SIZE size = {3,2};
  r.InflateRect( size);//The result is (-5,-5,5,5);
  CRect r1(-5,-5,-5,-5);
  r.InflateRect(&r1);//The result is (0,0 ,0,0);
  //Move left by -1 to the left (Move right by 1), move up by -1 on the upper side, move on the right by 2, and move down on the lower side by 2
  r.InflateRect(-1,-1,2,2);/ /The result is (1,1,2,2);
  CRect::DeflateRect, reduce the width and height, the method is similar to InflateRect.
  CRect::NormalizeRect, standardize, adjust the top and bottom and left and right sides to make it in line with human logic.
  CRect r(10,10,0,0);
  r.NormalizeRect(); The
  result is (0,0,10,10)
  CRect::OffsetRect, move the entire rectangle.
  CRect r(0,0,10,10);
  r.OffsetRect(1,1);//Move right by 1, move down by 1
  POINT point = {1,1};
  r.OffsetRect(point);//Right again Move 1, then move down 1
  SIZE size = {-2,-2};
  r.OffsetRect(size);//Move right -2, move down -2
  CRect::SubtractRect, lpRectSrc1 minus lpRectSrc2, note that it is not a minus sign . The process is not easy to express, you can see the effect by running the following code.
  void CSdiView::OnDraw(CDC* pDC)
  {   pDC->SelectStockObject(NULL_BRUSH);   CRect r1(10,10, 100,100);   CRect r2(50,10, 150,150); //and CRect r2(50,50, 150,150 ); The results are different   pDC->Rectangle(r1);   pDC->Rectangle(r2);   {//The red area is the result of SubtractRect   CRect r;







  r.SubtractRect(r1,r2);
  CBrush brush(RGB(255,0,0));
  pDC->SelectObject(&brush);
  pDC->Rectangle(&r);
  }
  }
  CRect::IntersectRect, find the intersection
  void CSdiView: :OnDraw(CDC* pDC)
  {   pDC->SelectStockObject(NULL_BRUSH);   CRect r1(10,10, 100,100);   CRect r2(50,10, 150,150); //and CRect r2(50,50, 150,150); result Not the same   pDC->Rectangle(r1);   pDC->Rectangle(r2);   {//The green area is the result of IntersectRect   CRect r;   r.IntersectRect(r1,r2);   CBrush brush(RGB(0,255,0));   pDC->SelectObject(&brush);   pDC->Rectangle(&r);   }   }   CRect::UnionRect, find the union














  void CSdiView::OnDraw(CDC* pDC)
  {   pDC->SelectStockObject(NULL_BRUSH);   CRect r1(10,10, 100,100);   CRect r2(50,50, 150,150); //and CRect r2(50,50, 150,150 ); The results are different   pDC->Rectangle(r1);   pDC->Rectangle(r2);   {//The blue border is the result of UnionRect   CRect r;   r.UnionRect(r1,r2);   CPen pen(PS_DOT,1, RGB(0,0,255));   pDC->SelectObject(&pen);   pDC->Rectangle(&r);   }   }   CRect::operator LPCRECT, converted to LPCRECT type.   CRect::operator LPRECT is transformed into LPRECT   CRect r(0,0,100,100);   LPCRECT pCRect = r;   LPRECT pRect = r; The   second line will call the LPCRECT operator, and the third line will call the LPRECT operator.



















  typedef const RECT* LPCRECT; // pointer to read/only RECT
  typedef struct tagRECT
  {   LONG left;   LONG top;   LONG right;   LONG bottom;   } RECT, *PRECT, NEAR *NPRECT, FAR *LPRECT;   CRect::operator =, Overload the "=" operator to actually call CopyRect.   CRect::operator ==, overload the "==" operator, and actually call EqualRect.   operator !=, overloads the "!=" operator, and actually calls EqualRect.   CRect::operator +=, overloading the "+=" operator, the first prototype and the second prototype call OffsetRect, and the third prototype calls InflateRect.   CRect::operator -=, overload the "-=" operator, the first prototype and the second prototype call OffsetRect, and the third prototype calls InflateRect.   CRect::operator &=, overload "&=" operator, actually call IntersectRect.   CRect::operator |= Overloads the "|=" operator, and actually calls UnionRect.   +, -, &, | are similar to the above, so I won’t explain them in detail.












 

Guess you like

Origin blog.csdn.net/qq_43662480/article/details/115019763