CPoint、CSize和CRect(POINT和SIZE、RECT的派生类)
CRect
CRect类是MFC中用于表示矩形的类。它提供了方便的方法来操作矩形的位置和大小。CRect类常用于图形绘制、窗口管理和布局等场景。
可以用于绘制矩形、椭圆、圆等图形时需要指定其位置和大小。此外,CRect类还常用于窗口管理,可以通过CRect对象来设置窗口的位置和大小。它还可以用于布局操作,例如计算矩形的交集、并集等。
CRect类说明
typedef struct tagRECT
{
LONG left; //矩形的左边界
LONG top; //矩形的上边界
LONG right; //矩形的右边界
LONG bottom; //矩形的下边界
} RECT, *PRECT, NEAR *NPRECT, FAR *LPRECT;
class CRect :
public tagRECT
{
// Constructors
public:
// uninitialized rectangle
CRect() throw();
// from left, top, right, and bottom
CRect(
_In_ int l,
_In_ int t,
_In_ int r,
_In_ int b) throw();
// copy constructor
CRect(_In_ const RECT& srcRect) throw();
// from a pointer to another rect
//typedef const RECT* LPCRECT ;
CRect(_In_ LPCRECT lpSrcRect) throw();
// from a point and size
CRect(
_In_ POINT point,
_In_ SIZE size) throw()
{
left = point.x;
top = point.y;
right = point.x+size.cx;
bottom = point.y+size.cy;
}
// from two points
CRect(
_In_ POINT topLeft,
_In_ POINT bottomRight) throw();
{
left = topLeft.x;
right = bottomRight.x;
}
// Attributes (in addition to RECT members)
// retrieves the width
int Width() const throw();
// returns the height
int Height() const throw();
// returns the size
CSize Size() const throw();
// reference to the top-left point
CPoint& TopLeft() throw();
// reference to the bottom-right point
CPoint& BottomRight() throw();
// const reference to the top-left point
const CPoint& TopLeft() const throw();
// const reference to the bottom-right point
const CPoint& BottomRight() const throw();
// the geometric center point of the rectangle
CPoint CenterPoint() const throw();
// swap the left and right
void SwapLeftRight() throw();
static void WINAPI SwapLeftRight(_Inout_ LPRECT lpRect) throw();
// convert between CRect and LPRECT/LPCRECT (no need for &)
operator LPRECT() throw(); //自动把对象转为指针类型。
operator LPCRECT() const throw();//自动把对象转为指针类型。
// returns TRUE if rectangle has no area
BOOL IsRectEmpty() const throw();
// returns TRUE if rectangle is at (0,0) and has no area
BOOL IsRectNull() const throw();
// returns TRUE if point is within rectangle
BOOL PtInRect(_In_ POINT point) const throw(); 某个点是否在矩形区域内
// Operations
//修改
// set rectangle from left, top, right, and bottom 设置矩形的位置和大小。
void SetRect(
_In_ int x1,
_In_ int y1,
_In_ int x2,
_In_ int y2) throw();
void SetRect(
_In_ POINT topLeft,
_In_ POINT bottomRight) throw();
// empty the rectangle
void SetRectEmpty() throw();//4个数值清零 将矩形设置为空矩形
// copy from another rectangle
void CopyRect(_In_ LPCRECT lpSrcRect) throw();
// TRUE if exactly the same as another rectangle
BOOL EqualRect(_In_ LPCRECT lpRect) const throw();
// Inflate rectangle's width and height by
// x units to the left and right ends of the rectangle
// and y units to the top and bottom.
void InflateRect(
_In_ int x,
_In_ int y) throw();
// Inflate rectangle's width and height by
// size.cx units to the left and right ends of the rectangle
// and size.cy units to the top and bottom.
void InflateRect(_In_ SIZE size) throw();
// Inflate rectangle's width and height by moving individual sides.
// Left side is moved to the left, right side is moved to the right,
// top is moved up and bottom is moved down.
void InflateRect(_In_ LPCRECT lpRect) throw();
void InflateRect(
_In_ int l,
_In_ int t,
_In_ int r,
_In_ int b) throw();
// deflate the rectangle's width and height without
// moving its top or left
void DeflateRect(
_In_ int x,
_In_ int y) throw();
void DeflateRect(_In_ SIZE size) throw();
void DeflateRect(_In_ LPCRECT lpRect) throw();
void DeflateRect(
_In_ int l,
_In_ int t,
_In_ int r,
_In_ int b) throw();
// translate the rectangle by moving its top and left
void OffsetRect( //将矩形对象的位置按指定的偏移量进行平移
_In_ int x,
_In_ int y) throw();
void OffsetRect(_In_ SIZE size) throw();
void OffsetRect(_In_ POINT point) throw();
void NormalizeRect() throw();
// absolute position of rectangle
void MoveToY(_In_ int y) throw();
void MoveToX(_In_ int x) throw();
void MoveToXY(
_In_ int x,
_In_ int y) throw();
void MoveToXY(_In_ POINT point) throw();
// set this rectangle to intersection of two others
BOOL IntersectRect(
_In_ LPCRECT lpRect1,
_In_ LPCRECT lpRect2) throw();
// set this rectangle to bounding union of two others
BOOL UnionRect(
_In_ LPCRECT lpRect1,
_In_ LPCRECT lpRect2) throw();
// set this rectangle to minimum of two others
BOOL SubtractRect(
_In_ LPCRECT lpRectSrc1,
_In_ LPCRECT lpRectSrc2) throw();
// Additional Operations
void operator=(_In_ const RECT& srcRect) throw();//将一个矩形对象的坐标值赋给另一个矩形对象
BOOL operator==(_In_ const RECT& rect) const throw();
BOOL operator!=(_In_ const RECT& rect) const throw();
void operator+=(_In_ POINT point) throw();
void operator+=(_In_ SIZE size) throw();
void operator+=(_In_ LPCRECT lpRect) throw();
void operator-=(_In_ POINT point) throw();
void operator-=(_In_ SIZE size) throw();
void operator-=(_In_ LPCRECT lpRect) throw();
void operator&=(_In_ const RECT& rect) throw();
void operator|=(_In_ const RECT& rect) throw();
通过点击按钮使窗口进行偏移
void CCRectDlg::OnBnClickedButton1()
{
CRect rect;
GetWindowRect(rect);
rect.OffsetRect(3, 5);
CSize size = rect.Size();
// CSize(right - left, bottom - top) 返回cx,cy 高宽 CSzie这个表示高宽的时候用 把高宽和在一起
MoveWindow(rect);
}
CPoint
CPoint类是MFC中用于表示二维平面上的点的类。它被广泛应用于图形绘制、鼠标操作和窗口管理。
在图形绘制中,可以使用CPoint对象来表示图形的顶点坐标,通过操作CPoint对象的坐标属性可以方便地绘制各种图形。在鼠标操作中,可以使用CPoint对象来表示鼠标的位置,从而实现对鼠标事件的处理。在窗口管理中,可以使用CPoint对象来表示窗口的位置,通过操作CPoint对象的坐标属性可以实现窗口的移动和调整。
CPoint类说明
class CPoint :
public tagPOINT
{
public:
// Constructors
// create an uninitialized point
CPoint() throw(); 默认构造函数,创建一个坐标为(0, 0)的点。
// create from two integers
CPoint( 带参数的构造函数,根据给定的x和y坐标创建一个点。
_In_ int initX,
_In_ int initY) throw();
// create from another point
CPoint(_In_ POINT initPt) throw();
// create from a size
CPoint(_In_ SIZE initSize) throw();
// create from an LPARAM: x = LOWORD(dw) y = HIWORD(dw)
CPoint(_In_ LPARAM dwPoint) throw();
// Operations
// translate the point
偏移:Offset
void Offset( 将点在x和y方向上分别偏移dx和dy个单位。可以用于移动点的位置。
_In_ int xOffset,
_In_ int yOffset) throw();
void Offset(_In_ POINT point) throw();
void Offset(_In_ SIZE size) throw();
//修改
void SetPoint(
_In_ int X,
_In_ int Y) throw();
//重载相等运算符,判断两个点是否相等。如果两个点的x和y坐标都相等,则返回TRUE,否则返回FALSE。
BOOL operator==(_In_ POINT point) const throw();
//重载不等运算符,判断两个点是否不相等。如果两个点的x和y坐标至少有一个不相等,则返回TRUE,否则返回FALSE。
BOOL operator!=(_In_ POINT point) const throw();
void operator+=(_In_ SIZE size) throw();
void operator-=(_In_ SIZE size) throw();
void operator+=(_In_ POINT point) throw();
void operator-=(_In_ POINT point) throw();
};
CSize
CSize类是MFC中用于表示二维平面上的大小的类。它通常用于指定图形对象的尺寸、窗口的大小、位图的尺寸等。CSize类提供了方便的方法来操作大小的宽度和高度。主要来获取对应的宽高。
可以用于指定图形对象的大小,例如绘制矩形、椭圆、圆等图形时需要指定其大小。此外,CSize类还常用于窗口管理,可以通过CSize对象来设置窗口的大小或获取窗口的大小。
CSize类说明
//高宽合并的结构体cx,cy
class CSize :
public tagSIZE
{
public:
// Constructors
// construct an uninitialized size
CSize() throw(); //默认构造函数,创建一个大小为0的CSize对象。
// create from two integers
CSize( //根据给定的宽度(cx)和高度(cy)创建一个CSize对象。
_In_ int initCX,
_In_ int initCY) throw();
// create from another size
CSize(_In_ SIZE initSize) throw();
// create from a point
CSize(_In_ POINT initPt) throw();
// create from a DWORD: cx = LOWORD(dw) cy = HIWORD(dw)
CSize(_In_ DWORD dwSize) throw();
// Operations
BOOL operator==(_In_ SIZE size) const throw();
BOOL operator!=(_In_ SIZE size) const throw();
void operator+=(_In_ SIZE size) throw();
void operator-=(_In_ SIZE size) throw();
void SetSize(_In_ int CX, _In_ int CY) throw();
// Operators returning CSize values
CSize operator+(_In_ SIZE size) const throw();
CSize operator-(_In_ SIZE size) const throw();
CSize operator-() const throw();
// Operators returning CPoint values
CPoint operator+(_In_ POINT point) const throw();
CPoint operator-(_In_ POINT point) const throw();
// Operators returning CRect values
CRect operator+(_In_ const RECT* lpRect) const throw();
CRect operator-(_In_ const RECT* lpRect) const throw();
};
附录
1、消息被控件捕获父窗口窗口无法获取
两种解决办法1、使用控件派生类;2、使用WM_SETCURSOR消息。
BOOL CBossDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) //光标切换消息
{
CString str;
pWnd->GetWindowText(str);
TRACE(str + _T("\r\n"));
return CDialogEx::OnSetCursor(pWnd, nHitTest, message);
}
2、CWnd类的坐标系转换函数:(来自于API)
a)ScreenToClient:屏幕坐标系转为某主窗口的客户区坐标系
void ScreenToClient( LPPOINT lpPoint ) const;
void ScreenToClient( LPRECT lpRect ) const;
b)ClientToScreen:以上的反向转换
void ClientToScreen( LPPOINT lpPoint ) const;
void ClientToScreen( LPRECT lpRect ) const;
3、对话框程序对象的生命期:文章来源:https://www.toymoban.com/news/detail-501123.html
a)CDialog::DoModal()函数弹出对话框,在这个函数阻塞期间对象内是有句柄(不为空)
b)执行DoModal()之前,和阻塞结束对话框关闭后,句柄都是NULL。
c)因此DoModal之前和之后都不能执行控件和窗口操作。文章来源地址https://www.toymoban.com/news/detail-501123.html
到了这里,关于MFC第八天 基本数据类型介绍的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!