共用方式為


CRect 類別

類似于 Windows RECT 結構。

語法

class CRect : public tagRECT

成員

公用建構函式

名稱 描述
CRect::CRect 建構 CRect 物件。

公用方法

名稱 描述
CRect::BottomRight 傳回 的右下角。 CRect
CRect::CenterPoint 傳回 的中心 CRect 點。
CRect::CopyRect 將來源矩形的維度複製到 CRect
CRect::DeflateRect 減少 的 CRect 寬度和高度。
CRect::EqualRect 判斷 是否 CRect 等於指定的矩形。
CRect::Height 計算 的高度 CRect
CRect::InflateRect 增加 的 CRect 寬度和高度。
CRect::IntersectRect 設定 CRect 等於兩個矩形的交集。
CRect::IsRectEmpty 判斷 是否 CRect 為空白。 CRect 如果寬度和/或高度為 0,則為空白。
CRect::IsRectNull 判斷 、 bottomleftright 成員變數是否 top 全部等於 0。
CRect::MoveToX 移至 CRect 指定的 x 座標。
CRect::MoveToXY 移至 CRect 指定的 x 和 y 座標。
CRect::MoveToY 移至 CRect 指定的 Y 座標。
CRect::NormalizeRect 標準化 的高度和寬度 CRect
CRect::OffsetRect 依指定的位移移動 CRect
CRect::PtInRect 判斷指定的點是否位於 內 CRect
CRect::SetRect 設定 的 CRect 維度。
CRect::SetRectEmpty 設定 CRect 為空白矩形(所有座標等於 0)。
CRect::Size 計算 的大小 CRect
CRect::SubtractRect 從另一個矩形減去一個矩形。
CRect::TopLeft 傳回 的 CRect 左上方點。
CRect::UnionRect 設定 CRect 等於兩個矩形的聯集。
CRect::Width 計算 的 CRect 寬度。

公用運算子

名稱 描述
CRect::operator - 從 或 減去指定的位移 CRectCRect ,並傳回產生的 CRect
CRect::operator LPCRECT CRect 轉換成 LPCRECT
CRect::operator LPRECT CRect 轉換成 LPRECT
CRect::operator != 判斷 是否 CRect 不等於矩形。
CRect::operator & 建立 和 矩形的 CRect 交集,並傳回產生的 CRect
CRect::operator &= 設定 CRect 等於 和 矩形的 CRect 交集。
CRect::operator | 建立 和 矩形的 CRect 聯集,並傳回產生的 CRect
CRect::operator |= 設定 CRect 等於 和 矩形的 CRect 聯集。
CRect::operator + 將指定的位移加入 CRect 或擴充 CRect ,並傳回產生的 CRect
CRect::operator += 將指定的位移加入 CRect 或擴充 CRect
CRect::operator = 將矩形的維度複製到 CRect
CRect::operator -= 將指定的位移從 CRect 減去,或減去 。 CRect
CRect::operator == 判斷 是否 CRect 等於矩形。

備註

CRect 也包含操作 CRect 物件和 Windows RECT 結構的成員函式。

CRect物件可以傳遞為函式參數,無論 RECT 結構、 LPCRECTLPRECT 傳遞的位置。

注意

