Date and time picker (DTP) controls have several styles that determine a control's appearance and behavior using the CreateWindowEx dwStyle parameter. A date and time picker (DTP) control sends notification messages when it receives user input. The parent of the control receives these notification messages in the form of WM_NOTIFY messages. The lParam contains a pointer to an NMHDR structure that contains the notification code and additional information.
To create a Date and Time Picker (DTP) control specify DATETIMEPICK_CLASS as the window class and register the class by specifying the ICC_DATE_CLASSES bit flag in the accompanying INITCOMMONCONTROLSEX structure.
#include <windows.h> #include <commctrl.h> #include <tchar.h> #pragma comment(lib, "comctl32.lib") void GetSelectedDate(HWND, HWND, HWND); HWND DateHandle; HWND TimeHandle; HWND hStat; LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wc; MSG msg; wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.lpszMenuName = NULL; wc.lpszClassName = TEXT("myWindowClass"); wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); RegisterClassEx(&wc); CreateWindowEx( WS_EX_CLIENTEDGE, TEXT("myWindowClass"), TEXT("Date and Time Picker"), WS_VISIBLE | WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 340, 220, NULL, NULL, hInstance, NULL); while (GetMessage(&msg, NULL, 0, 0) > 0) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { LPNMHDR lpNmHdr; switch (msg) { case WM_CREATE: { INITCOMMONCONTROLSEX icex; icex.dwSize = sizeof(INITCOMMONCONTROLSEX); icex.dwICC = ICC_DATE_CLASSES; InitCommonControlsEx(&icex); // Date picker DateHandle = CreateWindowEx( 0, DATETIMEPICK_CLASS, TEXT("DateTime"), WS_CHILD | WS_VISIBLE | WS_BORDER | DTS_SHOWNONE, 20, 50, 220, 25, hwnd, NULL, NULL, NULL); // Time picker TimeHandle = CreateWindowEx( 0, DATETIMEPICK_CLASS, TEXT("DateTime"), WS_CHILD | WS_VISIBLE | WS_BORDER | DTS_TIMEFORMAT, 20, 10, 90, 25, hwnd, NULL, NULL, NULL); // Static output hStat = CreateWindow( TEXT("static"), TEXT(""), WS_CHILD | WS_VISIBLE, 20, 130, 280, 30, hwnd, NULL, NULL, NULL); ShowWindow(DateHandle, SW_SHOW); break; } case WM_NOTIFY: { lpNmHdr = (LPNMHDR)lParam; if (lpNmHdr->code == DTN_DATETIMECHANGE) { GetSelectedDate(DateHandle, TimeHandle, hStat); } break; } case WM_CLOSE: DestroyWindow(hwnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; } void GetSelectedDate(HWND hMonthCal, HWND TimeHandle, HWND hStat) { SYSTEMTIME time; const int dsize = 80; TCHAR buf[dsize]; TCHAR date[dsize]; ZeroMemory(&time, sizeof(SYSTEMTIME)); // Read selected date SendMessage(hMonthCal, DTM_GETSYSTEMTIME, 0, (LPARAM)&time); wsprintf(date, TEXT("Selected date: %d-%d-%d"), time.wDay, time.wMonth, time.wYear); // Read selected time SendMessage(TimeHandle, DTM_GETSYSTEMTIME, 0, (LPARAM)&time); wsprintf(buf, TEXT(" Selected time: %d-%d-%d"), time.wHour, time.wMinute, time.wSecond); _tcscat(date, buf); SetWindowText(hStat, date); }