DateEdit.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. #include "stdafx.h"
  2. #include "DateEdit.h"
  3. #ifdef _DEBUG
  4. #define new DEBUG_NEW
  5. #undef THIS_FILE
  6. static char THIS_FILE[] = __FILE__;
  7. #endif
  8. /////////////////////////////////////////////////////////////////////////////
  9. // COleDateTime read /write
  10. /*
  11. COleDateTime ReadCOleDateTime(LPCSTR lpszData)
  12. {
  13. COleDateTime DateTime;
  14. DateTime.ParseDateTime(lpszData);
  15. return DateTime;
  16. }
  17. void FormatCOleDateTime(CString& strData, COleDateTime DateTime, int len)
  18. {
  19. strData = "";
  20. if (DateTime.m_dt == 0) return;
  21. if (len == 8)
  22. strData = DateTime.Format("%d/%m/%y");
  23. else if(len == 5) // added these two
  24. strData = DateTime.Format("%H:%M");
  25. else
  26. strData = DateTime.Format("%d/%m/%Y");
  27. }
  28. /////////////////////////////////////////////////////////////////////////////
  29. // DDX for mask control
  30. void AFXAPI DDX_OleDate(CDataExchange* pDX, int nIDC, CDateEdit& rControl, COleDateTime& Date)
  31. {
  32. DDX_Control(pDX, nIDC, (CWnd&)rControl);
  33. if (!pDX->m_bSaveAndValidate)
  34. rControl.SetDate(Date);
  35. else
  36. Date = rControl.GetDate();
  37. }
  38. */
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CDateEdit
  41. IMPLEMENT_DYNAMIC(CDateEdit, CEdit)
  42. BEGIN_MESSAGE_MAP(CDateEdit, CEdit)
  43. //{{AFX_MSG_MAP(CDateEdit)
  44. ON_WM_CHAR()
  45. ON_WM_KEYDOWN()
  46. ON_CONTROL_REFLECT(EN_SETFOCUS, OnEnSetfocus)
  47. ON_WM_KILLFOCUS()
  48. //}}AFX_MSG_MAP
  49. // ON_MESSAGE(WM_CUT, OnCut)
  50. // ON_MESSAGE(WM_PASTE, OnPaste)
  51. // ON_MESSAGE(WM_CLEAR, OnClear)
  52. END_MESSAGE_MAP()
  53. DWORD CDateEdit::GetCtrlData()
  54. {
  55. return m_dwData;
  56. }
  57. void CDateEdit::SetCtrlData(DWORD dwData)
  58. {
  59. m_dwData = dwData;
  60. }
  61. void CDateEdit::OnKillFocus(CWnd* pNewWnd)
  62. {
  63. CEdit::OnKillFocus(pNewWnd);
  64. CWnd* pParent = this->GetParent();
  65. ::PostMessage(pParent->GetSafeHwnd(), WM_USER_EDIT_END, m_bExchange, 0);
  66. }
  67. CDateEdit::CDateEdit()
  68. {
  69. Reset();
  70. SetClassType("DateEdit");
  71. }
  72. CDateEdit::~CDateEdit()
  73. {
  74. }
  75. /////////////////////////////////////////////////////////////////////////////
  76. // CDateEdit message handlers
  77. void CDateEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
  78. {
  79. CString str;
  80. GetWindowText(str);
  81. for (int i = 0; i < str.GetLength() && i < m_str.GetLength(); i++)
  82. m_str.SetAt(i, str.GetAt(i));
  83. if (!m_bMaskKeyInProgress)
  84. if (!CheckChar(nChar))
  85. return;
  86. if (m_bUseMask) //要使用掩码
  87. {
  88. if (isprint(nChar)) //是可打印字符
  89. {
  90. int startPos, endPos;
  91. GetSel(startPos, endPos);
  92. SetSel(startPos, endPos + 1);
  93. Clear();
  94. }
  95. /* else if(nChar==VK_BACK)
  96. {
  97. int startPos,endPos;
  98. GetSel(startPos,endPos);
  99. if((startPos==endPos) && (startPos>=1) && (startPos<=m_str.GetLength()))
  100. {
  101. char c;
  102. if(m_strMaskLiteral != _T(""))
  103. c=m_strMaskLiteral[startPos-1];
  104. TRACE("m_strMaskLiteral=[%s](%s)\n",m_strMaskLiteral,m_str);
  105. //回退光标
  106. SendMessage(WM_KEYDOWN,VK_LEFT,0);
  107. if(m_strMaskLiteral != _T(""))
  108. {
  109. //恢复在该位置预设的字符
  110. SendChar(c);
  111. //再次退回
  112. SendMessage(WM_KEYDOWN,VK_LEFT,0);
  113. }
  114. }
  115. else //越界或者存在选择区域
  116. MessageBeep((UINT)-1);
  117. return;
  118. }*/
  119. }
  120. CEdit::OnChar(nChar, nRepCnt, nFlags);
  121. if (!m_bMaskKeyInProgress && m_bUseMask && m_strLiteral != _T(""))
  122. {
  123. int startPos, endPos;
  124. GetSel(startPos, endPos);
  125. //确保字符串的长度小于m_strLiteral的长度
  126. if (endPos < m_strLiteral.GetLength())
  127. {
  128. UINT c = m_strLiteral.GetAt(endPos);
  129. if (c != '_')
  130. SendChar(c);
  131. }
  132. }
  133. }
  134. void CDateEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
  135. {
  136. /* if(m_bUseMask)
  137. {
  138. switch(nChar)//忽略删除和插入键
  139. {
  140. case VK_DELETE:
  141. case VK_INSERT: return;
  142. }
  143. }*/
  144. CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
  145. }
  146. BOOL CDateEdit::CheckChar(UINT nChar)
  147. {
  148. UINT c;
  149. int nTime = 0;
  150. CString sText;
  151. //如果不使用掩码,则返回
  152. if (!m_bUseMask) return TRUE;
  153. //如果是控制字符,则返回
  154. if (!isprint(nChar)) return TRUE;
  155. //如果存在选择区域,则取消选择
  156. int startPos, endPos;
  157. GetSel(startPos, endPos);
  158. SetSel(-1, 0);
  159. //重新选中原选择区域的第一个字符
  160. SetSel(startPos, startPos);
  161. GetSel(startPos, endPos);
  162. //确保字符串的长度不超过掩码的长度
  163. if (endPos >= m_strMask.GetLength())
  164. {
  165. MessageBeep((UINT)-1);
  166. return FALSE;
  167. }
  168. //检查在当前位置是否应存在文字
  169. c = '_';
  170. if (m_strLiteral != _T("") &&
  171. m_strLiteral.GetLength() > endPos)
  172. c = m_strLiteral.GetAt(endPos);
  173. if (c != '_')
  174. {//发送默认值
  175. SendChar(c);
  176. GetSel(startPos, endPos);
  177. }
  178. //检查字符的有效性
  179. if (m_strValid.Find(nChar) != -1) return TRUE;
  180. //检查当前位置的掩码
  181. c = m_strMask.GetAt(endPos);
  182. BOOL doit = TRUE;
  183. sText = m_str;
  184. sText.SetAt(endPos, nChar);
  185. if (nChar == VK_SPACE) return true; //空格
  186. switch (c)
  187. {
  188. case '0': //只能是数字
  189. {
  190. BOOL doit = TRUE;
  191. if (isdigit(nChar))
  192. {
  193. if (m_isDate || m_isDateTime) //是否按照日期格式输入
  194. {
  195. // sText.SetAt(endPos, nChar);
  196. if (0)//!IsValid(COleDateTime(COleVariant(sText))))
  197. {
  198. if (endPos < 8)
  199. {//月、日设置为默认
  200. sText.SetAt(4, '-');
  201. sText.SetAt(5, '0');
  202. sText.SetAt(6, '1');
  203. sText.SetAt(7, '-');
  204. sText.SetAt(8, '0');
  205. sText.SetAt(9, '1');
  206. SetWindowText(sText);
  207. SetSel(endPos + 1, endPos + 1);
  208. }
  209. if (endPos >= 8)
  210. {//日设置为少30
  211. if (sText.GetAt(8) > '3')
  212. sText.SetAt(8, '0');
  213. sText.SetAt(9, '1');
  214. SetWindowText(sText);
  215. SetSel(endPos + 1, endPos + 1);
  216. }
  217. doit = FALSE;
  218. }
  219. /* if(endPos==8)
  220. {
  221. if(nChar>'3') //每月只能有三十几天
  222. doit=FALSE;
  223. }
  224. if(endPos==9)
  225. {
  226. if(m_str.GetAt(5)=='0' &&
  227. m_str.GetAt(6)=='2' &&
  228. m_str.GetAt(8)=='2')
  229. {
  230. if(nChar>'8')//2月不超过29天
  231. doit=FALSE;
  232. }
  233. else if(m_str.GetAt(5)=='0' &&
  234. m_str.GetAt(6)=='2' &&
  235. m_str.GetAt(8)>'2')
  236. {//2月不起30
  237. doit=FALSE;
  238. }
  239. else if(m_str.GetAt(8)=='3')
  240. {
  241. if(nChar>'1')//每月不超过31天
  242. doit=FALSE;
  243. }
  244. }
  245. if(endPos==5)
  246. {
  247. if(nChar>'1') //一年只能有12个月
  248. doit=FALSE;
  249. }
  250. if(endPos==6)
  251. {
  252. if(m_str.GetAt(5)=='1')
  253. {
  254. if(nChar>'2') //一年只能有12个月
  255. doit=FALSE;
  256. }
  257. }
  258. */
  259. }//结束日期检查
  260. if (m_isDateTime) nTime = 11;
  261. if (m_isTime || m_isDateTime) //按时间格式检查
  262. {
  263. //转换时间为HH0MM
  264. // for(int i = nTime; i < nTime+5; i++)
  265. // {
  266. // if (sText.GetAt(i) == TCHAR(' ') ||
  267. // sText.GetAt(i) == TCHAR(':'))
  268. // sText.SetAt(i, TCHAR('0'));
  269. // }
  270. if (!IsValid(COleDateTime(COleVariant(sText))))
  271. // if (atoi((LPCSTR)sText.Mid(nTime)) > 24000)
  272. {
  273. if (endPos == nTime &&
  274. nChar < '3')
  275. {
  276. sText.SetAt(nTime + 1, '0');
  277. sText.SetAt(nTime + 2, ':');
  278. sText.SetAt(nTime + 3, '0');
  279. sText.SetAt(nTime + 4, '0');
  280. SetWindowText(sText);
  281. SetSel(endPos + 1, endPos + 1);
  282. }
  283. if (endPos == nTime + 1 &&
  284. nChar < '4')
  285. {
  286. sText.SetAt(nTime + 2, ':');
  287. sText.SetAt(nTime + 3, '0');
  288. sText.SetAt(nTime + 4, '0');
  289. SetWindowText(sText);
  290. SetSel(endPos + 1, endPos + 1);
  291. }
  292. doit = FALSE;
  293. }
  294. }//结束 时间检查
  295. return doit;
  296. }
  297. break;
  298. }
  299. }
  300. MessageBeep((UINT)-1);
  301. return FALSE;
  302. }
  303. void CDateEdit::SetMask(LPCSTR lpMask, LPCSTR lpLiteral, LPCSTR lpValid)
  304. {
  305. m_bUseMask = FALSE;
  306. TCHAR szMask[] = { '0', '9', '#', 'A', 'a', '?' };
  307. if (lpMask == NULL) return;
  308. m_strMask = lpMask;
  309. if (m_strMask == _T("")) return;
  310. if (lpLiteral != NULL)
  311. {
  312. m_strLiteral = lpLiteral;
  313. if (m_strLiteral.GetLength() != m_strMask.GetLength())
  314. m_strLiteral.Empty();
  315. }
  316. else
  317. {//自动生成
  318. //m_strLiteral.Empty();
  319. m_strLiteral = m_strMask;
  320. for (int i = 0; i < 6; i++)
  321. {
  322. int j = 0;
  323. j = m_strLiteral.Find(szMask[i], j);
  324. while (j >= 0)
  325. {
  326. m_strLiteral.SetAt(j, TCHAR('_'));
  327. j = m_strLiteral.Find(szMask[i], j);
  328. }//end while
  329. }//end for
  330. }
  331. if (lpValid != NULL)
  332. m_strValid = lpValid;
  333. else
  334. m_strValid.Empty();
  335. m_bUseMask = TRUE;
  336. //分配存储空间
  337. m_str = CString(TCHAR(' '), m_strMask.GetLength());
  338. }
  339. BOOL CDateEdit::IsValid(const COleDateTime& date)const
  340. {
  341. return (date.GetStatus() == COleDateTime::valid);
  342. }
  343. LONG CDateEdit::OnCut(UINT, LONG)
  344. {//禁止CUT
  345. /* int nStart, nEnd;
  346. GetSel(nStart, nEnd);
  347. if (nStart < nEnd)
  348. {
  349. SendMessage(WM_COPY); // copy the selection and...
  350. SendMessage(WM_KEYDOWN, VK_DELETE); // delete it
  351. }
  352. */
  353. return 0;
  354. }
  355. // Clears the current selection.
  356. LONG CDateEdit::OnClear(UINT wParam, LONG lParam)
  357. {//禁止CLEAR
  358. /* int nStart, nEnd;
  359. GetSel(nStart, nEnd);
  360. if (nStart < nEnd)
  361. SendMessage(WM_KEYDOWN, VK_DELETE); // delete the selection
  362. */
  363. return 0;
  364. }
  365. // Pastes the text from the clipboard onto the current selection.
  366. LONG CDateEdit::OnPaste(UINT, LONG)
  367. {//禁止PASTE
  368. /* int nStart, nEnd;
  369. GetSel(nStart, nEnd);
  370. CEdit::Default();
  371. CString strText = GetValidText();
  372. if (strText != GetText())
  373. {
  374. SetWindowText(strText);
  375. SetSel(nStart, nEnd);
  376. }
  377. */
  378. return 0;
  379. }
  380. // Insures that text set via SetWindowText is valid.
  381. void CDateEdit::SetClassType(LPCSTR pName, LPCSTR pMask)
  382. {
  383. try
  384. {
  385. CString sName = pName;
  386. sName.MakeUpper();
  387. Reset();
  388. if (sName == _T("DATEEDIT")) //日期
  389. {
  390. m_bUseMask = TRUE;
  391. m_isDate = TRUE; //added this
  392. m_strMask = _T("0000-00-00"); //掩码
  393. m_strLiteral = _T("____-__-__"); //掩码有效设置 有效_ 其它-
  394. m_str = _T(" "); //数据保存空间
  395. //SetWindowText(_T(" - - "));
  396. }
  397. else if (sName == _T("TIMEEDIT")) //时间
  398. {
  399. m_bUseMask = TRUE;
  400. m_isTime = true;
  401. m_strMask = _T("00:00");
  402. m_strLiteral = _T("__:__");
  403. m_str = _T(" ");
  404. SetWindowText(_T(" : "));
  405. }
  406. else if (sName == _T("DATETIMEEDIT")) //日期+时间
  407. {
  408. m_bUseMask = TRUE;
  409. m_isDateTime = true;
  410. m_strMask = _T("0000-00-00 00:00");
  411. m_strLiteral = _T("____-__-__ __:__");
  412. m_str = _T(" ");
  413. SetWindowText(_T(" - - : "));
  414. }
  415. }
  416. catch (...)
  417. {
  418. }
  419. }
  420. void CDateEdit::Reset()
  421. {
  422. m_bUseMask = FALSE;
  423. m_strMask = _T("");
  424. m_strLiteral = _T("");
  425. m_strValid = _T("");
  426. // m_strHours=_T("24");
  427. // m_strMins=_T("60");
  428. m_bMaskKeyInProgress = FALSE;
  429. m_strMaskLiteral = _T("");
  430. m_str = _T("");
  431. m_isDate = FALSE;//是否是日期格式
  432. m_isTime = FALSE;//是否是时间格式
  433. m_isDateTime = FALSE;//是否是日期时间格式
  434. m_isNumber = FALSE;
  435. }
  436. void CDateEdit::SendChar(UINT nChar)
  437. {
  438. m_bMaskKeyInProgress = TRUE;
  439. #ifdef WIN32
  440. AfxCallWndProc(this, m_hWnd, WM_CHAR, nChar, 1);
  441. #else
  442. SendMessage(WM_CHAR,nChar,1);
  443. #endif
  444. m_bMaskKeyInProgress = FALSE;
  445. }
  446. void CDateEdit::SetDefault()
  447. {
  448. try
  449. {
  450. if (m_isDate)
  451. SetWindowText(_T(" - - "));
  452. else if (m_isTime)
  453. SetWindowText(_T(" : "));
  454. else if (m_isDateTime)
  455. SetWindowText(_T(" - - : "));
  456. }
  457. catch (...)
  458. {
  459. }
  460. }
  461. void CDateEdit::SetNow()
  462. {
  463. try
  464. {
  465. COleDateTime tNow = COleDateTime::GetCurrentTime();
  466. if (m_isDate)
  467. SetWindowText(tNow.Format("%Y-%m-%d"));
  468. else if (m_isTime)
  469. SetWindowText(tNow.Format("%H:%M"));
  470. else if (m_isDateTime)
  471. SetWindowText(tNow.Format("%Y-%m-%d %H:%M"));
  472. }
  473. catch (...)
  474. {
  475. }
  476. }
  477. void CDateEdit::OnEnSetfocus()
  478. {
  479. try
  480. {
  481. m_bExchange = TRUE;
  482. CString str;
  483. GetWindowText(str);
  484. str.TrimLeft();
  485. str.TrimRight();
  486. if (str.IsEmpty())
  487. SetWindowText(CTime::GetCurrentTime().Format("%Y-%m-%d"));
  488. }
  489. catch (...)
  490. {
  491. }
  492. }