DateEdit.cpp 11 KB

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