The Date Picker Control
How many different applications ask users for dates? It can be annoying to have to typea date according to some preset format. Many users prefer to click on a calendar to
select a day. Others find this very slow and would rather type the date, especially if
they're merely changing an existing date. The date picker control, in the MFC class
CDateTimeCtrl, gives your users the best of both worlds.
Start, as usual, by adding a call to CreateDatePicker() to CCommonView::OnCreate()
and then adding the function to the class. Add the resource ID for IDC_DATE. Like the
IP Address control, the date picker needs only to be created. Add this code to
CommonView.cpp:
void CCommonView::CreateDatePicker()
{
m_date.Create(WS_CHILD | WS_VISIBLE | DTS_SHORTDATEFORMAT,
CRect(470,120,650,150), this, IDC_DATE);
}
The CDateTimeCtrl class, of which m_date is an object, defines special styles to be used
with date picker controls. Table 10.6 lists these special styles.
Table 10.6 Date Picker Control Styles
Style Description
DTS_APPCANPARSE Instructs the date control to give more control
to your application while the user edits dates.
DTS_LONGDATEFORMAT After the date is picked, displays it like Monday,
May 18, 1998 or whatever your locale has defined
for long dates.
DTS_RIGHTALIGN Aligns the calendar with the right edge of the
control (if you don't specify this style, it will
align with the left edge).
DTS_SHOWNONE A date is optional: A check box indicates that a
date has been selected.
DTS_SHORTDATEFORMAT After the date is picked, displays it like 5/18/98 or
whatever your locale has defined for short dates.
DTS_TIMEFORMAT Displays the time as well as the date.
DTS_UPDOWN Uses an up-down control instead of a calendar
for picking.
There are a number of member functions that you might use to set colors and fonts for
this control, but the most important function is GetTime(), which gets you the date and
time entered by the user. It fills in a COleDateTime or CTime object, or a SYSTEMTIME
structure, which you can access by individual members. Here's the declaration of
SYSTEMTIME:
typedef struct _SYSTEMTIME {
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME;
If you want to do anything with this date, you're probably going to find it easier to
work with as a CTime object. The CTime class is discussed in Appendix F, "Useful Classes."
For now, you probably just want to see how easy it is to use the control, so build and
test Common yet again. Click the drop-down box next to the short date, and you will
see how the date picker got its name. Choose a date and see the short date change. Edit
the date and then drop the month down again, and you will see that the highlight has
moved to the day you entered. Notice, also, that today's date is circled on the month
part of this control.
This month calendar is a control of its own. One is created by the date picker, but you
will create another one in the next section.
No comments:
Post a Comment