MFC第九天 CRect类的封装和窗口坐标系转换及获取窗口ID 以及对CTime类与COleDateTime类简介

news2025/2/23 17:12:54

文章目录

  • CRect类的封装、窗口坐标系转换及获取窗口ID 、CTime类与COleDateTime类简介
  • CRect类的封装
  • 窗口坐标系转换及获取窗口ID
  • CTime类与COleDateTime类简介
    • 关于设置日期控件上的时间问题

CRect类的封装、窗口坐标系转换及获取窗口ID 、CTime类与COleDateTime类简介


CRect类的封装

#pragma once

class CRectXq :public RECT
{
public:
	CRectXq()
	{
		(RECT&)*this = RECT();
		//memset(this, 0, sizeof(RECT));
	}
	// from left, top, right, and bottom
	CRectXq(int l, int t, int r, int b)//:left(l)//只限于本类,基类的数据不能用
	{
		left = l;
		top = t;
		right = r;
		bottom = b;
	}

	// copy constructor
	CRectXq(const RECT& src)
	{
		(RECT&)*this = src;
		//memcpy(this, &src, sizeof(RECT));
	}

	// from a pointer to another rect
	CRectXq(_In_ LPCRECT p)
	{
		(RECT&)*this = (RECT&)*p;
		//memcpy(this, p, sizeof(RECT));

	}
	// from a point and size
	CRectXq(POINT point, SIZE size)
	{
		left = point.x;
		top = point.y;
		right = point.x + size.cx;
		bottom = point.y + size.cy;
	}
	// from two points
	CRectXq(POINT topLeft, POINT bottomRight)
	{
		left = topLeft.x;
		top = topLeft.y;
		right = bottomRight.x;
		bottom = bottomRight.y;
	}

	// Attributes (in addition to RECT members)

		// retrieves the width
	int Width() const
	{
		return right - left;
	}
	// returns the height
	int Height() const
	{
		return bottom - top;
	}
	// returns the size
	CSize Size() const
	{
		return CSize(right - left, bottom - top);
	}
	// reference to the top-left point
	CPoint& TopLeft()
	{
		return (CPoint&)*this;//return *(CPoint*)this;
	}
	// reference to the bottom-right point
	CPoint& BottomRight()
	{
		return *((CPoint*)this + 1);
	}
	// const reference to the top-left point
	const CPoint& TopLeft() const
	{
		return (CPoint&)*this;//return *(CPoint*)this;
	}
	// const reference to the bottom-right point
	const CPoint& BottomRight() const
	{
		return *((CPoint*)this + 1);
	}
	// the geometric center point of the rectangle
	CPoint CenterPoint() const
	{
		return { (left + right) / 2,(top + bottom) / 2 };
	}
	// swap the left and right
	void SwapLeftRight()
	{
		left = left ^ right;
		right = left ^ right;
		left = left ^ right;
	}
	static void WINAPI SwapLeftRight(_Inout_ LPRECT lpRect) throw();

	// convert between CRect and LPRECT/LPCRECT (no need for &)
	operator LPRECT()
	{
		return this;
	}
	operator LPCRECT() const throw()
	{
		return this;
	}

	// returns TRUE if rectangle has no area
	BOOL IsRectEmpty() const throw(); //高或者宽为0,left与right相等或top和bottom相等
	// returns TRUE if rectangle is at (0,0) and has no area
	BOOL IsRectNull() const throw();//4个数值是0代表空矩形
	// returns TRUE if point is within rectangle
	BOOL PtInRect(_In_ POINT point) const  //某个点是否在矩形区域内
	{
		return point.x >= left && point.x <= right && point.y >= top && point.y <= bottom;
	}

	// Operations

