// DateTimeEidt.cpp : implementation file
//
#include "stdafx.h"
#include "DateTimeEidt.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDateTimeEidt
CDateTimeEidt::CDateTimeEidt()
{
m_bInit = FALSE;
m_nPos = 0;
m_bEnable = TRUE;
}
CDateTimeEidt::~CDateTimeEidt()
{
}
BEGIN_MESSAGE_MAP(CDateTimeEidt, CEdit)
//{{AFX_MSG_MAP(CDateTimeEidt)
ON_WM_KEYDOWN()
ON_WM_CHAR()
ON_WM_SIZE()
ON_WM_LBUTTONUP()
ON_WM_MOUSEWHEEL()
ON_WM_CTLCOLOR_REFLECT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDateTimeEidt message handlers
BOOL CDateTimeEidt::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID)
{
return CWnd::Create("EDIT", NULL, dwStyle, rect, pParentWnd, nID, NULL);
}
void CDateTimeEidt::PreSubclassWindow()
{
ModifyStyle(0, WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_NOHIDESEL | ES_MULTILINE | ES_READONLY);//归整样式
SetWindowText(" - - : : ");
//垂直居中,必须有多行样式
CRect rc;
GetClientRect(&rc);
CDC* pDC = GetDC();
TEXTMETRIC tm;
pDC->GetTextMetrics(&tm);
RECT rect = {0, rc.Height() - tm.tmHeight, rc.Width(), rc.Height()};
SetRectNP(&rect);
CEdit::PreSubclassWindow();
}
void CDateTimeEidt::OnSize(UINT nType, int cx, int cy)
{
CEdit::OnSize(nType, cx, cy);
if (::IsWindow(m_hWnd))
{//垂直居中
CDC* pDC = GetDC();
TEXTMETRIC tm;
pDC->GetTextMetrics(&tm);
RECT rect = {0, cy - tm.tmHeight, cx, cy};
SetRectNP(&rect);
}
}
HBRUSH CDateTimeEidt::CtlColor(CDC* pDC, UINT nCtlColor)
{
int nIndex = m_bEnable ? COLOR_WINDOW : COLOR_BTNFACE;
HBRUSH hbr = ::GetSysColorBrush(nIndex);
pDC->SetBkMode(TRANSPARENT);
return hbr;
}
void CDateTimeEidt::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if (!m_bEnable)
return;
//yyyy-mm-dd hh:mm:ss
//0123456789012345678
int nStartChar, nEndChar;
switch (nChar)
{
case VK_LEFT:
GetSel(nStartChar, nEndChar);
if (nStartChar > DTS_YEAR)
{
m_nPos = SmartSel(nStartChar - 1);
}
return;
case VK_RIGHT:
GetSel(nStartChar, nEndChar);
if (nEndChar < DTS_SECOND)
{
m_nPos = SmartSel(nEndChar + 1);
}
return;
case VK_UP:
SendMessage(WM_MOUSEWHEEL, MAKEWPARAM(0, WHEEL_DELTA), 0);
return;
case VK_DOWN:
SendMessage(WM_MOUSEWHEEL, MAKEWPARAM(0, - WHEEL_DELTA), 0);
return;
}
CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CDateTimeEidt::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if (!m_bEnable)
return;
if (nChar >= '0' && nChar <='9')
{
//yyyy-mm-dd hh:mm:ss
//0123456789012345678
int nStartChar, nEndChar;
GetSel(nStartChar, nEndChar);
char str[20];
GetWindowText(str, sizeof(str));
memcpy(str + m_nPos, &nChar, 1);
SetWindowText(str);
SetSel(nStartChar, nEndChar);
switch (m_nPos)
{
case 0:
case 1:
case 2:
case 3:
m_nPos = m_nPos < 3 ? ++ m_nPos : DTS_YEAR;
break;
case 5:
case 6:
m_nPos = m_nPos < 6 ? ++ m_nPos : DTS_MONTH;
break;
case 8:
case 9:
m_nPos = m_nPos < 9 ? ++ m_nPos : DTS_DAY;
break;
case 11:
case 12:
m_nPos = m_nPos < 12 ? ++ m_nPos : DTS_HOUR;
break;
case 14:
case 15:
m_nPos = m_nPos < 15 ? ++ m_nPos : DTS_MINUTE;
break;
case 17:
case 18:
m_nPos = m_nPos < 18 ? ++ m_nPos : DTS_SECOND;
break;
}
return;
}
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
BOOL CDateTimeEidt::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
if (!m_bEnable)
return FALSE;
COleDateTime dateSrc;
if (!GetDateTime(dateSrc))
return FALSE;
int nStartChar, nEndChar;
CString str, s;
int n;
GetSel(nStartChar, nEndChar);
GetWindowText(str);
s = str.Mid(nStartChar, nEndChar - nStartChar);
sscanf(s, "%d", &n);
zDelta > 0 ? n -- : n ++;
if (n < 0)
return FALSE;
int nYear = dateSrc.GetYear();
int nMonth = dateSrc.GetMonth();
int nDay = dateSrc.GetDay();
int nHour = dateSrc.GetHour();
int nMin = dateSrc.GetMinute();
int nSec = dateSrc.GetSecond();
switch (nStartChar)
{
case DTS_YEAR:
nYear = n;
break;
case DTS_MONTH:
nMonth = n;
break;
case DTS_DAY:
nDay = n;
break;
case DTS_HOUR:
nHour = n;
break;
case DTS_MINUTE:
nMin = n;
break;
case DTS_SECOND:
nSec = n;
break;
}
dateSrc.SetDateTime(nYear, nMonth, nDay, nHour, nMin, nSec);
SetDateTime(dateSrc);
SetSel(nStartChar, nEndChar);
return TRUE;
}
void CDateTimeEidt::OnLButtonUp(UINT nFlags, CPoint point)
{
if (m_bEnable)
{
if (!m_bInit)
{//第一次左键点击
m_bInit = SetDateTime(COleDateTime::GetCurrentTime());
}
m_nPos = SmartSel(CharFromPos(point));
}
CEdit::OnLButtonUp(nFlags, point);
}
int CDateTimeEidt::SmartSel(int nPos)
{
//yyyy-mm-dd hh:mm:ss
//0123456789012345678
int nRes = 0;//返回选区的初始位置
switch (nPos)
{
case 0:
case 1:
case 2:
case 3:
case 4:
SetSel(DTS_YEAR, DTS_YEAR + 4);
nRes = DTS_YEAR;
break;
case 5:
case 6:
case 7:
SetSel(DTS_MONTH, DTS_MONTH + 2);
nRes = DTS_MONTH;
break;
case 8:
case 9:
case 10:
SetSel(DTS_DAY, DTS_DAY + 2);
nRes = DTS_DAY;
break;
case 11:
case 12:
case 13:
SetSel(DTS_HOUR, DTS_HOUR + 2);
nRes = DTS_HOUR;
break;
case 14:
case 15:
case 16:
SetSel(DTS_MINUTE, DTS_MINUTE + 2);
nRes = DTS_MINUTE;
break;
case 17:
case 18:
case 19:
SetSel(DTS_SECOND, DTS_SECOND + 2);
nRes = DTS_SECOND;
break;
default:
nRes = DTS_SECOND;
}
return nRes;
}
BOOL CDateTimeEidt::SetDateTime(const COleDateTime& dateSrc)
{
BOOL bRes = FALSE;
if (dateSrc.GetStatus() == COleDateTime::valid)
{
CString str = dateSrc.Format("%Y-%m-%d %H:%M:%S");
SetWindowText(str);
bRes = TRUE;
}
return bRes;
}
BOOL CDateTimeEidt::GetDateTime(COleDateTime& dateSrc)
{
CString str;
GetWindowText(str);
return dateSrc.ParseDateTime(str);
}
BOOL CDateTimeEidt::EnableWindow(BOOL bEnable)
{
BOOL bRes = FALSE;
if (m_bEnable != bEnable)
{
m_bEnable = bEnable;
Invalidate();
bRes = TRUE;
}
return bRes;
}
没有合适的资源?快使用搜索试试~ 我知道了~
日期时间类CDateTimeEidt
共2个文件
h:1个
cpp:1个
5星 · 超过95%的资源 需积分: 12 253 下载量 48 浏览量
2012-01-08
21:01:03
上传
评论 9
收藏 3KB RAR 举报
温馨提示
一个智能化的日期时间类CDateTimeEidt 这个类虽然是从CEdit类继承,但做了格式化规范,用户输入感觉到很方便,可以用左右键来移动选区,用上下键或滚轮来改变数字。不会得到错误格式的日期时间。 用GetDateTime(COleDateTime& dateSrc)来获取编辑框中的内容 用SetDateTime(COleDateTime& dateSrc)来设置编辑框中的内容 可以EnableWindow()来允许或
资源推荐
资源详情
资源评论
收起资源包目录
DateTimeEidt.rar (2个子文件)
DateTimeEidt.h 2KB
DateTimeEidt.cpp 6KB
共 2 条
- 1
firefly3233
- 粉丝: 11
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页