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

news/2024/10/18 8:34:37/

文章目录

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

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


CRect类的封装

#pragma onceclass CRectXq :public RECT
{
public:CRectXq(){(RECT&)*this = RECT();//memset(this, 0, sizeof(RECT));}// from left, top, right, and bottomCRectXq(int l, int t, int r, int b)//:left(l)//只限于本类,基类的数据不能用{left = l;top = t;right = r;bottom = b;}// copy constructorCRectXq(const RECT& src){(RECT&)*this = src;//memcpy(this, &src, sizeof(RECT));}// from a pointer to another rectCRectXq(_In_ LPCRECT p){(RECT&)*this = (RECT&)*p;//memcpy(this, p, sizeof(RECT));}// from a point and sizeCRectXq(POINT point, SIZE size){left = point.x;top = point.y;right = point.x + size.cx;bottom = point.y + size.cy;}// from two pointsCRectXq(POINT topLeft, POINT bottomRight){left = topLeft.x;top = topLeft.y;right = bottomRight.x;bottom = bottomRight.y;}// Attributes (in addition to RECT members)// retrieves the widthint Width() const{return right - left;}// returns the heightint Height() const{return bottom - top;}// returns the sizeCSize Size() const{return CSize(right - left, bottom - top);}// reference to the top-left pointCPoint& TopLeft(){return (CPoint&)*this;//return *(CPoint*)this;}// reference to the bottom-right pointCPoint& BottomRight(){return *((CPoint*)this + 1);}// const reference to the top-left pointconst CPoint& TopLeft() const{return (CPoint&)*this;//return *(CPoint*)this;}// const reference to the bottom-right pointconst CPoint& BottomRight() const{return *((CPoint*)this + 1);}// the geometric center point of the rectangleCPoint CenterPoint() const{return { (left + right) / 2,(top + bottom) / 2 };}// swap the left and rightvoid 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 areaBOOL IsRectEmpty() const throw(); //高或者宽为0,left与right相等或top和bottom相等// returns TRUE if rectangle is at (0,0) and has no areaBOOL IsRectNull() const throw();//4个数值是0代表空矩形// returns TRUE if point is within rectangleBOOL 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 bottomvoid 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 rectanglevoid SetRectEmpty() throw();// copy from another rectanglevoid CopyRect(_In_ LPCRECT lpSrcRect) throw();// TRUE if exactly the same as another rectangleBOOL 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 leftvoid 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, '-');	//查找 - 如果没有返回-1if (!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.ppmy.cn/news/581077.html

相关文章

配置鼠标右键菜单功能 :一键csv转excel

配置右键菜单功能 &#xff1a;一键csv转excel 无需点开文件&#xff0c;双击即可以生成新的excel文件 步骤&#xff1a; 1、配置Python&#xff0c;安装依赖库 pip install openpyxl pip install pandas2、创建Python文件 csv_to_excel.py # -*- coding:utf-8 -*- impor…

【Java】JVM学习(三)

JVM的整体内存结构 本地方法栈 本地方法栈跟 Java 虚拟机栈的功能类似&#xff0c;Java 虚拟机栈用于管理 Java 函数的调用&#xff0c;而本地方法栈则用于管理本地方法的调用。但本地方法并不是用 Java 实现的&#xff0c;而是由 C 语言实现的(比如Object.hashcode方法)。 …

【面试官版】融合滤波算法+数据结构+C++面试题汇总

C部分 什么时候需要写虚函数、什么时候需要写纯虚函数&#xff1f; 只继承接口为纯虚函数 强调覆盖父类重写&#xff0c;或者父类也需要实现一定的功能&#xff0c;为虚函数指针传参和引用传参区别&#xff1f; 引用传参本质上是传递原参数地址&#xff0c;指针传参本质还是值…

使用Redission自定义注解实现分布式锁(声明式)

1.主要依赖 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.redisson</groupId><artifactId>redisso…

如何在h5页面里只嵌入腾讯视频播放框

例如&#xff1a;https://v.qq.com/x/page/k00290g0i00.html 把“k00290g0i00”加入下面一段代码 <iframe class"video_iframe" style"z-index:1;" src"https://v.qq.com/iframe/player.html?vidk00290g0i00&amp;width500&amp;height375…

c++练习4) 编写程序把华氏温度转换为摄氏温度,公式为 C=5/9*(F-32)

#include<iostream> using namespace std; int main(){float F,C;cout<<"请输入华氏温度&#xff1a; ";cin>>F;C(F-32)*5/9;cout<<endl<< "对应的摄氏温度为&#xff1a; "<<C;return 0; }

西门子S7-1200系列PLC输入/输出接线

西门子S7-1200是一款紧凑型、模块化的PLC&#xff0c;可完成简单逻辑控制、高级逻辑控制、HMI 和网络通信等任务。下面分享S7-1200系列PLC输入/输出接线图给大家。 CPU 1211C 接线图 CPU 1211C AC/DC/继电器 (6ES7 211-1BE40-0XB0) ① 24 VDC 传感器电源 ② 对于漏型输入将负…

终于知道韩熙美白祛斑霜

我18岁那年&#xff0c;发现自己脸上突然多了几个痘痘&#xff0c;而且长出零星的斑点&#xff0c;可那时不以为然。大学毕业后&#xff0c;参加工作&#xff0c;整天与电脑打交道&#xff0c;一忙就到昏天暗地&#xff0c;往往是日熬夜熬几个星期。那个斑就像被施了什么肥料一…