MFC第八天 基本数据类型介绍

这篇具有很好参考价值的文章主要介绍了MFC第八天 基本数据类型介绍。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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、对话框程序对象的生命期:

a)CDialog::DoModal()函数弹出对话框,在这个函数阻塞期间对象内是有句柄(不为空)
b)执行DoModal()之前,和阻塞结束对话框关闭后,句柄都是NULL。
c)因此DoModal之前和之后都不能执行控件和窗口操作。文章来源地址https://www.toymoban.com/news/detail-501123.html

到了这里,关于MFC第八天 基本数据类型介绍的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Leetcode刷题第八天-回溯

    22:括号生成 链接:22. 括号生成 - 力扣(LeetCode) 括号是一对,所以每一次递归结束条件是字符串长度=2*n 有效括号判断:\\\'(\\\'个数==\\\')\\\'个数时,当前必须是\\\'(\\\',\\\'(\\\'个数==n时,必须是\\\')\\\',其他情况当前位置遍历两边,既可以是\\\'(\\\'又可以是\\\')\\\' generateParenthesis 89:格雷编码 链接:89.

    2024年02月19日
    浏览(25)
  • 每日后端面试5题 第八天

    1.UDP无连接,速度快,安全性低,适合高速传输、实时广播通信等。 2.TCP面向连接,速度慢,安全性高,适合传输质量要求高、大文件等的传输,比如邮件发送等。 (还有:TCP只能是一对一的,UDP支持一对一、一对多、多对一) (还有:TCP首部开销有20个字节;UDP分组首部开

    2024年02月11日
    浏览(30)
  • 3-MySQL基本数据类型介绍

    数据类型的介绍: 数据类型(data_type)是指系统中所允许的数据的类型。数据库中的每个列都应有适当的数据类型,用于限制或允许该列中存储的数据。例如,列中存储的为数字,则相应的数据类型应该为数值类型。 如果使用错误的数据类型可能会严重影响应用程序的功能

    2024年02月08日
    浏览(27)
  • C语言百日刷题第八天

    打印7层杨辉三角形 图案如下: 这个题我再前几天的刷题中也写过,但是很多人私信说上次写的太简陋了,那我这次就写完整。 通过图,可以看出。无论它是多少层的杨辉三角,它的前两层都是1,所以,无论我们会不会,都可以先把前两层搞定一下。其次,我们可以看出从第

    2024年01月22日
    浏览(26)
  • Java实训第八天——2023.6.14

    官方文档:https://v2.cn.vuejs.org/v2/guide/index.html 1.将vue.min.js复制到js中: 2.在demo01.html中,引入js 3.将官方文档复制到demo01.html中 demo01.html代码如下: 4.出现如下结果,则环境搭建成功! html代码如下: 运行结果: html代码如下: 运行结果如下: 运行结果: 代码如下: 初始页面:

    2024年02月09日
    浏览(34)
  • Java重修第八天—枚举和泛型

    通过学习本篇文章可以掌握如下知识 1、枚举 2、泛型 枚举是一种 特殊类 枚举类的格式: 为什么说枚举是一个特殊类,定义一个简单的枚举如下: 将其编译成class文件后,使用IDEA打开, 结果如下 :可以看出枚举类A有私有构造器。 枚举类的第一行只能罗列一些名称, 这些

    2024年01月17日
    浏览(28)
  • C语言属刷题训练【第八天】

    A: ‘a’ ‘b’ B: ab\\0c\\0 C: ab c D: ab 字符串的结束标志是’\\0’,而’\\0’的ASCII值是0,而c[2]被初始化为0,就相当于是’\\0’,故字符串打印的内容只有\\\"ab\\\"。 🧥知识点:字符串的结束标志 A: a[2][0] B: a[2][3] C: a[0][3] D: a[12][1] 本题主要考虑数组越界访问的情况,二维数组的行和列

    2024年02月13日
    浏览(26)
  • redis 的基本介绍以及 五种 数据类型

    一句话:redis 是一个开源的、使用C语言编写的、支持网络交互,基于内存也可持久化的 key-value (非关系型)数据库 redis作者博客,有兴趣的小伙伴可以去逛一逛:http://github.com/antirez 关于key 的命名,不要太长也不要太短,Key太长越占资源,太短可读性又很差,key 之间的单词

    2024年02月11日
    浏览(35)
  • C语言中基本数据类型-char介绍

    char是c语言中最基本的数据类型之一,叫字符型,在内存中占用一个字节的空间,可以用于存放单个字符,也可以用于存放整数,char可以分为有符号和无符号两种类型,下面对着两种类型分别进行介绍: 有符号字符型变量使用char来定义,比如char b,b作为有符号字符型变量,

    2024年02月06日
    浏览(26)
  • 嵌入式培训机构四个月实训课程笔记(完整版)-Linux系统编程第八天-Linux sqlite3数据库(物联技术666)

       更多配套资料CSDN地址:点赞+关注,功德无量。更多配套资料,欢迎私信。 物联技术666_嵌入式C语言开发,嵌入式硬件,嵌入式培训笔记-CSDN博客 物联技术666擅长嵌入式C语言开发,嵌入式硬件,嵌入式培训笔记,等方面的知识,物联技术666关注机器学习,arm开发,物联网,嵌入式硬件

    2024年01月25日
    浏览(48)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包