Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
CRect is a class in the ATL(Active Template Library), a set of template-based C++ classes. We use the CRect class to define a rectangle's bottom-right and top-left points.
This article will discuss UtilityClass-CRect of ATL(Active Template Library), so let’s get started!
Syntax
The syntax of CRect is as follows,
class CRect : public tagRECT
Public Constructors
CRect::CRect : This is the constructor to construct a CRect object.
Public Methods
Name of the method
Description
CRect::BottomRight
This method gives the bottom-right point of CRect.
CRect::CenterPoint
This method gives the center point of CRect.
CRect::CopyRect
This method is used to copy the dimensions of a source rectangle to CRect.
CRect::DeflateRect
To decrease the width and height of CRect.
CRect::EqualRect
For a given rectangle, this method compares and tells whether CRect is equal to it.
CRect::Height
It gives the height of CRect.
CRect::InflateRect
To increase the width and height of CRect.
CRect::IntersectRect
This method makes a CRect equal to the intersection of two existing rectangles.
CRect::IsRectEmpty
This method tells whether CRect is empty.
CRect::IsRectNull
Determines whether CRect is null.
CRect::MoveToX
It moves the CRect to the specified x-coordinate.
CRect::MoveToXY
It moves the CRect to the specified x- and y-coordinates.
CRect::MoveToY
It moves the CRect to the specified y-coordinate.
CRect::NormalizeRect
This method normalizes CRect to make the height and width positive.
CRect::OffsetRect
It moves CRect by the specified offsets.
CRect::PtInRect
This method tells whether the specified point lies within CRect.
CRect::SetRect
This method sets the dimensions of CRect.
CRect::SetRectEmpty
To set CRect to an empty rectangle (all coordinates equal to 0).
CRect::Size
It calculates the size of CRect.
CRect::SubtractRect
To subtract one rectangle from another.
CRect::TopLeft
This method gives the top-left point of CRect.
CRect::UnionRect
To set CRect equal to the union of two rectangles.
CRect::Width
This method calculates the width of CRect.
Public Operators
Name of the operator
Description
CRect::operator -
This subtracts the given offsets from CRect and returns the resulting CRect.
CRect::operator LPCRECT
This operator is used to convert a CRect to an LPCRECT.
CRect::operator LPRECT
This operator is used to convert a CRect to an LPRECT.
CRect::operator !=
This operator checks whether CRect is equal to a rectangle.
CRect::operator &
It makes the intersection of a rectangle with CRect and returns the resulting CRect.
CRect::operator &=
This operator sets CRect equal to the intersection of the rectangle and CRect.
CRect::operator |
This makes the union of CRect and a rectangle and returns the resulting CRect.
CRect::operator |=
This operator sets CRect equal to the union of CRect and a rectangle.
CRect::operator +
It adds the given offsets to CRect and returns the resulting CRect.
CRect::operator +=
This adds the specified offsets to CRect.
CRect::operator =
It copies the dimensions of a rectangle to CRect.
CRect::operator -=
It subtracts the specified offsets from CRect.
CRect::operator ==
This operator checks whether CRect is equal to a rectangle.
Requirements
The required header is atltypes.h.
CRect::BottomRight
It gives the coordinates of the bottom-right corner of the rectangle.
Note: You can set the corner by using this function with the help of the assignment operator.
Code
CRect rect(21, 15, 35, 90);
CPoint point1;
point1 = rect.BottomRight();
/* point1 is now set to (35, 90)*/
ASSERT(point1 == CPoint(35, 90));
/* or, use BottomRight() to set the bottom */
/* right POINT*/
CRect rect2(1, 1, 35, 35);
CPoint point2(18, 18);
CRect rect2(1, 1, 35, 35);
CPoint point2(18, 18);
rect2.BottomRight() = point2;
/* rect2 is now (1, 1, 18, 18) */
ASSERT(rect2 == CRect(1, 1, 18, 18));
Note: This public method(CRect::BottomRight) returns the coordinates(point) of the bottom-right corner of the rectangle.
CRect::CenterPoint
We use this method to get the center point. We can get the center point by
Adding left and right values, dividing by two.
Adding top and bottom values, dividing by two.
Note: This method returns the center point as a CPoint object.
CRect::CopyRect
This method copies the RECT structure or CRect object(lpSrcRect) rectangle into CRect.
Code
CRect rectSource(7, 2, 25, 2);
CRect rectDestination;
rectDestination.CopyRect(&rectSource);
/* Now, rectDestination is set to (7, 2, 25, 2)*/
RECT rectSource1;
rectSource1.left = 0;
rectSource1.top = 0;
rectSource1.bottom = 48;
rectSource1.right = 64;
rectDestination.CopyRect(&rectSource1);
/* this works against RECT structures, too! */
/* rectDestination is now set to (0, 0, 64, 48). */
Note: It is a void type. It copies the lpSrcRect and returns nothing.
CRect::Height
We use this method to get the height of the CRect. We subtract the top value from the bottom to get the height.
Code
CRect rect(20, 30, 80, 70);
int nHt = rect.Height();
/* nHt is now 4 */
ASSERT(nHt == 4);
Note: It returns the height of CRect. The resulting value can be negative. If the rectangle is not normalized, then the function may fail. To normalize the rectangle, use the NormalizeRect method described below.
CRect::NormalizeRect
To normalize CRect, we use this method. This method makes both the height and width positive.
It compares the top and bottom values; if the top is greater than the bottom, it swaps them.
It compares the left and right values; if the left is greater than the right, it swaps them.
We use this operator to check if a source rectangle or RECT equals CRect.
This operator compares the coordinates of their lower-right and upper-left corners to give the result.
Code
CRect rect1(7, 30, 2, 5);
CRect rect2(7, 30, 2, 5);
CRect rect3(98, 999, 6, 3);
ASSERT(rect1 == rect2);
/* works just fine against RECTs, as well*/
RECT test;
test.left = 7;
test.top = 30;
test.right = 2;
test.bottom = 5;
ASSERT(rect1 == test);
Note: This operator gives a nonzero value if the RECT is equal to the CRect; if not, it provides 0.
Alright! We hope you understand the UtilityClass-CRect.
Frequently Asked Questions
What is a utility class?
A utility class is a class that has only static methods that perform certain operations on the objects passed as parameters. Utility classes generally have no state.
What is ATL?
ATL stands for the Active Template Library. ATL is a set of template-based C++ classes developed by Microsoft and intended to simplify the programming of Component Object Model objects.
What is MFC?
MFC stands for Microsoft Foundation Class Library. MFC is a C++ object-oriented library for developing desktop applications for Windows. MFC was introduced by Microsoft in 1992 and quickly gained widespread use.
What are the classes shared by MFC and ATL?
The classes shared by MFC and ATL are CFileTime, CFileTimeSpan, CFixedStringT, CImage, COleDateTime, COleDateTimeSpan, CPoint, CRect, CSimpleStringT, CSize, CStrBufT, CStrBufT, CStringT, CTime, CTimeSpan, and IAtlStringMgr.
Conclusion
This article discussed the UtilityClass-CRect of ATL(Active Template Library). We hope this blog on UtilityClass-CRect was helpful. You can also refer to other similar articles.