A class for the IE3 DateTime Picker

DateTimeCtrl image

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

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read