A class for the IE3 DateTime Picker
Environment:Win95/NT 4.0, UNICODE, VC++ 5.0, IE3.0, IE4.0, WinCE 2.0 and Windows CE Toolkit
The DateTimeCtrl is implemented in the IE3 common controls (v 4.70). Those with MS Visual C++ 5.0 will have this installed and hence will have access to the control, but unfortunately not to a simple MFC wrapper for the control.
This article and the date picker class has been updated to include CE support with the help of Gerrit Hoekstra. Those using VC6 and IE4 have access to this control built into VC IDE, and hence will not need the information in this article.
The CDateTimeCtrl presented here is a simple class that provides an MFC wrapper, as well as a few overridables in order to deal with some of the window messages sent by the control. More info on the DateTime control can be found at Microsoft.
To use the control in a dialog, create a custom control with classname "SysDateTimePick32", and then subclass the control with a member variable of type CDateTimeCtrl. Alternatively, the control can be created at run-time using CDateTimeCtrl::Create. If you create the control using a custom control on dialog template then you can set the value of the controls style using the "Style" edit box in controls Properties edit box.
DateTimeCtrl styles available:
| Style | Description |
|---|---|
| DTS_UPDOWN | Use UPDOWN instead of MONTHCAL |
| DTS_SHOWNONE | Allow a NONE selection |
| DTS_SHORTDATEFORMAT | Use the short date format (app must forward WM_WININICHANGE messages) |
| DTS_LONGDATEFORMAT | Use the long date format (app must forward WM_WININICHANGE messages) |
| DTS_TIMEFORMAT | Use the time format (app must forward WM_WININICHANGE messages) |
| DTS_APPCANPARSE | Allow user entered strings (app MUST respond to DTN_USERSTRING) |
| DTS_RIGHTALIGN | Right-align popup instead of left-align it |
Operations:
CTime GetDateTime(); BOOL SetDateTime(const CTime& time); COleDateTime GetOleDateTime(); // Not in CE BOOL SetDateTime(const COleDateTime& time); // Not in CE COLORREF SetMonthCalColour(int nColIndex, COLORREF colour); COLORREF GetMonthCalColour(int nColIndex); BOOL SetFormat(LPCTSTR szFmt); void SetMonthCalFont(CFont& font, BOOL bRedraw = TRUE); CFont* GetMonthCalFont(); BOOL SetRange(CTime* pMinTime, CTime* pMaxTime); DWORD GetRange(CTime* pMinTime, CTime* pMaxTime); BOOL SetRange(COleDateTime* pMinTime, COleDateTime* pMaxTime); // Not in CE DWORD GetRange(COleDateTime* pMinTime, COleDateTime* pMaxTime); // Not in CE
Set and GetDateTime set and retrieve the date and time for the control, SetRange/GetRange set the valid date ranges, and SetFormat sets the date display format (see GetDateFormat and GetTimeFormat for the format of szFmt).
SetMonthCalColour/GetMonthCalColour get and set the colour of the monthcal
dropdown. The following values of nColIndex are used to set the different
colours:
| Value of nColIndex | Meaning |
|---|---|
| MCSC_BACKGROUND | The background color (between months) |
| MCSC_TEXT | The dates |
| MCSC_TITLEBK | Background of the title |
| MCSC_TITLETEXT | Title text |
| MCSC_MONTHBK | Background within the month cal |
| MCSC_TRAILINGTEXT | The text color of header & trailing days |
The following notification messages are used with the DateTimeCtrl.
Notification Description
DTN_DATETIMECHANGE Signals a change within the DTP control. Must return 0.
DTN_DROPDOWN Indicates that the drop-down month calendar is about to be
displayed.
DTN_CLOSEUP Indicates that the drop-down month calendar is about to be
removed.
DTN_USERSTRING Signals the end of a user's edit operation within the control.
This notification is sent only by DTP controls that use the
DTS_APPCANPARSE style. Must return 0.
DTN_WMKEYDOWN Signals that the user has pressed a key in a callback field of
the DTP control. Must return 0.
DTN_FORMAT Requests text to display in a portion of the format string
described as a callback field. Must return 0.
DTN_FORMATQUERY Requests information about the maximum allowable size of the
text to be displayed in a callback field. Must return 0.
To handle illustrate how to handle these messages (and for convenience), a number of virtual functions are included:
virtual BOOL OnDateTimeChangeNotify(LPNMDATETIMECHANGE dtmh, LRESULT* result); virtual BOOL OnDropDown(NMHDR * pNotifyStruct, LRESULT* result); virtual BOOL OnCloseUp(NMHDR * pNotifyStruct, LRESULT* result); virtual BOOL OnUserString(NMDATETIMESTRING* lpDTstring, LRESULT* pResult); virtual BOOL OnKeyDown(NMDATETIMEWMKEYDOWN* lpDTKeystroke, LRESULT* pResult); virtual BOOL OnFormat(NMDATETIMEFORMAT* lpNMFormat, LRESULT* pResult); virtual BOOL OnFormatQuery(NMDATETIMEFORMATQUERY* lpNMFormat, LRESULT* pResult);
These functions return FALSE to allow the parent window to see the notification message, and TRUE to block it. A final function
virtual void DoDateTimeChange();has been provided to handle changes in the date or time of the control.
Download source files or sample project.
NEW: Download the CE demo project by Gerrit Hoekstra.
Last updated: 14 Feb 1999

