Date Time Control Example

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 DTP control call specify DATETIMEPICK_CLASS as the window class and register the class by specifying the ICC_DATE_CLASSES bit flag in the accompanying INITCOMMONCONTROLSEX structure.

//date time control example
#include <windows.h>
#include <commctrl.h>
#include <tchar.h>
#pragma comment(lib, "comctl32.lib")
void GetSelectedDate(HWND, HWND,HWND);
HWND DateHandle, TimeHandle,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:
{
// load common control class ICC_DATE_CLASSES from the dynamic-link library (DLL).
INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_DATE_CLASSES;
InitCommonControlsEx(&icex);
//Create date time control displaying the current date
DateHandle = CreateWindowEx(0,DATETIMEPICK_CLASS,"DateTime",WS_BORDER|WS_CHILD|WS_VISIBLE|DTS_SHOWNONE,20,50,220,25,hwnd,NULL,NULL,NULL);
//Create date time control displaying the current time
TimeHandle = CreateWindowEx (0, DATETIMEPICK_CLASS, TEXT("DateTime"),WS_BORDER|WS_CHILD|WS_VISIBLE| DTS_TIMEFORMAT, 20, 10,90, 25, (HWND)hwnd, NULL, NULL, NULL);
//create static boz
hStat = CreateWindow("static", "", WS_CHILD | WS_VISIBLE, 20, 130, 280, 30,hwnd, NULL, NULL, NULL);
ShowWindow(DateHandle, SW_SHOW);
}

case WM_NOTIFY:
//detects change in data time control
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;
}

//update static window with current data time
void GetSelectedDate(HWND hMonthCal, HWND TimeHandle,HWND hStat) {

SYSTEMTIME time;
const int dsize = 80;
TCHAR buf[dsize];
TCHAR date[dsize];

ZeroMemory(&time, sizeof(SYSTEMTIME));
SendMessage(hMonthCal, DTM_GETSYSTEMTIME, 0, (LPARAM) &time);

size_t cbDest = dsize * sizeof(wchar_t);
wsprintf(date,TEXT("Selected date: %i-"), time.wDay);
wsprintf(buf,TEXT("%i-"), time.wMonth);
_tcscat(date,buf);
wsprintf(buf,TEXT("%i"), time.wYear);
_tcscat(date,buf);
SendMessage(TimeHandle, DTM_GETSYSTEMTIME, 0, (LPARAM) &time);
wsprintf(buf,TEXT("\nSelected time %i-"), time.wHour);
_tcscat(date,buf);
wsprintf(buf,TEXT( "%i-"), time.wMinute);
_tcscat(date,buf);
wsprintf(buf,TEXT( "%i"), time.wSecond);
_tcscat(date,buf);
SetWindowText(hStat,date);
}