Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
CPoint is a class which is present in the ATL(Active Template Library). The ATL(Active Template Library) is a set of template-based C++ classes. In this article we will discuss Syntax, Constructors, Methods and Operators related to CPoint.
Are you ready? Let us learn about UtilityClass-CPoint.
Syntax
CPoint is a class which is present in the Active Template Library. It is very similar to the Windows POINT structure.
The syntax for CPoint Class is as follows:
class CPoint : public tagPOINT
You can also try this code with Online C++ Compiler
CPoint::Offset : It adds value to the x and y members of the CPoint.
Public Operators
Name
Description
CPoint::operator -
It returns the difference of a SIZE and of a CPoint, or the CSize difference between two POINTs, or the negation of a POINT, or the offset by a negative SIZE.
CPoint::operator !=
It checks for the inequality between two POINTs.
CPoint::operator +
It returns the sum of CPoint and POINT or a SIZE, or a CRect offset by a SIZE.
CPoint::operator +=
It offsets CPoint by adding a POINT or SIZE.
CPoint::operator -=
It offsets CPoint by subtracting a POINT or SIZE.
CPoint::operator ==
It checks equality between two POINTs.
Requirements
We need a header named atltypes.h to perform these operations.
initX: It specifies the value of the x member of CPoint.
initY: It specifies the value of the y member of CPoint.
initPt: It is a POINT structure or CPoint which specifies the values used to initialize the CPoint.
initSize: It is the SIZE structure or CSize that specifies the values to be used to initialize CPoint.
dwPoint: It sets the x member and the y member to the low-order word of dwPoint and to the high-order word of dwPoint respectively.
Note: x and y members are set to 0, by default.
Below is an illustration:
CPoint ptTopLeft(0, 0);
/*works from a POINT, too*/
POINT ptHere;
ptHere.x = 48;
ptHere.y = 59;
CPoint ptMFCHere(ptHere);
/* works from a SIZE*/
SIZE sHowBig;
sHowBig.cx = 200;
sHowBig.cy = 20;
CPoint ptMFCBig(sHowBig);
/* or from a DWORD*/
DWORD dwSize;
dwSize = MAKELONG(48, 59);
CPoint ptFromDouble(dwSize);
ASSERT(ptFromDouble == ptMFCHere);
You can also try this code with Online C++ Compiler
size: It contains a SIZE structure or CSize object.
point: It contains a POINT structure or CPoint object.
Note: In case of a second overload, it adds a POINT to the CPoint. For example, adding CPoint(3, -3) to a variable that contains CPoint(28, 30) changes the variable to CPoint(31, 27).
Below is an illustration:
CPoint ptStart(200, 200);
CSize szOffset(70, 70);
ptStart += szOffset;
CPoint ptResult(270, 270);
ASSERT(ptResult == ptStart);
/* It can work on SIZE*/
ptStart = CPoint(200, 200);
SIZE s;
s.cx = 70;
s.cy = 70;
ptStart += z;
ASSERT(ptResult == ptStart);
You can also try this code with Online C++ Compiler
size: It contains a SIZE structure or CSize object.
point: It contains a POINT structure or CPoint object.
Note: The second overload subtracts a POINT from the CPoint. For example, subtracting CPoint(7, -3) from a variable that contains CPoint(32, 73) changes the variable to CPoint(25, 76).
size: It contains a SIZE structure or CSize object.
point: It contains a POINT structure or CPoint object.
lpRect: It contains a pointer to a RECT structure or CRect object.
Its return value is a CPoint that is offset by a POINT, a CPoint that is offset by a SIZE, or a CRect offset by a POINT.
Note:
Using one of the first two overloads to offset the point CPoint(17, -39) by a point CPoint(8, 2) or size CSize(8, 2) returns the value CPoint(25, -37).
Adding a CRect to a POINT will return the CRect after being offset by the x and y value as specified in the POINT. We use the last overload to offset a rectangle CRect(41, 74, 13, 94) by a point CPoint(12, -9) returns CRect(53, 65, 13, 94).
lpRect: It is a pointer to a RECT structure or a CRect object.
Its return value is CSize which is the difference between two POINTs, a CPoint which is the negation of a POINT, a CPoint which is the offset by the negation of a SIZE, or a CRect which is the offset by the negation of a POINT.
Note:
The third will overload offset CRect by the negation of the CPoint. Finally, we can use the unary operator to negate CPoint. By using the second overload we can find the difference between the point CPoint(31, -21) and the size CSize(21, 7) returns CPoint(10, -28).
When we try subtracting a CSize from CPoint, it calculates as above but returns a CPoint object, not a CSize object. By using the second overload we can find the difference between the point CPoint(41, –23) and the size CSize(32, 18) returns CPoint(9, -41).
When we try subtracting a rectangle from a POINT, it will return the rectangle offset by the non-positives of the x and y values specified in given POINT. We can use the last overload to offset the rectangle CRect(31, 32, 41, 53) by the point CPoint(14, -21) returns CRect(17, 53, 41, 53).
We can use the unary operator in order to negate any POINT. For example, using the unary operator with the point CPoint(25, -19) will return the CPoint(-25, 19).
Alright! Now we hope you understand the UtilityClass-CPoint.
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 had the intention 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 that are shared by MFC and ATL are CFileTime, CFileTimeSpan, CFixedStringT, CImage, COleDateTime, COleDateTimeSpan, CPoint, CRect, CSimpleStringT, CSize, CStrBufT, CStrBufT, CStringT, CTime, CTimeSpan, and IAtlStringMgr.
Conclusion
In this article, we discussed the UtilityClass-CPoint of ATL(Active Template Library). We hope this blog on UtilityClass-CPoint was helpful. You can also refer to other similar articles.