DateTimeEditCtrl.h
上传用户:yd19691116
上传日期:2013-02-12
资源大小:43k
文件大小:9k
源码类别:

Windows编程

开发平台:

Visual C++

  1. ////////////////////////////////////////////////////////////////////////////
  2. // File: DateTimeEditCtrl.h
  3. // Version: 3
  4. // Created: 17-Jan-2003
  5. //
  6. // Author: Paul S. Vickery
  7. // E-mail: paul@vickeryhome.freeserve.co.uk
  8. //
  9. // Class to provide an editable Date picker control. Based on CDateTimeCtrl.
  10. //
  11. // You are free to use or modify this code, with no restrictions, other than
  12. // you continue to acknowledge me as the original author in this source code,
  13. // or any code derived from it.
  14. //
  15. // If you use this code, or use it as a base for your own code, it would be 
  16. // nice to hear from you simply so I know it's not been a waste of time!
  17. //
  18. // Copyright (c) 2003 Paul S. Vickery
  19. //
  20. ////////////////////////////////////////////////////////////////////////////
  21. // Version History:
  22. //
  23. // Version 3 - 17-Jan-2003
  24. // =======================
  25. // - Fixed bug where a call to Get/SetWindowText, SetFont, or any function 
  26. //   which resulted in a sending of a DTM_XXX message caused an assertion the 
  27. //   first time round, if called from the OnInitDialog of a parent dialog, or 
  28. //   equivalent.
  29. // 
  30. // Version 2 - 07-Jan-2003
  31. // =======================
  32. // - Edit control notifications (eg EN_CHANGE) now get sent to date control's 
  33. //   parent window
  34. // - Added features as suggested by Marc Clifton on CodeProject:
  35. //   - Added function to validate a date string (IsValidDate)
  36. //   - Added function to only allow valid chars to be typed into the edit control.
  37. //     Also a function to set the valid characters. The default valid characters 
  38. //     are 0-9 and the current user's locale's date separator. If the user changes 
  39. //     the locale settings then the control will automatically pick this up and 
  40. //     use the new separator.
  41. //   - Added ability to use up/down keys to edit portions of the date text
  42. // 
  43. // Version 1 - 03-Jan-2003
  44. // =======================
  45. // Initial version
  46. // 
  47. ////////////////////////////////////////////////////////////////////////////
  48. // PLEASE LEAVE THIS HEADER INTACT
  49. ////////////////////////////////////////////////////////////////////////////
  50. #if !defined(AFX_DATETIMEEDITCTRL_H__737F6EBA_CC75_11D6_8634_0000B48746CF__INCLUDED_)
  51. #define AFX_DATETIMEEDITCTRL_H__737F6EBA_CC75_11D6_8634_0000B48746CF__INCLUDED_
  52. #if _MSC_VER > 1000
  53. #pragma once
  54. #endif // _MSC_VER > 1000
  55. #include "afxdtctl.h" // only needed for date/time controls
  56. #define DTCEM_DESTROY_CALENDAR (WM_USER+1)
  57. #define DTCEM_RECREATE (WM_USER+2)
  58. /////////////////////////////////////////////////////////////////////////////
  59. // CDateTimeEditCtrlButton window
  60. class CDateTimeEditCtrlButton : public CButton
  61. {
  62. public:
  63.   CDateTimeEditCtrlButton() { m_bRestoreFocus = FALSE; m_pWndLastFocus = NULL; m_bNonEditable = FALSE; }
  64.   void RestoreFocus(BOOL bRestore = TRUE) { m_bRestoreFocus = bRestore; }
  65.   void SetNonEditable(BOOL bNonEditable = TRUE) { m_bNonEditable = bNonEditable; }
  66.   // ClassWizard generated virtual function overrides
  67.   //{{AFX_VIRTUAL(CDateTimeEditCtrlButton)
  68. public:
  69. virtual BOOL PreTranslateMessage(MSG* pMsg);
  70. //}}AFX_VIRTUAL
  71.   // Generated message map functions
  72. protected:
  73.   CWnd* m_pWndLastFocus;
  74.   BOOL m_bRestoreFocus;
  75.   BOOL m_bNonEditable;
  76.   //{{AFX_MSG(CDateTimeEditCtrlButton)
  77.   afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
  78.   afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
  79.   afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
  80.   //}}AFX_MSG
  81.   DECLARE_MESSAGE_MAP()
  82. };
  83. /////////////////////////////////////////////////////////////////////////////
  84. // CDateTimeEditCtrlEditCtrl window
  85. class CDateTimeEditCtrlEditCtrl : public CEdit
  86. {
  87. public:
  88.   BOOL GetAllowUpDownKeys();
  89.   void SetAllowUpDownKeys(BOOL bAllow = TRUE);
  90.   CString GetValidChars();
  91.   void SetValidChars(LPCTSTR lpszValidChars = NULL);
  92.   BOOL GetValidCharsOnly();
  93.   void SetValidCharsOnly(BOOL bValidCharsOnly = TRUE);
  94.   CDateTimeEditCtrlEditCtrl();
  95.   void SetNonEditable(BOOL bNonEditable = TRUE) { m_bNonEditable = bNonEditable; }
  96.   // ClassWizard generated virtual function overrides
  97.   //{{AFX_VIRTUAL(CDateTimeEditCtrlEditCtrl)
  98. public:
  99. virtual BOOL PreTranslateMessage(MSG* pMsg);
  100. //}}AFX_VIRTUAL
  101.   // Generated message map functions
  102. protected:
  103. void SendDateTimeChange();
  104.   BOOL m_bNonEditable;
  105.   BOOL m_bValidCharsOnly;
  106.   CString m_sValidChars;
  107.   BOOL m_bAllowUpDownKeys;
  108. //{{AFX_MSG(CDateTimeEditCtrlEditCtrl)
  109. afx_msg UINT OnGetDlgCode();
  110. afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
  111. afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
  112. afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
  113. afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
  114. afx_msg void OnSettingChange(UINT uFlags, LPCTSTR lpszSection);
  115. //}}AFX_MSG
  116.   DECLARE_MESSAGE_MAP()
  117. };
  118. /////////////////////////////////////////////////////////////////////////////
  119. // CDateTimeEditCtrlMonthCalCtrl window
  120. class CDateTimeEditCtrlMonthCalCtrl : public CMonthCalCtrl
  121. {
  122. // Construction
  123. public:
  124. CDateTimeEditCtrlMonthCalCtrl();
  125. // Attributes
  126. public:
  127. // Operations
  128. public:
  129. // Overrides
  130. // ClassWizard generated virtual function overrides
  131. //{{AFX_VIRTUAL(CDateTimeEditCtrlMonthCalCtrl)
  132. protected:
  133. virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
  134. //}}AFX_VIRTUAL
  135. // Implementation
  136. public:
  137. virtual ~CDateTimeEditCtrlMonthCalCtrl();
  138. // Generated message map functions
  139. protected:
  140. //{{AFX_MSG(CDateTimeEditCtrlMonthCalCtrl)
  141. //}}AFX_MSG
  142. DECLARE_MESSAGE_MAP()
  143. };
  144. /////////////////////////////////////////////////////////////////////////////
  145. // CDateTimeEditCtrlCalendarWnd window
  146. class CDateTimeEditCtrlCalendarWnd : public CWnd
  147. {
  148. // Construction
  149. public:
  150.   CDateTimeEditCtrlCalendarWnd(CWnd* pComboParent, DWORD dwMCStyle = 0);
  151. // Attributes
  152. public:
  153.   CDateTimeEditCtrlMonthCalCtrl* GetMonthCalCtrl() { return m_pCalendar; }
  154. // Operations
  155. public:
  156. // Overrides
  157. // ClassWizard generated virtual function overrides
  158. //{{AFX_VIRTUAL(CDateTimeEditCtrlCalendarWnd)
  159. public:
  160. virtual BOOL Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL);
  161. virtual BOOL DestroyWindow();
  162. protected:
  163. virtual BOOL OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult);
  164. //}}AFX_VIRTUAL
  165. // Implementation
  166. public:
  167. virtual ~CDateTimeEditCtrlCalendarWnd();
  168. // Generated message map functions
  169. protected:
  170. //{{AFX_MSG(CDateTimeEditCtrlCalendarWnd)
  171. afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
  172. afx_msg void OnActivateApp(BOOL bActive, HTASK hTask);
  173. //}}AFX_MSG
  174. DECLARE_MESSAGE_MAP()
  175. private:
  176. CDateTimeEditCtrlMonthCalCtrl* m_pCalendar;
  177. CWnd* m_pComboParent;
  178. DWORD m_dwMCStyle;
  179. };
  180. /////////////////////////////////////////////////////////////////////////////
  181. // CDateTimeEditCtrl window
  182. class CDateTimeEditCtrl : public CDateTimeCtrl
  183. {
  184.   DECLARE_DYNAMIC(CDateTimeEditCtrl)
  185. // Construction
  186. public:
  187. CDateTimeEditCtrl();
  188. // Attributes
  189. public:
  190. // Operations
  191. public:
  192. // Overrides
  193. // ClassWizard generated virtual function overrides
  194. //{{AFX_VIRTUAL(CDateTimeEditCtrl)
  195. protected:
  196. virtual BOOL OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult);
  197. virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam);
  198. virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
  199. virtual void PreSubclassWindow();
  200. //}}AFX_VIRTUAL
  201. // Implementation
  202. public:
  203.   BOOL Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID);
  204.   virtual ~CDateTimeEditCtrl();
  205.   CEdit* GetEditControl() const { return m_pEdit; }
  206.   void EnableButton(BOOL bEnable = TRUE);
  207.   void RestoreFocus(BOOL bRestore = TRUE) { if (m_pBtn != NULL) m_pBtn->RestoreFocus(bRestore); }
  208.   void SetNonEditable(BOOL bNonEditable = TRUE);
  209.   BOOL GetNonEditable() { return m_bNonEditable; }
  210.   virtual BOOL IsValidDate(LPCTSTR lpszDate = NULL);
  211.   void SetValidCharsOnly(BOOL bValidCharsOnly = TRUE);
  212.   BOOL GetValidCharsOnly();
  213.   void SetValidChars(LPCTSTR lpszValidChars = NULL);
  214.   CString GetValidChars();
  215.   void SetAllowUpDownKeys(BOOL bAllow = TRUE);
  216.   BOOL GetAllowUpDownKeys();
  217. // Generated message map functions
  218. protected:
  219.   BOOL DestroyCalendar(BOOL bDiscard = FALSE);
  220.   BOOL CreateCalendar();
  221.   CDateTimeEditCtrlEditCtrl* m_pEdit;
  222.   CDateTimeEditCtrlButton* m_pBtn;
  223.   BOOL m_bNonEditable;
  224.   CSpinButtonCtrl* m_pSpin;
  225.   CDateTimeEditCtrlCalendarWnd* m_pCalWnd;
  226.   CString m_sOrigDate;
  227.   BOOL m_bInCreate;
  228.   // monthcal properties
  229.   HFONT m_hMCFont;
  230.   COLORREF m_acrMonthCal[6];
  231. //{{AFX_MSG(CDateTimeEditCtrl)
  232. afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
  233. afx_msg void OnEnable(BOOL bEnable);
  234. afx_msg void OnSetFocus(CWnd* pOldWnd);
  235. afx_msg void OnSize(UINT nType, int cx, int cy);
  236. afx_msg void OnCancelMode();
  237. //}}AFX_MSG
  238. afx_msg LONG OnDestroyCalendar(WPARAM wParam, LPARAM lParam);
  239. afx_msg void OnStyleChanging(int nStyleType, LPSTYLESTRUCT lpStyleStruct);
  240. afx_msg LONG OnRecreate(WPARAM wParam, LPARAM lParam);
  241. DECLARE_MESSAGE_MAP()
  242. };
  243. /////////////////////////////////////////////////////////////////////////////
  244. //{{AFX_INSERT_LOCATION}}
  245. // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
  246. #endif // !defined(AFX_DATETIMEEDITCTRL_H__737F6EBA_CC75_11D6_8634_0000B48746CF__INCLUDED_)