		// set rectangle from left, top, right, and bottom
	void SetRect(int x1, int y1, int x2, int y2)
	{
		left = x1;
		top = y1;
		right = x2;
		bottom = y2;
	}
	void SetRect(
		_In_ POINT topLeft,
		_In_ POINT bottomRight) throw();
	// empty the rectangle
	void SetRectEmpty() throw();
	// copy from another rectangle
	void CopyRect(_In_ LPCRECT lpSrcRect) throw();
	// TRUE if exactly the same as another rectangle
	BOOL EqualRect(_In_ LPCRECT lpRect) const
	{
		return !memcmp(this, lpRect, sizeof(RECT));
		//return this ->left == lpRect->left && 
	}

	// 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(int x, int y)//膨胀
	{
		left -= x;
		right += x;
		top -= y;
		bottom += y;
	}
	// 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(SIZE size)//膨胀
	{
		InflateRect(size.cx, size.cy);
	}
	// 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(LPCRECT lpRect)
	{
		InflateRect(lpRect->left, lpRect->top, lpRect->right, lpRect->bottom);
	}
	void InflateRect(int l, int t, int r, int b)//膨胀
	{
		left -= l;
		right += r;
		top -= t;
		bottom += b;
	}

	// 压缩
	void DeflateRect( int x,  int y)
	{
		left += x;
		right -= x;
		top += y;
		bottom -= y;
	}
	void DeflateRect(_In_ SIZE size)
	{
		DeflateRect(size.cx, size.cy);
	}
	void DeflateRect(_In_ LPCRECT lpRect)
	{
		DeflateRect(lpRect->left, lpRect->top, lpRect->right, lpRect->bottom);
	}
	void DeflateRect(int l, int t, int r, int b)
	{
		left += l;
		right -= r;
		top += t;
		bottom -= b;
	}

	// translate the rectangle by moving its top and left
	void OffsetRect(int x, int y) //偏移:移动过程,高宽不变!
	{
		left += x;
		right += x;
		top += y;
		bottom += y;
	}
	void OffsetRect(SIZE size)
	{
		OffsetRect(size.cx, size.cy);
	}
	void OffsetRect(POINT point)
	{
		OffsetRect(point.x, point.y);
	}
	void NormalizeRect()//如果right<left或者bottom<top
	{

		int nTemp;
		if (left > right)
		{
			nTemp = left;
			left = right;
			right = nTemp;
		}
		if (top > bottom)
		{
			nTemp = top;
			top = bottom;
			bottom = nTemp;
		}
	}

};

窗口坐标系转换及获取窗口ID

对应的对话框如下:
在这里插入图片描述

/*
CString str;
pWnd->GetWindowText(str);
TRACE(str + _T("\r\n")); 当光标放在控件上时,获取其文字

HWND hButton = GetDlgItem(hWnd,IDC_BUTTON1);//获取按钮控件的句柄
UINT buttonID = GetDlgCtr1ID(hButton);//获取按钮控件的窗口ID

	//if(this->GetDlgItem(IDOK)==pWnd)
	//if(this->GetDlgItem(IDOK)->m_hWnd ==pWnd->m_hWnd) //IDOK 的句柄和pWnd和句柄 一致  甚至两者的指针是一致的也是可以的
	if (pWnd->GetDlgCtrlID()==IDOK)
*/

BOOL CBossDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) //光标切换消息
{
	if (pWnd->GetDlgCtrlID()==IDOK)
	{
		CRect rect,rc;
		GetClientRect(rect);	 //大窗口的客户区
		pWnd->GetWindowRect(rc); //小窗口的外壳 确定按钮	获取的是大坐标 以内部客户区为准的话会缩小
		ScreenToClient(rc);			//需要的是小坐标进行转换,并且与客户区的进行比较
		if (rc.right >= rect.right) 
		{
			rc.OffsetRect(0 - rc.left, 0);	  //回到界内
		}
		else
		{
			rc.OffsetRect(rc.Width(), 0);  //范围内
		}
		pWnd->MoveWindow(rc);
	}
	return CDialogEx::OnSetCursor(pWnd, nHitTest, message);
}

