Muokkaa

Jaa


CPoint Class

Similar to the Windows POINT structure.

Syntax

class CPoint : public tagPOINT

Members

Public Constructors

Name Description
CPoint::CPoint Constructs a CPoint.

Public Methods

Name Description
CPoint::Offset Adds values to the x and y members of the CPoint.

Public Operators

Name Description
CPoint::operator - Returns the difference of a CPoint and a SIZE, or the negation of a POINT, or the CSize difference between two POINTs, or the offset by a negative SIZE.
CPoint::operator != Checks for inequality between two POINTs.
CPoint::operator + Returns the sum of a CPoint and a SIZE or POINT, or a CRect offset by a SIZE.
CPoint::operator += Offsets CPoint by adding a SIZE or POINT.
CPoint::operator -= Offsets CPoint by subtracting a SIZE or POINT.
CPoint::operator == Checks for equality between two POINTs.

Remarks

It also includes member functions to manipulate CPoint and POINT structures.

A CPoint object can be used wherever a POINT structure is used. The operators of this class that interact with a SIZE accept either CSize objects or SIZE structures, since the two are interchangeable.

Note

This class is derived from the tagPOINT structure. (The name tagPOINT is a less commonly used name for the POINT structure.) This means that the data members of the POINT structure, x and y, are accessible data members of CPoint.

Note

For more information on shared utility classes (like CPoint), see Shared Classes.

Inheritance Hierarchy

tagPOINT

CPoint

Requirements

Header: atltypes.h

CPoint::CPoint

Constructs a CPoint object.

CPoint() throw();
CPoint(int initX, int initY) throw();
CPoint(POINT initPt) throw();
CPoint(SIZE initSize) throw();
CPoint(LPARAM dwPoint) throw();

Parameters

initX
Specifies the value of the x member of CPoint.

initY
Specifies the value of the y member of CPoint.

initPt
POINT structure or CPoint that specifies the values used to initialize CPoint.

initSize
SIZE structure or CSize that specifies the values used to initialize CPoint.

dwPoint
Sets the x member to the low-order word of dwPoint and the y member to the high-order word of dwPoint.

Remarks

If no arguments are given, x and y members are set to 0.

Example

CPoint   ptTopLeft(0, 0);
// works from a POINT, too

POINT   ptHere;
ptHere.x = 35;
ptHere.y = 95;

CPoint   ptMFCHere(ptHere);

// works from a SIZE
SIZE   sHowBig;
sHowBig.cx = 300;
sHowBig.cy = 10;

CPoint ptMFCBig(sHowBig);
// or from a DWORD

DWORD   dwSize;
dwSize = MAKELONG(35, 95);

CPoint ptFromDouble(dwSize);
ASSERT(ptFromDouble == ptMFCHere);

CPoint::Offset

Adds values to the x and y members of the CPoint.

void Offset(int xOffset, int yOffset) throw();
void Offset(POINT point) throw();
void Offset(SIZE size) throw();

Parameters

xOffset
Specifies the amount to offset the x member of the CPoint.

yOffset
Specifies the amount to offset the y member of the CPoint.

point
Specifies the amount (POINT or CPoint) to offset the CPoint.

size
Specifies the amount (SIZE or CSize) to offset the CPoint.

Example

CPoint   ptStart(100, 100);
ptStart.Offset(35, 35);

CPoint   ptResult(135, 135);
ASSERT(ptStart == ptResult);

// works with POINT, too

ptStart = CPoint(100, 100);
POINT pt;

pt.x = 35;
pt.y = 35;

ptStart.Offset(pt);

ASSERT(ptStart == ptResult);

// works with SIZE, too

ptStart = CPoint(100, 100);
SIZE size;

size.cx = 35;
size.cy = 35;

ptStart.Offset(size);

ASSERT(ptStart == ptResult);   

CPoint::operator ==

Checks for equality between two POINTs.

BOOL operator==(POINT point) const throw();

Parameters

point
Contains a POINT structure or CPoint object.

Return Value

Nonzero if the POINTs are equal; otherwise 0.

Example

CPoint ptFirst(256, 128);
CPoint ptTest(256, 128);

ASSERT(ptFirst == ptTest);

// works with POINTs, too

POINT pt;
pt.x = 256;
pt.y = 128;

ASSERT(ptTest == pt);

// note that pt == ptTest isn't correct!   

CPoint::operator !=

Checks for inequality between two POINTs.

BOOL operator!=(POINT point) const throw();

Parameters

point
Contains a POINT structure or CPoint object.

Return Value

Nonzero if the POINTs aren't equal; otherwise 0.

Example

CPoint ptFirst(256, 128);
CPoint ptTest(111, 333);

ASSERT(ptFirst != ptTest);

// works with POINTs, too

POINT pt;
pt.x = 333;
pt.y = 111;

ASSERT(ptTest != pt);

// note that pt != ptTest isn't correct!   

CPoint::operator +=

The first overload adds a SIZE to the CPoint.

void operator+=(SIZE size) throw();
void operator+=(POINT point) throw();

Parameters

size
Contains a SIZE structure or CSize object.

point
Contains a POINT structure or CPoint object.

Remarks

The second overload adds a POINT to the CPoint.

In both cases, addition is done by adding the x (or cx) member of the right-hand operand to the x member of the CPoint and adding the y (or cy) member of the right-hand operand to the y member of the CPoint.

