CRect 类

注意

继续支持Microsoft基础类(MFC)库和活动模板库(ATL)。 但是,我们不再添加功能或更新文档。

类似于 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 是否为空。 如果宽度和/或高度为 0,则 CRect 为空。
CRect::IsRectNull 确定 top、bottom、left 和 right 成员变量是否都等于 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 - 从 CRect 减去给定的偏移量,或缩小 CRect 并返回得出的 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 或 LPCRECT 的任何位置,都可以传递 LPRECT 对象作为函数参数。

注意

此类派生自 tagRECT 结构。 (名称 tagRECT 是 RECT 结构的一个不太常用的名称。)这意味着 left 结构的数据成员(top、right、bottom 和 RECT)是 CRect 的可访问数据成员。

CRect 包含用于定义矩形左上角和右下角点的成员变量。

指定 CRect 时,必须小心构造它,以便将其规范化 — 换言之,左侧坐标的值小于右侧坐标的值,上侧坐标的值小于下侧坐标的值。 例如,左上角坐标 (10,10) 和右下角坐标 (20,20) 定义一个规范化矩形,而左上角坐标 (20,20) 和右下角坐标 (10,10) 则是定义一个非规范化矩形。 如果矩形未规范化,许多 CRect 成员函数可能返回错误的结果。 (有关这些函数的列表,请参阅 CRect::NormalizeRect。)在调用需要规范化矩形的函数之前,可以通过调用 NormalizeRect 函数将非规范化矩形规范化。

使用 CRect 和 CDC::DPtoLP 成员函数操作 CDC::LPtoDP 时请小心。 如果显示上下文的映射模式使得 y 范围为负(如 MM_LOENGLISH 中一样),则 CDC::DPtoLP 将转换 CRect,使其上侧大于下侧。 然后,Height 和 Size 等函数将为转换的 CRect 的高度返回负值,并且矩形是非规范化的。

使用重载的 CRect 运算符时,第一个操作数必须是 CRect;第二个操作数可以是 RECT 结构或 CRect 对象。

继承层次结构

tagRECT

CRect

要求

标头:atltypes.h

CRect::BottomRight

坐标作为对 CPoint 中包含的 CRect 对象的引用返回。

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

通过将左右边值相加并除以 2,并将上下边值相加并除以 2 来计算 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、top、right 和 bottom 成员将设置为 0。

CRect(const RECT&) 和 CRect(LPCRECT) 构造函数执行 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
一个 SIZE 或 CSize,用于指定要将 CRect 缩小的单位数。 cx 值指定要将左右边缩小的单位数,cy 值指定要将上下边缩小的单位数。

lpRect
指向用于指定要将每条边缩小的单位数 RECT 结构或 CRect。

l
指定要将 CRect 的左侧边缩小的单位数。

t
指定要将 CRect 的上部边缩小的单位数。

r
指定要将 CRect 的右侧边缩小的单位数。

b
指定要将 CRect 的下部边缩小的单位数。

备注

为此,DeflateRect 会将单位数与左侧边和上部边相加,从右侧边和下部边减去单位数。 DeflateRect 的参数是有符号值;正值缩小 CRect,负值将其放大。

