How to Create a Date and Time Picker Control
This topic demonstrates how to dynamically create a date and time picker (DTP) control. The accompanying C++ code example creates a DTP control in a modeless dialog box. It uses the DTS_SHOWNONE style to enable the user to simulate deactivation of the date within the control.
What you need to know
Technologies
Prerequisites
- C/C++
- Windows User Interface Programming
Instructions
Step 1:
Register the window class by calling the InitCommonControlsEx function and specifying the ICC_DATE_CLASSES bit in the accompanying INITCOMMONCONTROLSEX structure.
INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(icex);
icex.dwICC = ICC_DATE_CLASSES;
InitCommonControlsEx(&icex);
Step 2:
To create the DTP control, use the CreateWindowEx function. Specify DATETIMEPICK_CLASS as the window class, and pass the handle to the parent dialog box.
The following C++ code example uses the CreateDialog function to create a modeless dialog box. It then calls CreateWindowEx to create the DTP control.
hwndDlg = CreateDialog (g_hinst,
MAKEINTRESOURCE(IDD_DIALOG1),
hwndMain,
DlgProc);
if(hwndDlg)
hwndDP = CreateWindowEx(0,
DATETIMEPICK_CLASS,
TEXT("DateTime"),
WS_BORDER|WS_CHILD|WS_VISIBLE|DTS_SHOWNONE,
20,50,220,20,
hwndDlg,
NULL,
g_hinst,
NULL);
Complete example
// CreateDatePick creates a DTP control within a dialog box.
// Returns the handle to the new DTP control if successful, or NULL
// otherwise.
//
// hwndMain - The handle to the main window.
// g_hinst - global handle to the program instance.
HWND WINAPI CreateDatePick(HWND hwndMain)
{
HWND hwndDP = NULL;
HWND hwndDlg = NULL;
INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(icex);
icex.dwICC = ICC_DATE_CLASSES;
InitCommonControlsEx(&icex);
hwndDlg = CreateDialog (g_hinst,
MAKEINTRESOURCE(IDD_DIALOG1),
hwndMain,
DlgProc);
if(hwndDlg)
hwndDP = CreateWindowEx(0,
DATETIMEPICK_CLASS,
TEXT("DateTime"),
WS_BORDER|WS_CHILD|WS_VISIBLE|DTS_SHOWNONE,
20,50,220,20,
hwndDlg,
NULL,
g_hinst,
NULL);
return (hwndDP);
}
Related topics