日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

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

编程问答

SIZE,POINT,CPoint, CRect,CSize浅谈

發布時間:2023/12/29 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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浅谈的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。