Use the Date/Time Picker common control (IE3+)
NOTE: You must also have the new COMCTL32.DLL (version 4.7 or later). This is installed with Internet Explorer 3 and will come standard with the new Windows 98. So if you are using VC++ 5 then you probably already have this DLL.
The API support is available in VC++ 5.0. Otherwise, you need to download the ActiveX SDK from the MS website. To use the date/time picker, do the following:
Put the following in your app's InitInstance:
INITCOMMONCONTROLSEX icc;
icc.dwSize = sizeof(INITCOMMONCONTROLSEX);
icc.dwICC = ICC_DATE_CLASSES;
InitCommonControlsEx(&icc);
Insert a custom control in your dialog where you want the date or time picker. For the class name, enter "SysDateTimePick32". For the style, see commctrl.h and search for the DTS_* style and enter the desired style (usually either DTS_SHORTDATEFORMAT or DTS_TIMEFORMAT). Be sure to make the custom control visible and have a tabstop.
You can always use CreateEx() to create them manually.
You can DDX the control to a SYSTEMTIME structure with the following code:
void AFXAPI DDX_DateTime(CDataExchange *pDX, int nIDC,
SYSTEMTIME &SysTime)
{
HWND hWnd = pDX->m_pDlgWnd->GetDlgItem(nIDC)->GetSafeHwnd();
ASSERT (hWnd != NULL);
if (pDX->m_bSaveAndValidate)
{
::SendMessage(hWnd, DTM_GETSYSTEMTIME, 0,
(LPARAM)&SysTime);
}
else // initializing
{
::SendMessage(hWnd, DTM_SETSYSTEMTIME, GDT_VALID,
(LPARAM)&SysTime);
}
}
void CCtrlTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCtrlTestDlg)
//}}AFX_DATA_MAP
// m_Date and m_Time are SYSTEMTIME structures
DDX_DateTime(pDX, IDC_DATE, m_Date);
DDX_DateTime(pDX, IDC_TIME, m_Time);
}

Comments
Please Help me
Posted by joselo58 on 07/05/2007 01:37pmI don't know how to work with DatatimePicker when I change the value, for example Today It's 5th July I want to add + 2 months. Please send me the code... Jose Luis.
ReplyHow to update the custom control with a date
Posted by Legacy on 10/07/2003 12:00amOriginally posted by: Jabberwoky
ReplyRead time from a edit box
Posted by Legacy on 01/09/2003 12:00amOriginally posted by: Stefano
Probaly it's a stupid question but I'm not a really good programmer. I want to read the hour, according to the format hh:mm, from a edit box.
Can you help me?
Thank you
ReplyExCalendar
Posted by Legacy on 01/30/2002 12:00amOriginally posted by: Mike Philips
ReplyUrgent!
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?
European format
Posted by Legacy on 12/26/2001 12:00amOriginally posted by: Malka Shafir
Can I change the text on the bottom "Today: 12/26/01" to European format ("Today: 26/12/01")?
ReplyIf yes, I would like to know how.
Thanks a lot.
Malka
returned dates 4 years in future
Posted by Legacy on 08/11/2001 12:00amOriginally posted by: sqe
After picking a date (let's say 1.8.2001) and closing the dialog with OK, in a variable m_blahblah (a CTime object) used for ddx (i used classwizard to add that variable(s)), the value in m_blahblah is about 4 years in the future (let's say 10.3.2004)..., i use 4 callendars and they all return values the same 4 years (minus a few days) in future (if i choose 2.8.2001, it returns 11.3.2004, so it works, but a little bit strange:) ... could you help me please? (i use MS VC++ 6.0)
thanx,
brano
PS: i also tryied to use CDateTimeOle (or what!, i don't know the exact name now:) object (not CTime) using classwizard, in that case, the returned dates were correct or "-1"'s, if i changed the year or month, the returned value was ok (no +4 years), but if i only clicked on some day (in actual month/year), the returned values (in .GetDay(), .GetMonth(), GetYear()) were all "-1". I am realy tired of it, please help me... ... thanx
b.
Replycontrol bug?
Posted by Legacy on 08/01/2001 12:00amOriginally posted by: TP
I have instantiated the control under Win32 and can select dates as normal when using the popup date grid, which works perfectly.
However, if I attempt to modify the year value in either the "long date" or "short date" view (without opening the popup date grid), I consistently get an access violation. As far as I know, that part of the interface should be handled automatically, so is there a bug in the control?
TP
ReplyHow do I change the date in run time (programmatically) ?
Posted by Legacy on 05/24/2001 12:00amOriginally posted by: Michael Tjong
ReplySaving Date Ctrl to Database
Posted by Legacy on 02/10/2001 12:00amOriginally posted by: Kalvain
Year and a half late post but thought I would add for anyone in the future looking for this answer.
When you bind a Date Time Picker to a database variable the class wizard will set up a function in the data exchange that does not exist. Correction is below...
///Bad code inserted by class wizard
DDX_FieldDateTimeCtrl(pDX, IDC_SR22Date, Driverptr->m_SR22DATE, Driverptr);
///Correction to code to make it work
DDX_DateTimeCtrl(pDX, IDC_SR22Date, Driverptr->m_SR22DATE );
Remove the word "Field" in the DDX function and remove the fourth parameter all together.
Kal
ReplyLoading, Please Wait ...