MFC中的CRect
Posted htj10
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MFC中的CRect相关的知识,希望对你有一定的参考价值。
VC中的CRect
环境VS2013
头文件:#include <afxwin.h>
A CRect object can be passed as a function parameter wherever a RECT structure, LPCRECT, or LPRECT can be passed.
当需要 RECT*、LPCRECT、LPRECT 类型参数传参时,可以直接使用CRect。
因为重载操作符了
operator LPCRECT Converts a CRect to an LPCRECT.
operator LPRECT Converts a CRect to an LPRECT.
常用函数:
Construction
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 rc1;//VS2013默认构造 0,0,0,0;注:VC6.0 可能不是全0,只是不初始化
CRect rc2(10, 10, 200, 100);
Operations(操作函数)
- 获取宽度、高度
Width()
Height()
Size()
例子:
//宽高
int w = rc2.Width();
int h = rc2.Height();
CSize sz = rc2.Size();//cx:width cy:height
- 获取点坐标
TopLeft() Returns the top-left point of CRect.
BottomRight() Returns the bottom-right point of CRect.
CenterPoint() Returns the centerpoint of CRect. (return CPoint((left+right)/2, (top+bottom)/2);)
- 判断是否为空矩形(一种是全0,一种是面积为0)
IsRectEmpty() Determines whether CRect is empty. CRect is empty if the width and/or height are 0.
IsRectNull() Determines whether the top, bottom, left, and right member variables are all equal to 0.
- 判断 pt 点是否在矩形内(在边界上返回TRUE)
PtInRect(POINT pt) Determines whether the specified point lies within CRect.
- 设置矩形坐标点
void SetRect( int l, int t, int r, int b );
- 放大矩形/缩小矩形
InflateRect(..) Increases the width and height of CRect. void InflateRect( int x, int y ); void InflateRect( SIZE size ); void InflateRect( LPCRECT lpRect ); void InflateRect( int l, int t, int r, int b ); DeflateRect(..) Decreases the width and height of CRect. void DeflateRect( int x, int y ); void DeflateRect( SIZE size ); void DeflateRect( LPCRECT lpRect ); void DeflateRect( int l, int t, int r, int b );
例子:
CRect rc(10, 10, 200, 100); rc.InflateRect(5, 6); // (10-5, 10-6, 200+5, 100+6) --> (5, 4, 205, 106) rc.SetRect(10, 10, 200, 100); rc.InflateRect(1, 2, 3, 4); // (10-1, 10-2, 200+3, 100+4) --> (9, 8, 203, 104) rc.SetRect(10, 10, 200, 100); rc.InflateRect(-5, 6); // (10+5, 10-6, 200-5, 100+6) --> (15, 4, 195, 106) rc.SetRect(10, 10, 200, 100); rc.InflateRect(-5, -6); //相当于缩小矩形 rc.DeflateRect(5, 6); --> (15, 16, 195, 94) rc.SetRect(10, 10, 200, 100); rc.DeflateRect(5, 6); // --> (15, 16, 195, 94) rc.SetRect(10, 10, 200, 100); rc.DeflateRect(1, 2, 3, 4); // (10+1, 10+2, 200-3, 100-4) --> (11, 12, 197, 96)
- 正常化矩形
NormalizeRect() Standardizes the height and width of CRect.
例子:
CRect rect1(110, 100, 250, 310);
CRect rect2(250, 310, 110, 100);
rect1.NormalizeRect();
rect2.NormalizeRect();
// rect1 should be unchanged
// rect2 becomes (110, 100, 250, 310)
ASSERT(rect1 == rect2);
- 平移矩形
void OffsetRect( int x, int y );
void OffsetRect( POINT point );
void OffsetRect( SIZE size );
例子:
CRect rc(10, 10, 200, 100); rc.OffsetRect(5, 6);//left +5, right +5 ; top +6, bottom +6 --> (15, 16, 205, 106) rc.SetRect(10, 10, 200, 100); rc.InflateRect(5, 6);// left -5, right +5 ; top -6, bottom +6 --> (5, 4, 205, 106) //对比 // OffsetRect(x,y) :表示平移,左右的坐标都加x,上下的坐标都加y // InflateRect(x,y):表示放大,左减右加 x,上减下加 y // 另外 void InflateRect( int l, int t, int r, int b ); 可以等价 void OffsetRect( int x, int y ); rc.SetRect(10, 10, 200, 100); rc.InflateRect(-5, -6, 5, 6);//等价 rc.OffsetRect(5, 6); --> (15, 16, 205, 106)
- 矩形lprcSrc1中减去lprcSrc2得到的矩形区域结果。
注:lprcSrc1与lprcSrc2这两个矩形必须在水平或垂直方向完全相交。换句话说,倘若在lprcSrc1中拿掉与lprcSrc2相交的部分,结果必须是个矩形。
BOOL SubtractRect( LPCRECT lpRectSrc1, LPCRECT lpRectSrc2 );
例子:
CRect rc1(10, 10, 200, 200); CRect rc2(10, 10, 200, 100); CRect rcRes; rcRes.SubtractRect(rc1, rc2);//rcRes = (10,100,200,200) //若去掉后不是矩形则返回rc1 rcRes.SubtractRect(rc1, CRect(10,10,50,50));// rcRes = (10, 10, 200, 200) rcRes.SubtractRect(rc1, CRect(2, 2, 50, 50));// rcRes = (10, 10, 200, 200) rcRes.SubtractRect(rc1, CRect(100, 100, 200, 200));// rcRes = (10, 10, 200, 200) // 只要去掉俩个矩形的交集后还是一个矩形就行 rcRes.SubtractRect(rc1, CRect(25, 0, 300, 300));// rcRes = (10, 10, 25, 200) rcRes.SubtractRect(rc1, CRect(2, 50, 500, 300));// rcRes = (10, 10, 200, 50)
- 获得能包含这两个矩形的最小矩形
BOOL UnionRect( LPCRECT lpRect1, LPCRECT lpRect2 );
例子:
CRect rect1(100, 0, 200, 300); CRect rect2(0, 100, 300, 200); CRect rect3; // 获得能放下两个矩形的最小矩形 rect3.UnionRect(&rect1, &rect2);// (0, 0, 300, 300) rect3.UnionRect(CRect(10, 10, 50, 50), CRect(100, 100, 150, 150));// (10, 10, 150, 150) rect3.UnionRect(CRect(10, 10, 50, 50), CRect(10, 10, 15, 15));// (10, 10, 50, 50) rect3.UnionRect(CRect(10, 10, 50, 50), CRect(8, 9, 15, 15));// (8, 9, 50, 50)
- 获取两个矩形的交集矩形
BOOL IntersectRect( LPCRECT lpRect1, LPCRECT lpRect2 );
例子:
CRect rect1(100, 0, 200, 300); CRect rect2(0, 100, 300, 200); CRect rect3; // 获得能两个矩形的交集矩形 rect3.IntersectRect(rect1, rect2);// (100, 100, 200, 200) rect3.IntersectRect(CRect(10, 10, 50, 50), CRect(100, 100, 150, 150));// (0, 0, 0, 0) rect3.IntersectRect(CRect(10, 10, 50, 50), CRect(10, 10, 15, 15));// (10, 10, 15, 15) rect3.IntersectRect(CRect(10, 10, 50, 50), CRect(8, 9, 15, 15));// (10, 10, 15, 15) rect3.IntersectRect(CRect(10, 10, 50, 50), CRect(0, 0, 100, 100));// (10, 10, 50, 50)
另外:
Operators (操作符重载)
operator LPCRECT Converts a CRect to an LPCRECT. operator LPRECT Converts a CRect to an LPRECT. operator = Copies the dimensions of a rectangle to CRect. operator == Determines whether CRect is equal to a rectangle. operator != Determines whether CRect is not equal to a rectangle. operator += Adds the specified offsets to CRect or inflates CRect. operator –= Subtracts the specified offsets from CRect or deflates CRect. operator &= Sets CRect equal to the intersection of CRect and a rectangle. operator |= Sets CRect equal to the union of CRect and a rectangle. operator + Adds the given offsets to CRect or inflates CRect and returns the resulting CRect. operator – Subtracts the given offsets from CRect or deflates CRect and returns the resulting CRect. operator & Creates the intersection of CRect and a rectangle and returns the resulting CRect. operator | Creates the union of CRect and a rectangle and returns the resulting CRect.
CRect::operator +
CRect operator +( POINT point ) const;
CRect operator +( LPCRECT lpRect ) const;
CRect operator +( SIZE size ) const;
Return Value
The CRect resulting from moving or inflating CRect by the number of units specified in the parameter.
Example
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 operator -( POINT point ) const;
CRect operator -( SIZE size ) const;
CRect operator -( LPCRECT lpRect ) const;
Return Value
The CRect resulting from moving or deflating CRect by the number of units specified in the parameter.
Example
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 operator &( const RECT& rect2 ) const;
Return Value
A CRect that is the intersection of CRect and rect2.
Example
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 operator |( const RECT& rect2 ) const;
Return Value
A CRect that is the union of CRect and rect2.
Remarks
Returns a CRect that is the union of CRect and rect2. The union is the smallest rectangle that contains both rectangles.
Example
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);
-----------------------------------------------
以上都要注意:执行前的矩形正常化
Note Both of the rectangles must be normalized or this function may fail. You can call NormalizeRect to normalize the rectangles before calling this function.
以上是关于MFC中的CRect的主要内容,如果未能解决你的问题,请参考以下文章
猎豹MFC1 第一个窗口CWinApp CFrameWnd InitInstance 消息映射CPaint CRect Create