Chinaunix首页 | 论坛 | 博客
  • 博客访问: 510120
  • 博文数量: 158
  • 博客积分: 4015
  • 博客等级: 上校
  • 技术积分: 1711
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-27 14:00
文章分类

全部博文(158)

文章存档

2010年(71)

2009年(87)

我的朋友

分类: C/C++

2010-02-06 13:42:59

这几个结构容易混淆:

SIZE:The SIZE structure specifies the width and height of a rectangle.

typedef struct tagSIZE {
  LONG cx;
  LONG cy;
}SIZE, *PSIZE;

再看看POINT结构:
Represents an ordered pair of integer x- and y-coordinates that defines a point in a two-dimensional plane.它主要有两个成员了。

typedef struct tagPOINT {
   LONG x;
   LONG y;
} POINT;

由此我们再看MFC中的CPoint:

class CPoint : public tagPOINT

它直接从结构当中继承而来,x, y成员都是public属性。
当然CPoint是相当强大的,我们从MSDN可以看出一二:
1.结构函数:

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

所有的构造函数都要求不会抛出任何异常,我们可以用x,y来初始化一个点,从另一个CPoint类,甚至从SIZE结构来初始化,不可谓不强大。

2.运算符重载
(1) ==, !=

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

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

写个例子?
pointDemo.h

class CMyApp : public CWinApp {
    virtual BOOL InitInstance();
};
class CMainWindow : public CFrameWnd {
public:
    CMainWindow();
protected:
    afx_msg void OnPaint();
    DECLARE_MESSAGE_MAP()
};

pointDemo.cpp

#include <afxwin.h>
#include "pointDemo.h"

CMyApp myApp;

BOOL CMyApp::InitInstance() {
    m_pMainWnd = new CMainWindow;
    m_pMainWnd->ShowWindow(m_nCmdShow);
    m_pMainWnd->UpdateWindow();
    return TRUE;
}

BEGIN_MESSAGE_MAP(CMainWindow, CFrameWnd)
ON_WM_PAINT()
END_MESSAGE_MAP()

CMainWindow::CMainWindow() {
    Create(NULL, _T("看看点的属性"));
}
void CMainWindow::OnPaint() {
    CPaintDC dc(this);
    RECT rect;
    GetClientRect(&rect);
    CPoint p1(rect.right / 4, rect.bottom / 4);
    CPoint p2(p1);
    p2.Offset(rect.right / 2, rect.bottom / 2);
    dc.MoveTo(p1);
    dc.LineTo(p2);
    if(p1 == p2) {
        DrawText(dc, "这两个点是相同的!", -1, &rect, 0);
    } else {
        DrawText(dc, "\n\n这两个点不相同!", -1, &rect, 0);
    }
}

运行结果:
(2)+,-,+=,-+
+:

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

两个点相加是一个点,一个点和一个长度相加是一个点,一个lprect加一个点是一个rect.
-:

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

两个点相减是一个长度,一个点减一个长度是一个点,一个点减一个lprect是一个rect(关于这点,请看msdn: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).)
+=:
The first overload adds a size to the CPoint.

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

看清楚了返回的是一个void
-=:

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

我靠,得写一个例子了:我们以上面为例:
头文件:

class CMyApp : public CWinApp {
    virtual BOOL InitInstance();
};
class CMainWindow : public CFrameWnd {
public:
    CMainWindow();
protected:
    afx_msg void OnPaint();
    DECLARE_MESSAGE_MAP()
};

cpp:

#include <afxwin.h>
#include <string>
#include "pointDemo.h"
using namespace std;

CMyApp myApp;

BOOL CMyApp::InitInstance() {
    m_pMainWnd = new CMainWindow;
    m_pMainWnd->ShowWindow(m_nCmdShow);
    m_pMainWnd->UpdateWindow();
    return TRUE;
}

BEGIN_MESSAGE_MAP(CMainWindow, CFrameWnd)
ON_WM_PAINT()
END_MESSAGE_MAP()

CMainWindow::CMainWindow() {
    Create(NULL, _T("看看点的属性"));
}
void CMainWindow::OnPaint() {
    CPaintDC dc(this);
    RECT rect;
    GetClientRect(&rect);
    CPoint p1(rect.right / 4, rect.bottom / 4);
    CPoint p2(p1);
    CPoint p3 = p1 + p2;
    string str = "我在这里!";
    string strP3("p3: " + str);
    TextOut(dc, p3.x, p3.y, strP3.c_str(), strP3.size());
    p1 += p3;
    string strP1("p1: " + str);
    dc.TextOut(p1.x, p1.y, strP1.c_str(), strP3.size());
    p1 -= p3;
    strP1.append(", 这是我-=后的新位置");
    dc.TextOut(p1.x, p1.y, strP1.c_str(), strP1.size());

}

运行结果:

CPoint就到此吧。
CSize?还是看MSDN吧,写不了这么多,呼呼

阅读(5660) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~