Comments
CDateTimeCtrl in VC7.1
Posted by Legacy on 01/08/2004 12:00amOriginally posted by: Sandip Shahane
Hi,
I am using CDateTimeCtrl.cpp a wrapper class over date time picker control in VC 6.0. While moving my code base to VC 7.1 it gives me following errors in the message map section. How can i fix this.
DateTimeCtrl.cpp(121) : error C2440: 'static_cast' : cannot convert from 'BOOL (__thiscall CDateTimeCtrl::* )(NMDATETIMEFORMATW *,LRESULT *)' to 'BOOL (__thiscall CCmdTarget::* )(NMHDR *,LRESULT *)'
None of the functions with this name in scope match the target type
the code piece is as follows:
BEGIN_MESSAGE_MAP(CDateTimeCtrl, CWnd)
//{{AFX_MSG_MAP(CDateTimeCtrl)
//}}AFX_MSG_MAP
ON_NOTIFY_REFLECT(DTN_DATETIMECHANGE, OnDateTimeChangeNotify)
ON_NOTIFY_REFLECT_EX(DTN_DROPDOWN, OnDropDown)
ON_NOTIFY_REFLECT_EX(DTN_CLOSEUP, OnCloseUp)
ON_NOTIFY_REFLECT_EX(DTN_FORMAT, OnFormat)
ON_NOTIFY_REFLECT_EX(DTN_FORMATQUERY, OnFormatQuery)
ON_NOTIFY_REFLECT_EX(DTN_USERSTRING, OnUserString)
ON_NOTIFY_REFLECT_EX(DTN_WMKEYDOWN, OnKeyDown)
ON_NOTIFY_REFLECT_EX(NM_SETFOCUS, OnDateTimeChangeNotify)
END_MESSAGE_MAP()
Thanks in advance.
-
ReplyI encountered this problem also and solved it this way
Posted by Matthew Sin on 04/06/2004 03:02amIn the message handler, change the parameter from NMDATETIMEFORMATW* back to NMHDR*, add a variable of type NMDATETIMEFORMATW* and assign the value of parameter to the variable by casting it from NMDATETIMEFORMATW* to NMHDR*. Use the variable instead of the parameter in the handler
ReplyHow disappear arrows the DTP control to modify date values?
Posted by Legacy on 08/29/2003 12:00amOriginally posted by: Dmitry
How disappear arrows the DTP control to modify date values?
ReplyI need that DTP control was in time mode and without arrows.
DTS_UPDOWN style is not aid. Please, help me.
Notification of unhecking the checkbox inside the DateTimeCtrl...
Posted by Legacy on 07/06/2003 12:00amOriginally posted by: Tammam de Dragonfly
if I choose the DTS_SHOWNONE style how can i get a notification if the user disable the DateTimeCtrl by unchecking the small checkbox that appears inside the control
Replythanx
Random Font Size
Posted by Legacy on 01/29/2003 12:00amOriginally posted by: Chris
The calandar font size changes randomly when I use cdatetimectrl. Sometimes the calendar will display very large diagonal font making the calendar very large on the display. Has anyone seen this problem or have a fix?
Replyaccessing cdatetimectrl in browser/html?
Posted by Legacy on 10/21/2002 12:00amOriginally posted by: ben
hi...
i want to access a cdatetimectrl-element in the browser window of a mfc application. is there any possibility to create such a ctrl through html code?
(it is realized this way in the "start->search->search files->search options/date" function in win2000)
thanks for any help...
ReplyHow to use Callback fields in CDateCtrl?
Posted by Legacy on 07/02/2002 12:00amOriginally posted by: Ruthy
Will appreciate if someone could send an example of using the CDateTimeCtrl with a modified format.
ReplyI need to get Milliseconds in the time format.
Thax
ExCalendar
Posted by Legacy on 01/30/2002 12:00amOriginally posted by: Mike Philips
ReplyHelp!
Posted by Legacy on 01/16/2002 12:00amOriginally posted by: Patricia
Can you tell me if it is possible to change the color of the text appearing in the edit? The WM_CTCOLOR seems to not work...It is quite easy to change the background color but for the text it seems impossible!
ReplyIs it possible to display the date time control as edit only without spin?
DTS_APPCANPARSE & DTN_USERSTRING
Posted by Legacy on 11/19/2001 12:00amOriginally posted by: Ralf Hartmann
I have changed DTS_APPCANPARSE to true in your example and know i have the problem, that some changes (full replace, character delete or insert) in the edit field are revoked after leaving the field. Maybe this is an knwon bug in my environment (NT 4.0 SP6, VC++ 6.0, SP4). Or is there any additional message which have to be handled?
ReplyI know..
Posted by Legacy on 08/02/2001 12:00amOriginally posted by: seowontae
Hi...
I want to display like "2001-08-02" using DateTimepicker control
ReplyLoading, Please Wait ...