Use the Date/Time Picker common control (IE3+)

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

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);
}

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read