123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521 |
- #include "stdafx.h"
- #include "DateEdit.h"
- #include "LYFZIPManage.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // COleDateTime read /write
- /*
- COleDateTime ReadCOleDateTime(LPCSTR lpszData)
- {
- COleDateTime DateTime;
- DateTime.ParseDateTime(lpszData);
- return DateTime;
- }
- void FormatCOleDateTime(CString& strData, COleDateTime DateTime, int len)
- {
- strData = "";
- if (DateTime.m_dt == 0) return;
- if (len == 8)
- strData = DateTime.Format("%d/%m/%y");
- else if(len == 5) // added these two
- strData = DateTime.Format("%H:%M");
- else
- strData = DateTime.Format("%d/%m/%Y");
- }
- /////////////////////////////////////////////////////////////////////////////
- // DDX for mask control
- void AFXAPI DDX_OleDate(CDataExchange* pDX, int nIDC, CDateEdit& rControl, COleDateTime& Date)
- {
- DDX_Control(pDX, nIDC, (CWnd&)rControl);
- if (!pDX->m_bSaveAndValidate)
- rControl.SetDate(Date);
- else
- Date = rControl.GetDate();
- }
- */
- /////////////////////////////////////////////////////////////////////////////
- // CDateEdit
- IMPLEMENT_DYNAMIC(CDateEdit,CEdit)
- BEGIN_MESSAGE_MAP(CDateEdit, CEdit)
- //{{AFX_MSG_MAP(CDateEdit)
- ON_WM_CHAR()
- ON_WM_KEYDOWN()
- ON_CONTROL_REFLECT(EN_SETFOCUS, OnEnSetfocus)
- //}}AFX_MSG_MAP
- // ON_MESSAGE(WM_CUT, OnCut)
- // ON_MESSAGE(WM_PASTE, OnPaste)
- // ON_MESSAGE(WM_CLEAR, OnClear)
- END_MESSAGE_MAP()
- CDateEdit::CDateEdit()
- {
- Reset();
- SetClassType("DateEdit");
- }
- CDateEdit::~CDateEdit()
- {
- }
- /////////////////////////////////////////////////////////////////////////////
- // CDateEdit message handlers
- void CDateEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- CString str;
- try
- {
- GetWindowText(str);
- }
- catch(...)
- {
- }
- for (int i = 0; i<str.GetLength() && i<m_str.GetLength();i++)
- m_str.SetAt(i, str.GetAt(i));
- if(!m_bMaskKeyInProgress)
- if(!CheckChar(nChar)) return;
- if(m_bUseMask) //要使用掩码
- {
- if(isprint(nChar)) //是可打印字符
- {
- int startPos,endPos;
- GetSel(startPos,endPos);
- SetSel(startPos,endPos+1);
- Clear();
- }
- /* else if(nChar==VK_BACK)
- {
- int startPos,endPos;
- GetSel(startPos,endPos);
- if((startPos==endPos) && (startPos>=1) && (startPos<=m_str.GetLength()))
- {
- char c;
- if(m_strMaskLiteral != _T(""))
- c=m_strMaskLiteral[startPos-1];
- TRACE("m_strMaskLiteral=[%s](%s)\n",m_strMaskLiteral,m_str);
- //回退光标
- SendMessage(WM_KEYDOWN,VK_LEFT,0);
- if(m_strMaskLiteral != _T(""))
- {
- //恢复在该位置预设的字符
- SendChar(c);
- //再次退回
- SendMessage(WM_KEYDOWN,VK_LEFT,0);
- }
- }
- else //越界或者存在选择区域
- MessageBeep((UINT)-1);
- return;
- }*/
- }
-
- CEdit::OnChar(nChar, nRepCnt, nFlags);
- if(!m_bMaskKeyInProgress && m_bUseMask && m_strLiteral != _T(""))
- {
- int startPos,endPos;
- GetSel(startPos,endPos);
- //确保字符串的长度小于m_strLiteral的长度
- if(endPos<m_strLiteral.GetLength())
- {
- UINT c=m_strLiteral.GetAt(endPos);
- if(c!='_')
- SendChar(c);
- }
- }
- }
- void CDateEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- /* if(m_bUseMask)
- {
- switch(nChar)//忽略删除和插入键
- {
- case VK_DELETE:
- case VK_INSERT: return;
- }
- }*/
- CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
- }
- BOOL CDateEdit::CheckChar(UINT nChar)
- {
- UINT c;
- int nTime = 0;
- CString sText;
- //如果不使用掩码,则返回
- if(!m_bUseMask) return TRUE;
- //如果是控制字符,则返回
- if(!isprint(nChar)) return TRUE;
- //如果存在选择区域,则取消选择
- int startPos,endPos;
- GetSel(startPos,endPos);
- SetSel(-1,0);
- //重新选中原选择区域的第一个字符
- SetSel(startPos,startPos);
- GetSel(startPos,endPos);
- //确保字符串的长度不超过掩码的长度
- if(endPos>=m_strMask.GetLength())
- {
- MessageBeep((UINT)-1);
- return FALSE;
- }
- //检查在当前位置是否应存在文字
- c='_';
- if(m_strLiteral != _T("") &&
- m_strLiteral.GetLength()>endPos)
- c=m_strLiteral.GetAt(endPos);
- if(c!='_')
- {//发送默认值
- SendChar(c);
- GetSel(startPos,endPos);
- }
- //检查字符的有效性
- if(m_strValid.Find(nChar)!=-1) return TRUE;
- //检查当前位置的掩码
- c=m_strMask.GetAt(endPos);
- BOOL doit=TRUE;
- sText = m_str;
- sText.SetAt(endPos, nChar);
- if (nChar == VK_SPACE) return true; //空格
- switch(c)
- {
- case '0': //只能是数字
- {
- BOOL doit=TRUE;
- if(isdigit(nChar))
- {
- if(m_isDate || m_isDateTime) //是否按照日期格式输入
- {
- // sText.SetAt(endPos, nChar);
- if (!IsValid(COleDateTime(COleVariant(sText))))
- {
- if(endPos < 8)
- {//月、日设置为默认
- sText.SetAt(4, '-');
- sText.SetAt(5, '0');
- sText.SetAt(6, '1');
- sText.SetAt(7, '-');
- sText.SetAt(8, '0');
- sText.SetAt(9, '1');
- SetWindowText(sText);
- SetSel(endPos+1,endPos+1);
- }
- if(endPos >= 8)
- {//日设置为少30
- if (sText.GetAt(8) > '3')
- sText.SetAt(8, '0');
- sText.SetAt(9, '1');
- SetWindowText(sText);
- SetSel(endPos+1,endPos+1);
- }
- doit=FALSE;
- }
- /* if(endPos==8)
- {
- if(nChar>'3') //每月只能有三十几天
- doit=FALSE;
- }
- if(endPos==9)
- {
- if(m_str.GetAt(5)=='0' &&
- m_str.GetAt(6)=='2' &&
- m_str.GetAt(8)=='2')
- {
- if(nChar>'8')//2月不超过29天
- doit=FALSE;
- }
- else if(m_str.GetAt(5)=='0' &&
- m_str.GetAt(6)=='2' &&
- m_str.GetAt(8)>'2')
- {//2月不起30
- doit=FALSE;
- }
- else if(m_str.GetAt(8)=='3')
- {
- if(nChar>'1')//每月不超过31天
- doit=FALSE;
- }
- }
- if(endPos==5)
- {
- if(nChar>'1') //一年只能有12个月
- doit=FALSE;
- }
- if(endPos==6)
- {
- if(m_str.GetAt(5)=='1')
- {
- if(nChar>'2') //一年只能有12个月
- doit=FALSE;
- }
- }
- */
- }//结束日期检查
- if (m_isDateTime) nTime = 11;
- if (m_isTime || m_isDateTime) //按时间格式检查
- {
- //转换时间为HH0MM
- // for(int i = nTime; i < nTime+5; i++)
- // {
- // if (sText.GetAt(i) == TCHAR(' ') ||
- // sText.GetAt(i) == TCHAR(':'))
- // sText.SetAt(i, TCHAR('0'));
- // }
- if (!IsValid(COleDateTime(COleVariant(sText))))
- // if (atoi((LPCSTR)sText.Mid(nTime)) > 24000)
- {
- if(endPos == nTime &&
- nChar < '3')
- {
- sText.SetAt(nTime+1, '0');
- sText.SetAt(nTime+2, ':');
- sText.SetAt(nTime+3, '0');
- sText.SetAt(nTime+4, '0');
- SetWindowText(sText);
- SetSel(endPos+1,endPos+1);
- }
- if(endPos == nTime + 1 &&
- nChar < '4')
- {
- sText.SetAt(nTime+2, ':');
- sText.SetAt(nTime+3, '0');
- sText.SetAt(nTime+4, '0');
- SetWindowText(sText);
- SetSel(endPos+1,endPos+1);
- }
- doit=FALSE;
- }
- }//结束 时间检查
- return doit;
- }
- break;
- }
- }
- MessageBeep((UINT)-1);
- return FALSE;
- }
- void CDateEdit::SetMask(LPCSTR lpMask, LPCSTR lpLiteral, LPCSTR lpValid)
- {
- m_bUseMask=FALSE;
- TCHAR szMask[] = {'0', '9', '#', 'A', 'a', '?'};
- if(lpMask==NULL) return;
- m_strMask=lpMask;
- if(m_strMask == _T("")) return;
- if(lpLiteral!=NULL)
- {
- m_strLiteral=lpLiteral;
- if(m_strLiteral.GetLength()!=m_strMask.GetLength())
- m_strLiteral.Empty();
- }
- else
- {//自动生成
- //m_strLiteral.Empty();
- m_strLiteral = m_strMask;
- for(int i = 0; i < 6; i++)
- {
- int j = 0;
- j = m_strLiteral.Find(szMask[i], j);
- while(j>=0)
- {
- m_strLiteral.SetAt(j, TCHAR('_'));
- j = m_strLiteral.Find(szMask[i], j);
- }//end while
- }//end for
- }
- if(lpValid!=NULL)
- m_strValid=lpValid;
- else
- m_strValid.Empty();
- m_bUseMask=TRUE;
- //分配存储空间
- m_str = CString(TCHAR(' '), m_strMask.GetLength());
- }
- BOOL CDateEdit::IsValid(const COleDateTime& date)const
- {
- return (date.GetStatus() == COleDateTime::valid);
- }
- LONG CDateEdit::OnCut(UINT, LONG)
- {//禁止CUT
- /* int nStart, nEnd;
- GetSel(nStart, nEnd);
- if (nStart < nEnd)
- {
- SendMessage(WM_COPY); // copy the selection and...
- SendMessage(WM_KEYDOWN, VK_DELETE); // delete it
- }
- */
- return 0;
- }
- // Clears the current selection.
- LONG CDateEdit::OnClear(UINT wParam, LONG lParam)
- {//禁止CLEAR
- /* int nStart, nEnd;
- GetSel(nStart, nEnd);
- if (nStart < nEnd)
- SendMessage(WM_KEYDOWN, VK_DELETE); // delete the selection
- */
- return 0;
- }
- // Pastes the text from the clipboard onto the current selection.
- LONG CDateEdit::OnPaste(UINT, LONG)
- {//禁止PASTE
- /* int nStart, nEnd;
- GetSel(nStart, nEnd);
- CEdit::Default();
- CString strText = GetValidText();
- if (strText != GetText())
- {
- SetWindowText(strText);
- SetSel(nStart, nEnd);
- }
- */
- return 0;
- }
- // Insures that text set via SetWindowText is valid.
- void CDateEdit::SetClassType(LPCSTR pName, LPCSTR pMask)
- {
- try
- {
- CString sName = pName;
- sName.MakeUpper();
- Reset();
- if(sName == _T("DATEEDIT")) //日期
- {
- m_bUseMask = TRUE;
- m_isDate = TRUE; //added this
- m_strMask = _T("0000-00-00"); //掩码
- m_strLiteral = _T("____-__-__"); //掩码有效设置 有效_ 其它-
- m_str = _T(" "); //数据保存空间
- //SetWindowText(_T(" - - "));
- }
- else if(sName == _T("TIMEEDIT")) //时间
- {
- m_bUseMask = TRUE;
- m_isTime = true;
- m_strMask = _T("00:00");
- m_strLiteral = _T("__:__");
- m_str = _T(" ");
- SetWindowText(_T(" : "));
- }
- else if(sName == _T("DATETIMEEDIT")) //日期+时间
- {
- m_bUseMask = TRUE;
- m_isDateTime = true;
- m_strMask = _T("0000-00-00 00:00");
- m_strLiteral = _T("____-__-__ __:__");
- m_str = _T(" ");
- SetWindowText(_T(" - - : "));
- }
- }
- catch(...)
- {
- }
- }
- void CDateEdit::Reset()
- {
- m_bUseMask=FALSE;
- m_strMask=_T("");
- m_strLiteral=_T("");
- m_strValid=_T("");
- // m_strHours=_T("24");
- // m_strMins=_T("60");
- m_bMaskKeyInProgress=FALSE;
- m_strMaskLiteral=_T("");
- m_str = _T("");
- m_isDate = FALSE;//是否是日期格式
- m_isTime = FALSE;//是否是时间格式
- m_isDateTime = FALSE;//是否是日期时间格式
- m_isNumber = FALSE;
- }
- void CDateEdit::SendChar(UINT nChar)
- {
- m_bMaskKeyInProgress=TRUE;
- #ifdef WIN32
- AfxCallWndProc(this,m_hWnd,WM_CHAR,nChar,1);
- #else
- SendMessage(WM_CHAR,nChar,1);
- #endif
- m_bMaskKeyInProgress=FALSE;
- }
- void CDateEdit::SetDefault()
- {
- try
- {
- if (m_isDate)
- SetWindowText(_T(" - - "));
- else if (m_isTime)
- SetWindowText(_T(" : "));
- else if (m_isDateTime)
- SetWindowText(_T(" - - : "));
- }
- catch(...)
- {
- }
- }
- void CDateEdit::SetNow()
- {
- try
- {
- COleDateTime tNow = COleDateTime::GetCurrentTime();
- if (m_isDate)
- SetWindowText(tNow.Format("%Y-%m-%d"));
- else if (m_isTime)
- SetWindowText(tNow.Format("%H:%M"));
- else if (m_isDateTime)
- SetWindowText(tNow.Format("%Y-%m-%d %H:%M"));
- }
- catch(...)
- {
- }
- }
- void CDateEdit::OnEnSetfocus()
- {
- try
- {
- CString str;
- GetWindowText(str);
- str.TrimLeft ();
- str.TrimRight ();
- if(str.IsEmpty ())
- SetWindowText(CTime::GetCurrentTime ().Format ("%Y-%m-%d"));
- }
- catch(...)
- {
- }
- }
|