這個類別衍生自 tagRECT 結構。 (此名稱 tagRECT 是結構較不常使用的名稱 RECT 。這表示 結構的資料成員 ( lefttopright 、 和 bottomRECT 是 的可存取資料成員 CRect

CRect包含成員變數,定義矩形的左上方和右下角點。

指定 CRect 時,您必須小心建構它,使其正規化,換句話說,讓左座標的值小於右邊,而頂端小於底部。 例如,左上方的 (10,10) 和右下角 (20,20) 定義正規化矩形,但左上方的 (20,20) 和右下角 (10,10) 定義非正規化矩形。 如果矩形未正規化,許多 CRect 成員函式可能會傳回不正確的結果。 (如需這些函式的清單,請參閱 CRect::NormalizeRect 。呼叫需要正規化矩形的函式之前,您可以藉由呼叫 NormalizeRect 函式來正規化非正規化矩形。

使用 和 CDC::LPtoDP 成員函式操作 CRectCDC::DPtoLP 時請小心。 如果顯示內容的對應模式讓 y 範圍是負數,如同 中的 MM_LOENGLISH ,則會 CDC::DPtoLP 轉換 CRect ,使其頂端大於底部。 和 之類的 HeightSize 函式接著會傳回已轉換 CRect 高度的負值,而且矩形將會非正規化。

使用多載 CRect 運算子時,第一個 CRect 運算元必須是 ;第二個 RECT 運算元可以是 結構或 CRect 物件。

繼承階層架構

tagRECT

CRect

需求

標頭:atltypes.h

CRect::BottomRight

座標會以 包含在 中的 CRect 物件參考 CPoint 傳回。

CPoint& BottomRight() throw();
const CPoint& BottomRight() const throw();

傳回值

矩形右下角的座標。

備註

您可以使用此函式來取得或設定矩形的右下角。 使用指派運算子左邊的這個函式來設定角落。

範例

// use BottomRight() to retrieve the bottom
// right POINT
CRect rect(210, 150, 350, 900);
CPoint ptDown;

ptDown = rect.BottomRight();

// ptDown is now set to (350, 900)
ASSERT(ptDown == CPoint(350, 900));

// or, use BottomRight() to set the bottom
// right POINT
CRect rect2(10, 10, 350, 350);
CPoint ptLow(180, 180);

CRect rect2(10, 10, 350, 350);
CPoint ptLow(180, 180);
rect2.BottomRight() = ptLow;

// rect2 is now (10, 10, 180, 180)
ASSERT(rect2 == CRect(10, 10, 180, 180));

CRect::CenterPoint

藉由新增左右值並除以兩個,以及新增頂端和底部值,並除以兩,計算 的中心點 CRect

CPoint CenterPoint() const throw();

傳回值

CPoint物件,為 的中心 CRect 點。

範例

// Code from this OnPaint() implementation can be pasted into your own application
// to draw lines that would look like a letter "Y" within your dialog.
void CMyDlg::OnPaint()
{
    CPaintDC dc(this);

    // device context for painting

    // get the size and position of the client area of
    // your window

    CRect rect;
    GetClientRect(&rect);

    // Move the current pen to the top left of the window. We call the
    // TopLeft() member of CRect here and it returns a CPoint object we
    // pass to the override of CDC::MoveTo() that accepts a CPoint.

    dc.MoveTo(rect.TopLeft());

    // Draw a line from the top left to the center of the window.
    // CenterPoint() gives us the middle point of the window as a
    // CPoint, and since CDC::LineTo() has an override that accepts a
    // CPoint, we can just pass it along.

    dc.LineTo(rect.CenterPoint());

    // Now, draw a line to the top right of the window. There's no
    // CRect member which returns a CPoint for the top right of the
    // window, so we'll reference the CPoint members directly and call
    // the CDC::LineTo() override which takes two integers.

    dc.LineTo(rect.right, rect.top);

    // The top part of the "Y" is drawn. Now, we'll draw the stem. We
    // start from the center point.

    dc.MoveTo(rect.CenterPoint());

    // and then draw to the middle of the bottom edge of the window.
    // We'll get the x-coordinate from the x member of the CPOINT
    // returned by CenterPoint(), and the y value comes directly from
    // the rect.

    dc.LineTo(rect.CenterPoint().x, rect.bottom);
}

CRect::CopyRect

lpSrcRect 矩形複製到 CRect

void CopyRect(LPCRECT lpSrcRect) throw();

參數

lpSrcRect
指向 RECT 要複製的結構或 CRect 物件。

範例

CRect rectSource(35, 10, 125, 10);
CRect rectDest;

rectDest.CopyRect(&rectSource);

// rectDest is now set to (35, 10, 125, 10)

RECT rectSource2;
rectSource2.left = 0;
rectSource2.top = 0;
rectSource2.bottom = 480;
rectSource2.right = 640;

rectDest.CopyRect(&rectSource2);

// works against RECT structures, too!
// rectDest is now set to (0, 0, 640, 480)

CRect::CRect

建構 CRect 物件。

CRect() throw();
CRect(int l, int t, int r, int b) throw();
CRect(const RECT& srcRect) throw();
CRect(LPCRECT lpSrcRect) throw();
CRect(POINT point, SIZE size) throw();
CRect(POINT topLeft, POINT bottomRight) throw();

參數

l
指定 的 CRect 左位置。

t
指定 的 CRect 頂端。

r
指定 的正確位置 CRect

b
指定 的 CRect 底部。

srcRect
RECT參考 具有 座標的結構 CRect

lpSrcRect
RECT指向 具有 座標的結構 CRect

point
指定要建構之矩形的原點。 對應至左上角。

size
指定要建構矩形的左上角到右下角的位移。

topLeft
指定 的左上方位置 CRect

bottomRight
指定 的 CRect 右下角位置。

備註

如果未指定任何引數, left 則 、 toprightbottom 成員會設定為 0。

const RECT& ) 和 CRectLPCRECT ) 建 CRect 構函式會執行 CopyRect 。 其他建構函式會直接初始化 物件的成員變數。

範例

// default constructor is equivalent to CRect(0, 0, 0, 0)
CRect emptyRect;

// four-integers are left, top, right, and bottom
CRect rect(0, 0, 100, 50);
ASSERT(rect.Width() == 100);
ASSERT(rect.Height() == 50);

// Initialize from RECT structure
RECT sdkRect;
sdkRect.left = 0;
sdkRect.top = 0;
sdkRect.right = 100;
sdkRect.bottom = 50;

CRect rect2(sdkRect);
// by reference
CRect rect3(&sdkRect);

// by address
ASSERT(rect2 == rect);
ASSERT(rect3 == rect);

// from a point and a size
CPoint pt(0, 0);
CSize sz(100, 50);
CRect rect4(pt, sz);
ASSERT(rect4 == rect2);

// from two points
CPoint ptBottomRight(100, 50);
CRect rect5(pt, ptBottomRight);
ASSERT(rect5 == rect4);

CRect::DeflateRect

DeflateRect 將兩側向中心移動來消解 CRect

void DeflateRect(int x, int y) throw();
void DeflateRect(SIZE size) throw();
void DeflateRect(LPCRECT lpRect) throw();
void DeflateRect(int l, int t, int r, int b) throw();

參數

x
指定要解除的左右兩側 CRect 的單位數目。

y
指定要解除頂端和底部的 CRect 單位數目。

size
SIZECSize ,指定要化解 CRect 的單位數目。 值 cx 會指定要解除左右兩側的單位數目,而 cy 值會指定要解除頂端和底部的單位數目。

lpRect
指向 RECT 結構,或 CRect 指定要解除每個側邊的單位數目。

l
指定要解除的左側 CRect 的單位數目。

t
指定要解除頂端的 CRect 單位數目。

r
指定要解除其右側 CRect 的單位數目。

b
指定要解除底端的 CRect 單位數目。

備註

若要這樣做,請將 DeflateRect 單位新增至左側和頂端,並從右下減去單位。 DeflateRect的參數是帶正負號的值;正值會解除, CRect 負值會擴大值。

前兩個多載將兩對對反面 CRect 化解,使其總寬度減少兩次(或 cx ),而其總高度則減少兩次 xy (或 cy )。 另外兩個多載會排解彼此各自獨立的兩端 CRect

範例

CRect rect(10, 10, 50, 50);
rect.DeflateRect(1, 2);
ASSERT(rect.left == 11 && rect.right == 49);
ASSERT(rect.top == 12 && rect.bottom == 48);

CRect rect2(10, 10, 50, 50);
CRect rectDeflate(1, 2, 3, 4);
rect2.DeflateRect(&rectDeflate);
ASSERT(rect2.left == 11 && rect2.right == 47);
ASSERT(rect2.top == 12 && rect2.bottom == 46);

CRect::EqualRect

判斷 是否 CRect 等於指定的矩形。

BOOL EqualRect(LPCRECT lpRect) const throw();

參數

lpRect
指向 RECT 包含矩形左上角和右下角座標的結構或 CRect 物件。

傳回值

如果兩個矩形具有相同的上、左、下和右值,則為非零;否則為 0。

注意

這兩個矩形都必須正規化,否則此函式可能會失敗。 您可以呼叫 NormalizeRect 來將矩形正規化,再呼叫此函式。

範例

CRect rect1(35, 150, 10, 25);
CRect rect2(35, 150, 10, 25);
CRect rect3(98, 999, 6, 3);
ASSERT(rect1.EqualRect(rect2));
ASSERT(!rect1.EqualRect(rect3));
// works just fine against RECTs, as well

RECT test;
test.left = 35;
test.top = 150;
test.right = 10;
test.bottom = 25;

ASSERT(rect1.EqualRect(&test));

CRect::Height

藉由從底部值減去頂端值來計算 的高度 CRect

int Height() const throw();

傳回值

的高度 CRect

備註

產生的值可以是負值。

注意

矩形必須正規化,否則此函式可能會失敗。 您可以呼叫 NormalizeRect 將矩形正規化,再呼叫此函式。

範例

CRect rect(20, 30, 80, 70);
int nHt = rect.Height();

// nHt is now 40
ASSERT(nHt == 40);

CRect::InflateRect

InflateRect 將雙方從中心移開來膨脹 CRect

void InflateRect(int x, int y) throw();
void InflateRect(SIZE size) throw();
void InflateRect(LPCRECT lpRect) throw();
void InflateRect(int l, int t, int r,  int b) throw();

參數

x
指定要擴充 的左右兩側 CRect 的單位數目。

y
指定要擴大頂端和底部的 CRect 單位數目。

size
SIZECSize ,指定要擴充 CRect 的單位數目。 值 cx 會指定要擴充左右兩側的單位數目,而 cy 值會指定要擴充上層和底部的單位數目。

lpRect
指向 RECT 結構,或 CRect 指定要擴充每一側的單位數目。

l
指定要擴充左側的 CRect 單位數目。

t
指定要擴充頂端的 CRect 單位數目。

r
指定要擴充右側的 CRect 單位數目。

b
指定要擴充底部的 CRect 單位數目。

備註

若要這樣做, InflateRect 請從左和上減單位,並將單位新增至右和下。 InflateRect的參數是帶正負號的值;正值膨脹 CRect 和負值會將其解除化。

前兩個多載會擴大兩對對的兩 CRect 側,使其總寬度增加兩倍(或 cx ),而其總高度則增加兩倍 xy (或 cy )。 另外兩個多載會獨立地擴充每一端 CRect

範例

CRect rect(0, 0, 300, 300);
rect.InflateRect(50, 200);

// rect is now (-50, -200, 350, 500)
ASSERT(rect == CRect(-50, -200, 350, 500));

CRect::IntersectRect

使 等於 CRect 兩個現有矩形的交集。

BOOL IntersectRect(LPCRECT lpRect1, LPCRECT lpRect2) throw();

參數

lpRect1
指向 RECT 包含來源矩形的結構或 CRect 物件。

lpRect2
指向 RECT 包含來源矩形的結構或 CRect 物件。

傳回值

如果交集不是空的,則為非零;如果交集是空的,則為 0。

備註

交集是兩個現有矩形中包含的最大矩形。

注意

這兩個矩形都必須正規化,否則此函式可能會失敗。 您可以呼叫 NormalizeRect 來將矩形正規化,再呼叫此函式。

範例

CRect rectOne(125,  0, 150, 200);
CRect rectTwo(0, 75, 350, 95);
CRect rectInter;

rectInter.IntersectRect(rectOne, rectTwo);
ASSERT(rectInter == CRect(125, 75, 150, 95));
// operator &= can do the same task:

CRect rectInter2 = rectOne;
rectInter2 &= rectTwo;
ASSERT(rectInter2 == CRect(125, 75, 150, 95));

CRect::IsRectEmpty

判斷 是否 CRect 為空白。

BOOL IsRectEmpty() const throw();

傳回值

如果 CRect 為空白,則為非零;如果 CRect 不是空白,則為 0。

備註

如果寬度和/或高度為 0 或負數,則矩形是空的。 與 IsRectNull 不同,決定矩形的所有座標是否為零。

注意

矩形必須正規化,否則此函式可能會失敗。 您可以呼叫 NormalizeRect 將矩形正規化,再呼叫此函式。

範例

CRect rectNone(0, 0, 0, 0);
CRect rectSome(35, 50, 135, 150);
ASSERT(rectNone.IsRectEmpty());
ASSERT(!rectSome.IsRectEmpty());
CRect rectEmpty(35, 35, 35, 35);
ASSERT(rectEmpty.IsRectEmpty());

CRect::IsRectNull

判斷 的頂端、左、下和右值 CRect 是否全部等於 0。

BOOL IsRectNull() const throw();

傳回值

如果 CRect 的頂端、左、下和右值都等於 0,則為非零,否則為 0。

備註

IsRectEmpty 不同,它會判斷矩形是否為空白。

範例

CRect rectNone(0, 0, 0, 0);
CRect rectSome(35, 50, 135, 150);
ASSERT(rectNone.IsRectNull());
ASSERT(!rectSome.IsRectNull());
// note that null means _all_ zeros

CRect rectNotNull(0, 0, 35, 50);
ASSERT(!rectNotNull.IsRectNull());

CRect::MoveToX

呼叫此函式,將矩形移至 所 x 指定的絕對 x 座標。

void MoveToX(int x) throw();

參數

x
矩形左上角的絕對 x 座標。

範例

CRect rect(0, 0, 100, 100);
rect.MoveToX(10);

// rect is now (10, 0, 110, 100);
ASSERT(rect == CRect(10, 0, 110, 100));

CRect::MoveToXY

呼叫此函式,將矩形移至指定的絕對 x 和 y 座標。

void MoveToXY(int x, int y) throw();
void MoveToXY(POINT point) throw();

參數

x
矩形左上角的絕對 x 座標。

y
矩形左上角的絕對 Y 座標。

point
POINT結構,指定矩形的絕對左上角。

範例

CRect rect(0, 0, 100, 100);
rect.MoveToXY(10, 10);
// rect is now (10, 10, 110, 110);
ASSERT(rect == CRect(10, 10, 110, 110));

CRect::MoveToY

呼叫此函式,將矩形移至 所 y 指定的絕對 Y 座標。

void MoveToY(int y) throw();

參數

y
矩形左上角的絕對 Y 座標。

範例

CRect rect(0, 0, 100, 100);
rect.MoveToY(10);
// rect is now (0, 10, 100, 110);
ASSERT(rect == CRect(0, 10, 100, 110));

CRect::NormalizeRect

正規化 CRect ,讓高度和寬度都是正數。

void NormalizeRect() throw();

備註

矩形會針對第四象限位置正規化,Windows 通常會用於座標。 NormalizeRect 會比較頂端和底部值,並在頂端大於底部時交換它們。 同樣地,如果左邊大於右邊,則會交換左右的值。 處理不同的對應模式和反向矩形時,此函式很有用。

注意

下列 CRect 成員函式需要正規化矩形才能正常運作: HeightWidthEqualRectSizePtInRectIsRectEmptySubtractRectoperator ==IntersectRectUnionRectoperator |operator |=operator !=operator & 和 。 operator &=

範例

CRect rect1(110, 100, 250, 310);
CRect rect2(250, 310, 110, 100);
rect1.NormalizeRect();
rect2.NormalizeRect();
ASSERT(rect1 == rect2);

CRect::OffsetRect

依指定的位移移動 CRect

void OffsetRect(int x, int y) throw();
void OffsetRect(POINT point) throw();
void OffsetRect(SIZE size) throw();

參數

x
指定要向左或向右移動的數量。 向左移動必須是負數。

y
指定要向上或向下移動的數量。 向上移動必須是負數。

point
包含結構或 CPoint 物件,指定要移動的兩個 POINT 維度。

size
包含結構或 CSize 物件,指定要移動的兩個 SIZE 維度。

備註

沿著 CRectx X 軸移動單位,沿著 y Y 軸移動單位。 xy 參數是帶正負號的值,因此 CRect 可以向左或向右或向上或向下移動。

範例

CRect rect(0, 0, 35, 35);
rect.OffsetRect(230, 230);

// rect is now (230, 230, 265, 265)
ASSERT(rect == CRect(230, 230, 265, 265));

CRect::operator LPCRECTCRectLPCRECT 轉換為 。

operator LPCRECT() const throw();

備註

當您使用此函式時,不需要 ( & ) 運算子的位址。 當您將 CRect 物件傳遞至預期 LPCRECT 為 的函式時,會自動使用這個運算子。

CRect::operator LPRECT

CRect 轉換成 LPRECT

operator LPRECT() throw();

備註

當您使用此函式時,不需要 ( & ) 運算子的位址。 當您將 CRect 物件傳遞至預期 LPRECT 為 的函式時,會自動使用這個運算子。

範例

請參閱 的 CRect::operator LPCRECT 範例。

CRect::operator =

srcRect指派給 CRect

void operator=(const RECT& srcRect) throw();

參數

srcRect
是指來源矩形。 可以是 RECTCRect

範例

CRect rect(0, 0, 127, 168);
CRect rect2;

rect2 = rect;
ASSERT(rect2 == CRect(0, 0, 127, 168));

CRect::operator ==

藉由比較左上角和右下角的座標,判斷 是否 rectCRect 等。

BOOL operator==(const RECT& rect) const throw();

參數

rect
是指來源矩形。 可以是 RECTCRect

傳回值

如果相等,則為非零;否則為 0。

備註

注意

這兩個矩形都必須正規化,否則此函式可能會失敗。 您可以呼叫 NormalizeRect 來將矩形正規化,再呼叫此函式。

範例

CRect rect1(35, 150, 10, 25);
CRect rect2(35, 150, 10, 25);
CRect rect3(98, 999, 6, 3);
ASSERT(rect1 == rect2);
// works just fine against RECTs, as well

RECT test;
test.left = 35;
test.top = 150;
test.right = 10;
test.bottom = 25;

ASSERT(rect1 == test);

CRect::operator !=

藉由比較左上角和右下角的座標,判斷 是否 rect 不相等 CRect

BOOL operator!=(const RECT& rect) const throw();

參數

rect
是指來源矩形。 可以是 RECTCRect

傳回值

如果不相等,則為非零;否則為 0。

備註

注意

這兩個矩形都必須正規化,否則此函式可能會失敗。 您可以呼叫 NormalizeRect 來將矩形正規化,再呼叫此函式。

範例

CRect rect1(35, 150, 10, 25);
CRect rect2(35, 150, 10, 25);
CRect rect3(98, 999,  6,  3);
ASSERT(rect1 != rect3);
// works just fine against RECTs, as well

RECT test;
test.left = 35;
test.top = 150;
test.right = 10;
test.bottom = 25;

ASSERT(rect3 != test);

CRect::operator +=

前兩個多載會依指定的位移移動 CRect

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

參數

point
POINT結構或 CPoint 物件,指定要移動矩形的單位數目。

size
SIZE結構或 CSize 物件,指定要移動矩形的單位數目。

lpRect
指向 RECT 結構或 CRect 物件,其中包含要擴充每個側面的 CRect 單位數。

備註

參數的 xy (或 cxcy ) 值會新增至 CRect

第三個多載會 CRect 依參數每個成員中指定的單位數目來擴充。

範例

CRect   rect1(100, 235, 200, 335);
CPoint  pt(35, 65);
CRect   rect2(135, 300, 235, 400);

rect1 += pt;
ASSERT(rect1 == rect2);

CRect::operator -=

前兩個多載會依指定的位移移動 CRect

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

參數

point
POINT結構或 CPoint 物件,指定要移動矩形的單位數目。

size
SIZE結構或 CSize 物件,指定要移動矩形的單位數目。

lpRect
指向 RECT 結構或 CRect 物件,其中包含要解除的每一端 CRect 的單位數目。

備註

參數的 xy (或 cxcy ) 值會從 CRect 減去。

第三個多載會 CRect 依參數每個成員中指定的單位數目來解構。 請注意,這個多載函式類似 DeflateRect

範例

CRect   rect1(100, 235, 200, 335);
CPoint pt(35, 65);

rect1 -= pt;
CRect   rectResult(65, 170, 165, 270);
ASSERT(rect1 == rectResult);

CRect::operator &=

設定 CRect 等於 和 rectCRect 交集。

void operator&=(const RECT& rect) throw();

參數

rect
包含 RECTCRect

備註

交集是兩個矩形中包含的最大矩形。

注意

這兩個矩形都必須正規化,否則此函式可能會失敗。 您可以呼叫 NormalizeRect 來將矩形正規化,再呼叫此函式。

範例

請參閱 的 CRect::IntersectRect 範例。

CRect::operator |=

設定 CRect 等於 和 rectCRect 聯集。

void operator|=(const RECT& rect) throw();

參數

rect
包含 CRectRECT

備註

聯集是包含兩個來源矩形的最小矩形。

注意

這兩個矩形都必須正規化,否則此函式可能會失敗。 您可以呼叫 NormalizeRect 來將矩形正規化,再呼叫此函式。

範例

CRect   rect1(100,  0, 200, 300);
CRect   rect2(0, 100, 300, 200);

rect1 |= rect2;
CRect   rectResult(0, 0, 300, 300);
ASSERT(rectResult == rect1);

CRect::operator +

前兩個 CRect 多載會傳回等於 CRect 所指定位移所取代的物件。

CRect operator+(POINT point) const throw();
CRect operator+(LPCRECT lpRect) const throw();
CRect operator+(SIZE size) const throw();

參數

point
POINT結構或 CPoint 物件,指定要移動傳回值的單位數目。

size
SIZE結構或 CSize 物件,指定要移動傳回值的單位數目。

lpRect
指向 RECT 結構或 CRect 物件,其中包含要擴充傳回值之每一端的單位數目。

傳回值

CRect 參數中指定的單位數目移動或擴充 CRect 所產生的 。

備註

參數的 xy (或 cxcy ) 參數會新增至 CRect 的位置。

第三個多載會傳回新的 CRect ,其 CRect 等於參數每個成員中指定的單位數目。

範例

CRect   rect1(100, 235, 200, 335);
CPoint pt(35, 65);
CRect   rect2;

rect2 = rect1 + pt;
CRect   rectResult(135, 300, 235, 400);
ASSERT(rectResult == rect2);

CRect::operator -

前兩個 CRect 多載會傳回等於 CRect 所指定位移所取代的物件。

CRect operator-(POINT point) const throw();
CRect operator-(SIZE size) const throw();
CRect operator-(LPCRECT lpRect) const throw();

參數

point
POINT結構或 CPoint 物件,指定要移動傳回值的單位數目。

size
SIZE結構或 CSize 物件,指定要移動傳回值的單位數目。

lpRect
指向 RECT 結構或 CRect 物件,其中包含要解除傳回值每一端的單位數目。

傳回值

CRect 參數中指定的單位數目移動或取消擴充 CRect 所產生的 。

備註

參數的 x 和 (或 cx 和 ) cy 參數會從 CRecty 的位置減去。

第三個多載會傳回新的 CRect ,其 CRect 等於參數每個成員中指定的單位數目。 請注意,這個多載函式類似 DeflateRect ,而不是 SubtractRect

範例

CRect   rect1(100, 235, 200, 335);
CPoint pt(35, 65);
CRect   rect2;

rect2 = rect1 - pt;
CRect   rectResult(65, 170, 165, 270);
ASSERT(rect2 == rectResult);

CRect::operator &

CRect 回 ,其為 和 rect2 CRect 交集。

CRect operator&(const RECT& rect2) const throw();

參數

rect2
包含 RECTCRect

傳回值

CRect,為 和 rect2CRect 交集。

備註

交集是兩個矩形中包含的最大矩形。

注意

這兩個矩形都必須正規化,否則此函式可能會失敗。 您可以呼叫 NormalizeRect 來將矩形正規化,再呼叫此函式。

範例

CRect   rect1(100,  0, 200, 300);
CRect   rect2(0, 100, 300, 200);
CRect   rect3;

rect3 = rect1 & rect2;
CRect   rectResult(100, 100, 200, 200);
ASSERT(rectResult == rect3);

CRect::operator |

CRect 回 ,其為 和 rect2CRect 聯集。

CRect operator|(const RECT&
rect2) const throw();

參數

rect2
包含 RECTCRect

傳回值

CRect,為 和 rect2CRect 聯集。

備註

聯集是包含這兩個矩形的最小矩形。

注意

這兩個矩形都必須正規化,否則此函式可能會失敗。 您可以呼叫 NormalizeRect 來將矩形正規化,再呼叫此函式。

範例

CRect   rect1(100,  0, 200, 300);
CRect   rect2(0, 100, 300, 200);
CRect   rect3;

rect3 = rect1 | rect2;
CRect   rectResult(0, 0, 300, 300);
ASSERT(rectResult == rect3);

CRect::PtInRect

判斷指定的點是否位於 內 CRect

BOOL PtInRect(POINT point) const throw();

參數

point
包含 POINT 結構或 CPoint 物件。

傳回值

如果點位於 內 CRect ,則為非零;否則為 0。

備註

如果某個點 CRect 位於左側或頂端,或位於所有四側,則為內部。 右側或底部的點位於 外部 CRect

注意

矩形必須正規化,否則此函式可能會失敗。 您可以呼叫 NormalizeRect 將矩形正規化,再呼叫此函式。

範例

CRect rect(5, 5, 100, 100);
CPoint pt1(35, 50);
CPoint pt2(125, 298);

// this is true, because pt1 is inside the rectangle
ASSERT(rect.PtInRect(pt1));

// this is NOT true, because pt2 is outside the rectangle
ASSERT(!rect.PtInRect(pt2));

// note that the right and the bottom aren't inside
ASSERT(!rect.PtInRect(CPoint(35, 100)));
ASSERT(!rect.PtInRect(CPoint(100, 98)));

// but the top and the left are inside
ASSERT(rect.PtInRect(CPoint(5, 65)));
ASSERT(rect.PtInRect(CPoint(88, 5)));

// and that PtInRect() works against a POINT, too
POINT pt;
pt.x = 35;
pt.y = 50;
ASSERT(rect.PtInRect(pt));

CRect::SetRect

將 的 CRect 維度設定為指定的座標。

void SetRect(int x1, int y1, int x2, int y2) throw();

參數

x1
指定左上角的 X 座標。

y1
指定左上角的 Y 座標。

x2
指定右下角的 X 座標。

y2
指定右下角的 Y 座標。

範例

CRect rect;
rect.SetRect(256, 256, 512, 512);
ASSERT(rect == CRect(256, 256, 512, 512));

CRect::SetRectEmpty

將所有座標設定為零,建立 CRect Null 矩形。

void SetRectEmpty() throw();

範例

CRect rect;
rect.SetRectEmpty();

// rect is now (0, 0, 0, 0)
ASSERT(rect.IsRectEmpty());

CRect::SIZE

cx 回值的 和 cy 成員包含 的高度和寬度 CRect

CSize Size() const throw();

傳回值

CSize物件,包含 的大小 CRect

備註

高度或寬度可以是負數。

注意

矩形必須正規化,否則此函式可能會失敗。 您可以呼叫 NormalizeRect 將矩形正規化,再呼叫此函式。

範例

CRect rect(10, 10, 50, 50);
CSize sz = rect.Size();
ASSERT(sz.cx == 40 && sz.cy == 40);

CRect::SubtractRect

使 的 CRect 維度等於 的減法 lpRectSrc2lpRectSrc1

BOOL SubtractRect(LPCRECT lpRectSrc1, LPCRECT lpRectSrc2) throw();

參數

lpRectSrc1
RECT指向要從中減去矩形的結構或 CRect 物件。

lpRectSrc2
RECT指向要從 參數所指向 lpRectSrc1 之矩形減去的結構或 CRect 物件。

傳回值

如果函式成功則為非零,否則為 0。

備註

減法是包含 和 交集 lpRectScr1lpRectScr2 中所有點 lpRectScr1 的最小矩形。

如果 所指定的矩形未完全重迭在至少其中一個 x 或 y 方向中指定的矩形,則 所 lpRectSrc2 指定的 lpRectSrc1 矩形 lpRectSrc1 不會變更。

例如,如果 lpRectSrc1 是 (10,10,100,100),而 lpRectSrc2 是 (50,50, 150,150,150),則 所指向 lpRectSrc1 的矩形會在函式傳回時保持不變。 如果 lpRectSrc1 是 (10,10,100,100,100),且 lpRectSrc2 是 (50,10, 150,150),則 所 lpRectSrc1 指向的矩形會包含函式傳回時的座標 (10,10, 50,100)。

SubtractRect與運算子 - 運算子 -= 不同 。 這兩個運算子都未呼叫 SubtractRect

注意

這兩個矩形都必須正規化,否則此函式可能會失敗。 您可以呼叫 NormalizeRect 來將矩形正規化,再呼叫此函式。

範例

RECT   rectOne;
RECT   rectTwo;

rectOne.left = 10;
rectOne.top = 10;
rectOne.bottom = 100;
rectOne.right = 100;

rectTwo.left = 50;
rectTwo.top = 10;
rectTwo.bottom = 150;
rectTwo.right = 150;

CRect   rectDiff;

rectDiff.SubtractRect(&rectOne, &rectTwo);
CRect   rectResult(10, 10, 50, 100);

ASSERT(rectDiff == rectResult);

// works for CRect, too, since there is
// implicit CRect -> LPCRECT conversion

CRect rect1(10, 10, 100, 100);
CRect rect2(50, 10, 150, 150);
CRect rectOut;

rectOut.SubtractRect(rect1, rect2);
ASSERT(rectResult == rectOut);

CRect::TopLeft

座標會以 包含在 中的 CRect 物件參考 CPoint 傳回。

CPoint& TopLeft() throw();
const CPoint& TopLeft() const throw();

傳回值

矩形左上角的座標。

備註

您可以使用此函式來取得或設定矩形的左上角。 使用指派運算子左邊的這個函式來設定角落。

範例

請參閱 的 CRect::CenterPoint 範例。

CRect::UnionRect

使 的 CRect 維度等於兩個來源矩形的聯集。

BOOL UnionRect(LPCRECT lpRect1, LPCRECT lpRect2) throw();

參數

lpRect1
RECT指向 包含來源矩形的 或 CRect

lpRect2
RECT指向 包含來源矩形的 或 CRect

傳回值

如果聯集不是空的,則為非零;如果聯集是空的,則為 0。

備註

聯集是包含兩個來源矩形的最小矩形。

Windows 會忽略空矩形的維度;也就是說,沒有高度或沒有寬度的矩形。

注意

這兩個矩形都必須正規化,否則此函式可能會失敗。 您可以呼叫 NormalizeRect 來將矩形正規化,再呼叫此函式。

範例

CRect   rect1(100,  0, 200, 300);
CRect   rect2(0, 100, 300, 200);
CRect   rect3;

rect3.UnionRect(&rect1, &rect2);
CRect   rectResult(0, 0, 300, 300);
ASSERT(rectResult == rect3);

CRect::Width

藉由從右值減去左值來計算 的寬度 CRect

int Width() const throw();

傳回值

CRect 寬度。

備註

寬度可以是負數。

注意

矩形必須正規化,否則此函式可能會失敗。 您可以呼叫 NormalizeRect 將矩形正規化,再呼叫此函式。

範例

CRect rect(20, 30, 80, 70);
int nWid = rect.Width();
// nWid is now 60
ASSERT(nWid == 60);

另請參閱

CPoint
CSize
RECT