/*
方法1
CString str;
	if (pWnd->GetDlgCtrlID()==IDOK)
	{
		CRect rect,rc;
		pWnd->GetWindowRect(rect);
		this->GetWindowRect(rc);

		if (rect.right>=rc.right)
		{
			rect.OffsetRect(rc.left - rect.left,0);
			this->ScreenToClient(rect); //有个this 对象 代表是谁的客户区  主窗口的
		}
		else {
			this->ScreenToClient(rect);
			rect.OffsetRect(rect.Width(), 0);
		}
		pWnd->MoveWindow(rect);
	}

	return CDialogEx::OnSetCursor(pWnd, nHitTest, message);
*/

CTime类与COleDateTime类简介

CTime类和COleDateTime类都是在MFC框架下用于处理日期和时间的类。它们提供了各种方法和操作符来处理日期和时间的计算、比较和格式化。CTime类是基于系统时间的表示,而COleDateTime类是基于OLE Automation规范的日期时间表示方式。两者都可以互相转换。

CTime类还支持与COleDateTime类之间的转换,可以通过CTime::GetTime()方法将CTime对象转换为COleDateTime对象。
在这里插入图片描述

class CTimeXq
{
public:
	static CTimeXqGetCurrentTime()
	{
		return time(NULL);
	}
	static BOOL IsValidFILETIME(_In_ const FILETIME& ft);

	CTimeXq():m_time(0)
	{
	}
	CTimeXq(__time_t time);
	CTimeXq(
		_In_ int nYear,
		_In_ int nMonth,
		_In_ int nDay,
		_In_ int nHour,
		_In_ int nMin,
		_In_ int nSec,
		_In_ int nDST = -1);
	
	CTimeXq& operator=(_In_ __time_t time);

	bool operator==(_In_ CTimeLx time) const;
	bool operator!=(_In_ CTimeLx time) const;
	bool operator<(_In_ CTimeLx time) const;
	bool operator>(_In_ CTimeLx time) const;
	bool operator<=(_In_ CTimeLx time) const;
	bool operator>=(_In_ CTimeLx time) const;

	__time_t GetTime() const;

	int GetYear() const;
	int GetMonth() const;
	int GetDay() const;
	int GetHour() const;
	int GetMinute() const;
	int GetSecond() const;
	int GetDayOfWeek() const;
};

关于设置日期控件上的时间问题

推荐使用方法1
方法1:

	auto pDateCtrl = (CDateTimeCtrl*)GetDlgItem(IDC_DATETIME);
	COleDateTime time;	
	time.ParseDateTime(m_sDate); //将文字解析成时间
	pDateCtrl->SetTime(time);

方法2:
ConvertTime 将sDate中的时间分段取出

static CTime ConvertTime(CString sDate){
		_bstr_t bstr = sDate;
		const char* p=bstr;
		int nYear = atoi(p);

		p = strchr(p, '-');	//查找 - 如果没有返回-1
		if (!p)
			return -1;
		int nMonth = atoi(++p);
		p = strchr(p, '-');	 
		if (!p)
			return -1;
		int nDay = atoi(++p);
		return CTime(nYear, nMonth, nDay, 0, 0, 0);
	}

	SetDlgItemText(IDC_DATETIME	, m_sDate);
	auto pDateCtrl = (CDateTimeCtrl*)GetDlgItem(IDC_DATETIME);
	CTime time = CApp::ConvertTime(m_sDate);	
	//CTime time()	time()加上()误认为成函数声明了	返回值CTime	函数名time ()参数列表
	pDateCtrl->SetTime(&time);
	return TRUE;   

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/685984.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

300黑客共闯沙盒|赛宁数字化靶场助力第十六届全国大学生信息安全竞赛

6月24日&#xff0c;由中央网信办网络安全协调局指导、教育部高等学校网络空间安全专业教学指导委员会主办、福州大学承办的第十六届全国大学生信息安全竞赛—创新实践能力赛华东南分区选拔赛&#xff08;简称“分区赛”&#xff09;圆满结束。赛宁网安基于数字化靶场打造的“赛…

