文章目录
- 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;