前两个重载将 CRect 的两组对边缩小,使其总宽度减少 x(或 cx)的两倍,总高度减少 y(或 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
一个 SIZE 或 CSize,用于指定要将 CRect 放大的单位数。 cx 值指定要将左右边放大的单位数,cy 值指定要将上下边放大的单位数。

lpRect
指向用于指定要将每条边放大的单位数 RECT 结构或 CRect。

l
指定要将 CRect 的左侧边放大的单位数。

t
指定要将 CRect 的上部边放大的单位数。

r
指定要将 CRect 的右侧边放大的单位数。

b
指定要将 CRect 的下部边放大的单位数。

备注

为此,InflateRect 会从左侧边和上部边减去单位数,并将单位数与右侧边和下部边相加。 InflateRect 的参数是有符号值;正值放大 CRect,负值将其缩小。

前两个重载将 CRect 的两组对边放大,使其总宽度增加 x(或 cx)的两倍,总高度增加 y(或 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 成员函数需要规范化矩形才能正常运行:Height、Width、Size、IsRectEmpty、PtInRect、EqualRect、UnionRect、IntersectRect、SubtractRect、operator ==、operator !=、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
包含 POINT 结构或 CPoint 对象,用于指定两个移动维度。

size
包含 SIZE 结构或 CSize 对象,用于指定两个移动维度。

备注

沿 x 轴移动 CRectx 个单位,沿 y 轴移动 y 个单位。 x 和 y 参数是有符号值,因此 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 LPCRECT

将 CRect 转换为 LPCRECT。

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
引用源矩形。 可以是 RECT 或 CRect。

示例

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

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

CRect::operator ==

通过比较左上角和右下角的坐标来确定 rect 是否等于 CRect。

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

参数

rect
引用源矩形。 可以是 RECT 或 CRect。

返回值

如果相等,则返回非零值;否则返回 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
引用源矩形。 可以是 RECT 或 CRect。

返回值

如果不相等,则返回非零值;否则返回 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 的每条边放大的单位数。

备注

参数的 x 和 y(或者 cx 和 cy)值将与 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 的每条边缩小的单位数。

备注

将从 x 减去参数的 y 和 cx(或者 cy 和 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 等于 CRect 和 rect 的交集。

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

参数

rect
包含 RECT 或 CRect。

备注

交集是两个矩形中包含的最大矩形。

注意

两个矩形都必须规范化,否则此函数可能失败。 在调用此函数之前,可以调用 NormalizeRect 来规范化矩形。

示例

请参阅 CRect::IntersectRect 的示例。

CRect::operator |=

设置 CRect 等于 CRect 和 rect 的并集。

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

参数

rect
包含 CRect 或 RECT。

备注

并集是包含两个源矩形的最小矩形。

注意

两个矩形都必须规范化,否则此函数可能失败。 在调用此函数之前,可以调用 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。

备注

参数的 x 和 y(或者 cx 和 cy)参数将与 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 的位置减去参数的 y 和 cx(或者 cy 和 CRect)参数。

第三个重载返回一个新的 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,即是 CRect 和 rect2 的交集。

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

参数

rect2
包含 RECT 或 CRect。

返回值

一个 CRect,即 CRect 和 rect2 的交集。

备注

交集是两个矩形中包含的最大矩形。

注意

两个矩形都必须规范化,否则此函数可能失败。 在调用此函数之前,可以调用 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,即 CRect 和 rect2 的并集。

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

参数

rect2
包含 RECT 或 CRect。

返回值

一个 CRect,即 CRect 和 rect2 的并集。

备注

并集是包含两个矩形的最小矩形。

注意

两个矩形都必须规范化,否则此函数可能失败。 在调用此函数之前,可以调用 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 的维度等于从 lpRectSrc2 减去 lpRectSrc1 后的结果。

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

参数

lpRectSrc1
指向要从中减去矩形的 RECT 结构或 CRect 对象。

lpRectSrc2
指向要从 RECT 参数指向的矩形中减去的 CRect 结构或 lpRectSrc1 对象。

返回值

如果该函数成功,则为非 0;否则为 0。

备注

减法是包含 lpRectScr1 中不在 lpRectScr1 和 lpRectScr2 的交集中的所有点的最小矩形。

如果 lpRectSrc1 指定的矩形与 lpRectSrc2 指定的矩形至少在 x 或 y 方向之一上未完全重叠,则 lpRectSrc1 指定的矩形将保持不变。

例如,如果 lpRectSrc1 是 (10,10, 100,100) 且 lpRectSrc2 是 (50,50, 150,150),则当函数返回时,lpRectSrc1 指向的矩形将保持不变。 但是,如果 lpRectSrc1 是 (10,10, 100,100) 且 lpRectSrc2 是 (50,10, 150,150),则当函数返回时,lpRectSrc1 指向的矩形将包含坐标 (10,10, 50,100)。

SubtractRect 与 operator - 和 operator -= 不同。 这两个运算符永不调用 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

坐标作为对 CPoint 中包含的 CRect 对象的引用返回。

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