我写了一个类CPoint1 它基于tagPOINT1 结构。
代码:
typedef struct tagPOINT1
{
long double x;
long double y;
} POINT1;
class CPoint1 : public tagPOINT1
{
public:
// Constructors
// create an uninitialized point
CPoint1();
// create from two integers
CPoint1(double initX, double initY);
// create from another point
CPoint1(POINT1 initPt);
// create from a size
CPoint1(SIZE initSize);
// create from a dword: x = LOWORD(dw) y = HIWORD(dw)
CPoint1(DWORD dwPoint);
// Operations
// translate the point
void Offset(double xOffset, double yOffset);
void Offset(POINT1 point);
void Offset(SIZE size);
BOOL operator==(POINT1 point) const;
BOOL operator!=(POINT1 point) const;
void operator+=(SIZE size);
void operator-=(SIZE size);
void operator+=(POINT1 point);
void operator-=(POINT1 point);
// Operators returning CPoint values
CPoint1 operator+(SIZE size) const;
CPoint1 operator-(SIZE size) const;
CPoint1 operator-() const;
CPoint1 operator+(POINT1 point) const;
};
CPoint1::CPoint1()
{ /* random filled */ }
CPoint1::CPoint1(double initX, double initY)
{ x = initX; y = initY; }
CPoint1::CPoint1(POINT1 initPt)
{ *(POINT1*)this = initPt; }
CPoint1::CPoint1(SIZE initSize)
{ *(SIZE*)this = initSize; }
CPoint1::CPoint1(DWORD dwPoint)
{
x = (short)LOWORD(dwPoint);
y = (short)HIWORD(dwPoint);
}
void CPoint1::Offset(double xOffset, double yOffset)
{ x += xOffset; y += yOffset; }
void CPoint1::Offset(POINT1 point)
{ x += point.x; y += point.y; }
void CPoint1::Offset(SIZE size)
{ x += size.cx; y += size.cy; }
BOOL CPoint1::operator==(POINT1 point) const
{ return (x == point.x && y == point.y); }
BOOL CPoint1::operator!=(POINT1 point) const
{ return (x != point.x || y != point.y); }
void CPoint1::operator+=(SIZE size)
{ x += size.cx; y += size.cy; }
void CPoint1::operator-=(SIZE size)
{ x -= size.cx; y -= size.cy; }
void CPoint1::operator+=(POINT1 point)
{ x += point.x; y += point.y; }
void CPoint1::operator-=(POINT1 point)
{ x -= point.x; y -= point.y; }
CPoint1 CPoint1::operator+(SIZE size) const
{ return CPoint1(x + size.cx, y + size.cy); }
CPoint1 CPoint1::operator-(SIZE size) const
{ return CPoint1(x - size.cx, y - size.cy); }
CPoint1 CPoint1::operator-() const
{ return CPoint1(-x, -y); }
CPoint1 CPoint1::operator+(POINT1 point) const
{ return CPoint1(x + point.x, y + point.y); }
然后我在鼠标按下时调用。
void CDemo2View::OnLButtonDown(UINT nFlags, CPoint1 point) //已改为我的类
{
// TODO: Add your message handler code here and/or call default
CDC * pDC=GetDC();
double x=point.x+100;
double y=point.y+100;
pDC->MoveTo(50,50);
pDC->LineTo(x,y);
ReleaseDC(pDC);
}
结果取出的xy没有值,请问这是为什么?
谢谢
因为MFC在发送一个鼠标被按下的消息时,第二个参数是它们自己定义的CPoint,所以如果你要改第二个参数为你自己定义的类,那么有两种办法:
1)、你自己定义的类的内存布局与他们的一模一样;
2)、你的类是从他们的类继承下来的,但这种情况下参数应该为指针才行;
我看你这里的CPoint1和CPoint类完全相同,只不过tagPOINT1和tagPOINT不同,这可能是造成他们内存布局不一样的缘故!