3、数仓之采集工具MaxWell(MaxWell简介、MaxWell原理、MaxWell部署、MaxWell使用)

1、Maxw简介 1.1 MaxWell概述 Maxwell 是由美国Zendesk公司开源&#xff0c;用Java编写的MySQL变更数据抓取软件。它会实时监控Mysql数据库的数据变更操作&#xff08;包括insert、update、delete&#xff09;&#xff0c;并将变更数据以 JSON 格式发送给 Kafka、Kinesi等流数…

100页干货!一文看懂10+行业领域发展趋势

导读&#xff1a; 当前全球经济环境及疫情的变化&#xff0c;使得中国各行业的发展呈现向好趋势&#xff0c;但市场仍充满诸多不确定性。 在今年伊始&#xff0c;罗兰贝格重磅发布了《“预见2023”中国行业趋势报告》&#xff08;文中简称《报告》&#xff09;&#xff0c;囊…

学生台灯怎么选对眼睛好的?看完再买不踩坑!

我们都知道&#xff0c;对眼睛最好的光就是自然的太阳光&#xff0c;但并不是每时每刻都能享受到太阳光般的光源&#xff0c;所以现在有很多台灯的出现&#xff0c;而护眼台灯是对眼睛最好的&#xff0c;不过护眼台灯的挑选也有一些需要注意的细节&#xff01; 1、全光谱&#…

Lancet Microbe -- 新冠轻症者可能是“超级传播者”

一项对有意感染SARS-CoV-2的人群的研究提供了关于病毒传播的丰富见解&#xff0c;比如&#xff0c;某些特定人群是“超级传播者”&#xff0c;他们向空气中释放的病毒远比其他人多。 该论文描述了一项具有争议的“挑战性研究”的数据&#xff0c;在这项研究中&#xff0c;科学家…

Sangfor华东天勇战队:AspectJWeaver反序列化利用链

依赖&#xff1a; <dependencies><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.2</version></dependency> </dependencies>测试类&#xff1a; import java…

基于Matlab实现SVM算法的手写字体识别(附上完整仿真源码+数据 )

手写字体识别是一个重要的人工智能应用领域。在本文中&#xff0c;我们将展示如何使用MATLAB实现手写数字的识别。 首先&#xff0c;我们需要准备一个手写数字数据集。在本文中&#xff0c;我们将使用MNIST数据集&#xff0c;这是一个广泛使用的手写数字数据集&#xff0c;包含…

界面组件DevExpress WinForm v23.1新版亮点 - 皮肤矢量图标全新升级

DevExpress WinForms拥有180组件和UI库&#xff0c;能为Windows Forms平台创建具有影响力的业务解决方案。DevExpress WinForms能完美构建流畅、美观且易于使用的应用程序&#xff0c;无论是Office风格的界面&#xff0c;还是分析处理大批量的业务数据&#xff0c;它都能轻松胜…

【机器学习】正则化对过拟合和欠拟合的影响

模型过拟合和欠拟合的图像特征 偏差大表示欠拟合&#xff0c;而方差大表示过拟合&#xff0c;我们这一节再深入探讨下过拟合和欠拟合问题。一个经典的图如下&#xff1a; 其中d1为欠拟合&#xff0c;d4为过拟合&#xff0c;而d2则刚刚好。回顾下刚刚说的使用训练集和交叉验证…

JavaScript知识点DOM 模型详细讲解

DOM 模型 DOM 全称是 Document Object Model 文档对象模型 大白话&#xff0c;就是把文档中的标签&#xff0c;属性&#xff0c;文本&#xff0c;转换成为对象来管理。 Document 对象 Document对象的理解&#xff1a; ​ 第一点&#xff1a;Document 它管理了所有的 HTML 文…

三次登录验证和验证码功能实现

