欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

SIZE,POINT,CPoint, CRect,CSize浅谈

发布时间:2023/12/29 编程问答 45 豆豆
生活随笔 收集整理的这篇文章主要介绍了 SIZE,POINT,CPoint, CRect,CSize浅谈 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

这几个结构容易混淆:

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( );

写个例子?随便新建一个基于对话框的MFC,添加如下代码:

CClientDC dc(this); // 取得设备描述表RECT rt;GetClientRect(&rt);CPoint p1(rt.right / 4, rt.bottom / 4);CPoint p2(p1);p2.Offset(rt.right / 2, rt.bottom / 2);//偏移值dc.MoveTo(p1);dc.LineTo(p2);if (p1==p2){DrawText(dc.m_hDC, _T("这两个点是相同的!"), -1, &rt, 0);}else{DrawText(dc.m_hDC, _T("\n这两个点不相同!"), -1, &rt, 0);}

运行结果:

(2)+,-,+=,-+
+:

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

两个点相加是一个点,一个点和一个长度相加是一个点,一个lprect加一个点是一个rect.
还是来举个栗子吧,如下:

CClientDC dc(this); // 取得设备描述表RECT rt;GetClientRect(&rt);CPoint p1(rt.right / 4, rt.bottom / 4);CPoint p2(p1);CSize s1(rt.right / 4, rt.bottom / 4);CPoint p3 = p1 + s1;CString str = _T("我在这里!");CString strP3(_T("p3: ") + str);TextOut(dc.m_hDC, p3.x, p3.y, strP3, strP3.GetLength());CRect rect(0, 0, rt.right / 4, rt.bottom / 4);rect = p1 + rect; //CRect operator+(_In_ POINT point) const throw();还是CRect哦。FillRect(dc.m_hDC, rect, (HBRUSH)GetStockObject(DKGRAY_BRUSH));

运行结果:

-:

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( );

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

CClientDC dc(this); // 取得设备描述表RECT rt;GetClientRect(&rt);CPoint p1(rt.right / 4, rt.bottom / 4);CPoint p2(p1);CPoint p3 = p1 + p2;CString str = _T("我在这里!");CString strP3(_T("p3: ") + str);TextOut(dc.m_hDC, p3.x, p3.y, strP3, strP3.GetLength());p1 += p3;CString strP1(_T("p1: ") + str);TextOut(dc.m_hDC, p1.x, p1.y, strP1, strP1.GetLength());p1 -= p3;strP1 += _T(", 这是我-=后的新位置");::TextOut(dc.m_hDC, p1.x, p1.y, strP1, strP1.GetLength());

运行结果:

CPoint就到此吧。

CRect类 的介绍
类CRect是对Windows结构RECT的封装,凡是能用RECT结构的地方都可以用CRect代替。
结构RECT表示一个矩形的位置和尺寸,其定义为:

typedef struct tagRECT{LONG left;LONG top;LONG right;LONG bottom;} RECT;

其中 left、top分别表示矩形左上角顶点的横坐标和纵坐标,right、bottom分别表示矩形右下角顶点的横坐标和纵坐标。由于CRect提供了一些成员函数和重载运算符,使得CRect的操作更加方便。 1.CRect的构造函数 CRect有如下6个构造函数:

  • CRect( );

  • CRect( int l, int t, int r, int b );

  • CRect( const RECT& srcRect );

  • CRect( LPCRECT lpSrcRect );

  • CRect( POINT point, SIZE size );

  • CRect( POINT topLeft, POINT bottomRight );

说明:分别以不同的方式构造CRect对象,参数l,t,r,b分别指定矩形的左边、上边、右边和底边。SrcRect是一个RECT结构的引用。LpSrcRect是一个指向RECT结构的指针。Point指定矩形的左上角顶点的坐标,size指定矩形的长度和宽度。topLeft指定矩形的左上角顶点的坐标,bottomRight指定矩形的右下角顶点的坐标。
CSize?还是看MSDN吧,写不了这么多,呼呼。

以上为转载加上一点修改,错误地方欢迎指正。

总结

以上是生活随笔为你收集整理的SIZE,POINT,CPoint, CRect,CSize浅谈的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。