Table of contents
1.
Introduction
2.
Syntax
3.
Public Constructors
4.
Public Methods
5.
Public Operators
6.
Requirements
7.
CRect::BottomRight
8.
CRect::CenterPoint
9.
CRect::CopyRect
10.
CRect::Height
11.
CRect::NormalizeRect
12.
CRect::IsRectEmpty
13.
CRect::operator =
14.
CRect::operator ==
15.
Frequently Asked Questions
15.1.
What is a utility class?
15.2.
What is ATL?
15.3.
What is MFC?
15.4.
What are the classes shared by MFC and ATL?
16.
Conclusion
Last Updated: Mar 27, 2024
Easy

UtilityClass-CRect

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

intro image

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.


Code

CRect rectangle1(25, 31, 11, 10);
CRect rectangle2(11, 10, 25, 31);

rectangle1.NormalizeRect();
rectangle2.NormalizeRect();
ASSERT(rectangle1 == rectangle2);


Note: This method does not return anything; it changes and normalizes the CRect.

CRect::IsRectEmpty

This method checks whether the CRect is empty or not.

Code

CRect rectNone(10, 10, 10, 10);
CRect rectSome(7, 10, 27, 30);

ASSERT(rectNone.IsRectEmpty());
ASSERT(!rectSome.IsRectEmpty());

CRect rectEmpty(7, 7, 7, 7);
ASSERT(rectEmpty.IsRectEmpty());


Note: The IsRectEmpty method returns a nonzero value if CRect is empty. It returns 0 if CRect is empty.

CRect::operator =

We use this operator to assign a source rectangle(srcRect) to CRect.

srcRECT can be a RECT or CRect.


Code

CRect rect(35, 35, 35, 35);
CRect rect2;

 
rect2 = rect;
ASSERT(rect2 == CRect(35, 35, 35, 35));

CRect::operator ==

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.

You may refer to our Guided Path on Code Studios for enhancing your skill set on DSACompetitive ProgrammingSystem Design, etc. Check out essential interview questions, practice our available mock tests, look at the interview bundle for interview preparations, and so much more!

Happy Learning Ninja!

Live masterclass