For example, adding CPoint(5, -7) to a variable that contains CPoint(30, 40) changes the variable to CPoint(35, 33).

Example

CPoint   ptStart(100, 100);
CSize   szOffset(35, 35);

ptStart += szOffset;

CPoint   ptResult(135, 135);

ASSERT(ptResult == ptStart);

// also works on SIZE

ptStart = CPoint(100, 100);

SIZE   sz;
sz.cx = 35;
sz.cy = 35;

ptStart += sz;

ASSERT(ptResult == ptStart);   

CPoint::operator -=

The first overload subtracts a SIZE from the CPoint.

void operator-=(SIZE size) throw();
void operator-=(POINT point) throw();

Parameters

size
Contains a SIZE structure or CSize object.

point
Contains a POINT structure or CPoint object.

Remarks

The second overload subtracts a POINT from the CPoint.

In both cases, subtraction is done by subtracting the x (or cx) member of the right-hand operand from the x member of the CPoint and subtracting the y (or cy) member of the right-hand operand from the y member of the CPoint.

For example, subtracting CPoint(5, -7) from a variable that contains CPoint(30, 40) changes the variable to CPoint(25, 47).

Example

CPoint   ptStart(100, 100);
CSize   szOffset(35, 35);

ptStart -= szOffset;

CPoint   ptResult(65, 65);

ASSERT(ptResult == ptStart);

// also works on SIZE

ptStart = CPoint(100, 100);

SIZE   sz;
sz.cx = 35;
sz.cy = 35;

ptStart -= sz;

ASSERT(ptResult == ptStart);   

CPoint::operator +

Use this operator to offset CPoint by a CPoint or CSize object, or to offset a CRect by a CPoint.

CPoint operator+(SIZE size) const throw();
CPoint operator+(POINT point) const throw();
CRect operator+(const RECT* lpRect) const throw();

Parameters

size
Contains a SIZE structure or CSize object.

point
Contains a POINT structure or CPoint object.

lpRect
Contains a pointer to a RECT structure or CRect object.

Return Value

A CPoint that is offset by a SIZE, a CPoint that is offset by a POINT, or a CRect offset by a POINT.

Remarks

For example, using one of the first two overloads to offset the point CPoint(25, -19) by a point CPoint(15, 5) or size CSize(15, 5) returns the value CPoint(40, -14).

Adding a CRect to a POINT returns the CRect after being offset by the x and y values specified in the POINT. For example, using the last overload to offset a rectangle CRect(125, 219, 325, 419) by a point CPoint(25, -19) returns CRect(150, 200, 350, 400).

Example

CPoint   ptStart(100, 100);
CSize   szOffset(35, 35);
CPoint   ptEnd;

ptEnd = ptStart + szOffset;

CPoint   ptResult(135, 135);

ASSERT(ptResult == ptEnd);

// also works on SIZE

ptStart = CPoint(100, 100);

SIZE   sz;
sz.cx = 35;
sz.cy = 35;

ptEnd = ptStart + sz;

ASSERT(ptResult == ptEnd);   

CPoint::operator -

Use one of the first two overloads to subtract a CPoint or CSize object from CPoint.

CSize operator-(POINT point) const throw();
CPoint operator-(SIZE size) const throw();
CRect operator-(const RECT* lpRect) const throw();
CPoint operator-() const throw();

Parameters

point
A POINT structure or CPoint object.

size
A SIZE structure or CSize object.

lpRect
A pointer to a RECT structure or a CRect object.

Return Value

A CSize that is the difference between two POINTs, a CPoint that is offset by the negation of a SIZE, a CRect that is offset by the negation of a POINT, or a CPoint that is the negation of a POINT.

Remarks

The third overload offsets a CRect by the negation of CPoint. Finally, use the unary operator to negate CPoint.

For example, using the first overload to find the difference between two points CPoint(25, -19) and CPoint(15, 5) returns CSize(10, -24).

Subtracting a CSize from CPoint does the same calculation as above but returns a CPoint object, not a CSize object. For example, using the second overload to find the difference between the point CPoint(25, -19) and the size CSize(15, 5) returns CPoint(10, -24).

Subtracting a rectangle from a POINT returns the rectangle offset by the negatives of the x and y values specified in the POINT. For example, using the last overload to offset the rectangle CRect(125, 200, 325, 400) by the point CPoint(25, -19) returns CRect(100, 219, 300, 419).

Use the unary operator to negate a POINT. For example, using the unary operator with the point CPoint(25, -19) returns CPoint(-25, 19).

Example

// example for CPoint subtraction
CPoint   ptStart(100, 100);
CSize   szOffset(35, 35);
CPoint   ptEnd;

ptEnd = ptStart - szOffset;

CPoint   ptResult(65, 65);

ASSERT(ptResult == ptEnd);

// also works on SIZE

ptStart = CPoint(100, 100);

SIZE   sz;
sz.cx = 35;
sz.cy = 35;

ptEnd = ptStart - sz;

ASSERT(ptResult == ptEnd);

// example for CPoint unary operator
CPoint   pt(35, 35);
pt = -pt;

CPoint ptNeg(-35, -35);
ASSERT(pt == ptNeg);   

See also

MFC Sample MDI
Hierarchy Chart
POINT Structure
CRect Class
CSize Class