三次登录验证和验证码功能实现 最近手头上的事忙的差不多了&#xff0c;就想着自己写写小demo玩一下&#xff0c;结果突然看到我们旧系统的登录好像有点拉胯&#xff0c;然后就自己写了个小demo&#xff0c;指不定哪天就用上了呢 一、pom文件 首先当然是pom文件啦&#xff0…

【JS】1724- 重学 JavaScript API - Drag and Drop API

❝ 前期回顾&#xff1a; 1. Page Visibility API 2. Broadcast Channel API 3. Beacon API 4. Resize Observer API 5. Clipboard API 6. Fetch API 7. Performance API 8. WebStorage API 9. WebSockets API 10. Fullscreen API 11. Geolocation API ❞ &#x1f3dd; 1. 快速…

ThinkPHP6.0 数据迁移工具 migration 入门使用教程

文章目录 安装数据库迁移工具创建迁移文件执行迁移回滚参考资料 开始前需要做好的准备工作&#xff1a; 搭建好 PHP 开发环境&#xff08;推荐 phpstudy&#xff0c;PHP>7.2.5&#xff0c;MySql5.7.x&#xff09;。安装好 ThinkPHP6.0&#xff0c;并做配置可正常连接到 MySq…

docker安装nginx,发布部署vue项目

场景 前后端项目&#xff0c;实现前后端简单部署到服务器。前端vue&#xff0c;后端springboot。服务器ubuntu&#xff08;18.04&#xff09;<linux系统同理>. 后端通过(nohup java -jar xxx.jar &) 指令简单部署。该文主要说明部署前端vue项目。 部署vue需要安装ng…

一文看懂51单片机和stm32区别,怎么用怎么学怎么选

一文看懂51单片机和stm32区别&#xff0c;怎么用怎么学怎么选 对于初学单片机的童鞋而言&#xff0c;开始会有这样的疑问&#xff1f;到底选哪个怎么选呢&#xff1f; 1、工业控制51优于stm&#xff1f; 2、没区别,51就是个入门级,不过也有贵的,我用的就是51,用的还可以&…

PG系列4:linux下编译安装PG15

文章目录 一. 源码安装1.1 下载并解压1.2 安装依赖包1.3 开始编译安装1.4 创建用户1.5 创建目录及修改权限1.6 设置环境变量1.7 初始化数据库1.8 启动和关闭数据库 二. 验证2.1 查看数据库后台进程2.2 验证和登陆数据库2.3 查看数据库版本2.4 查看数据库运行状态2.5 修改白名单…

Sangfor华东天勇战队:h2数据库console命令执行( CVE-2021-42392 漏洞)

漏洞版本 1.1.100 < H2 Console < 2.0.204 漏洞复现 此处复现版本1.4.197 启动项目如下 在Driver Class中输入javax.naming.InitialContext 在JDBCURL中输入jndi注入恶意链接 生成链接命令&#xff1a; java -jar JNDI-Injection-Exploit-1.0-SNAPSHOT-all.jar -C …

CCD与CMOS

#1, 相机内部结构 https://zhuanlan.zhihu.com/p/158502818 #2&#xff0c;

大数据从0到1的完美落地之Hive分区

分区简介 为什么分区 Hive的Select查询时&#xff0c;一般会扫描整个表内容。随着系统运行的时间越来越长&#xff0c;表的数据量越来越大&#xff0c;而hive查询做全表扫描&#xff0c;会消耗很多时间&#xff0c;降低效率。而有时候&#xff0c;我们需求的数据只需要扫描表…

java面试高频面试题

文章目录 面向对象 什么是面向对象&#xff1f;封装继承多态 和equals比较hashCode与equals重载和重写的区别Final类加载器spring是什么AOP的理解谈谈你对IOC的理解零拷贝RocketMQ 架构设计RocketMq 事务消息原理RockeMq顺序消息消费原理简述RockerMQ持久化机制RocketMQ如何保…