123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527 |
- #include "stdafx.h"
- #include "DateEdit.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- IMPLEMENT_DYNAMIC(CDateEdit, CEdit)
- BEGIN_MESSAGE_MAP(CDateEdit, CEdit)
-
- ON_WM_CHAR()
- ON_WM_KEYDOWN()
- ON_CONTROL_REFLECT(EN_SETFOCUS, OnEnSetfocus)
- ON_WM_KILLFOCUS()
-
-
-
-
- END_MESSAGE_MAP()
- DWORD CDateEdit::GetCtrlData()
- {
- return m_dwData;
- }
- void CDateEdit::SetCtrlData(DWORD dwData)
- {
- m_dwData = dwData;
- }
- void CDateEdit::OnKillFocus(CWnd* pNewWnd)
- {
- CEdit::OnKillFocus(pNewWnd);
- CWnd* pParent = this->GetParent();
- ::PostMessage(pParent->GetSafeHwnd(), WM_USER_EDIT_END, m_bExchange, 0);
- }
- CDateEdit::CDateEdit()
- {
- Reset();
- SetClassType("DateEdit");
- }
- CDateEdit::~CDateEdit()
- {
- }
- void CDateEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- CString str;
- GetWindowText(str);
- 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();
- }
-
- }
- CEdit::OnChar(nChar, nRepCnt, nFlags);
- if (!m_bMaskKeyInProgress && m_bUseMask && m_strLiteral != _T(""))
- {
- int startPos, endPos;
- GetSel(startPos, endPos);
-
- if (endPos < m_strLiteral.GetLength())
- {
- UINT c = m_strLiteral.GetAt(endPos);
- if (c != '_')
- SendChar(c);
- }
- }
- }
- void CDateEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
-
- 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)
- {
-
- if (0)
- {
- 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)
- {
- if (sText.GetAt(8) > '3')
- sText.SetAt(8, '0');
- sText.SetAt(9, '1');
- SetWindowText(sText);
- SetSel(endPos + 1, endPos + 1);
- }
- doit = FALSE;
- }
-
- }
- if (m_isDateTime) nTime = 11;
- if (m_isTime || m_isDateTime)
- {
-
-
-
-
-
-
-
- if (!IsValid(COleDateTime(COleVariant(sText))))
-
- {
- 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 = 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);
- }
- }
- }
- 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)
- {
-
- return 0;
- }
- LONG CDateEdit::OnClear(UINT wParam, LONG lParam)
- {
-
- return 0;
- }
- LONG CDateEdit::OnPaste(UINT, LONG)
- {
-
- return 0;
- }
- void CDateEdit::SetClassType(LPCSTR pName, LPCSTR pMask)
- {
- try
- {
- CString sName = pName;
- sName.MakeUpper();
- Reset();
- if (sName == _T("DATEEDIT"))
- {
- m_bUseMask = TRUE;
- m_isDate = TRUE;
- m_strMask = _T("0000-00-00");
- m_strLiteral = _T("____-__-__");
- m_str = _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_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
- {
- m_bExchange = TRUE;
- CString str;
- GetWindowText(str);
- str.TrimLeft();
- str.TrimRight();
- if (str.IsEmpty())
- SetWindowText(CTime::GetCurrentTime().Format("%Y-%m-%d"));
- }
- catch (...)
- {
- }
- }
|