Datepicker control

Datepicker
Datepicker

This is a sample of a window with date and time picker control. Just in case you might need it. In the DTN_DATETIMECHANGE message you'll be notified when the user selects a date or time. Download the solution(MS Visual Studio.NET)
#define   STRICT

#include  <windows.h>
#include  <stdio.h>
#include  <commctrl.h>

#pragma comment(lib, "comctl32.lib")

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
   static SYSTEMTIME st;
   static HWND       hWndStartDate, hWndStartTime;
   static char       szDate[15], szTime[10], szStamp[20];
   NMDATETIMECHANGE  *Date;
   CREATESTRUCT      *cs;

   switch (iMsg)
   {
   case WM_CREATE :
      cs = (LPCREATESTRUCT)lParam;
      GetLocalTime(&st);
      GetDateFormat(LOCALE_SYSTEM_DEFAULT, 0, &st, "yyyyMMdd", szDate, sizeof(szDate));
      GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, &st, "HHmmss", szTime, sizeof(szTime));

      hWndStartDate = CreateWindowEx(0, DATETIMEPICK_CLASS, NULL,
         WS_BORDER | WS_CHILD | WS_VISIBLE,
         10, 10, 100, 25,
         hwnd, NULL, cs->hInstance, NULL);

      hWndStartTime = CreateWindowEx(0, DATETIMEPICK_CLASS, NULL,
         WS_BORDER | WS_CHILD | WS_VISIBLE | DTS_TIMEFORMAT,
         10, 40, 100, 25,
         hwnd, NULL, cs->hInstance, NULL);
      break;

   case WM_NOTIFY :
      switch(((LPNMHDR)lParam)->code)
      {
      case DTN_DATETIMECHANGE :
         Date = (LPNMDATETIMECHANGE)lParam;
         if(Date->nmhdr.hwndFrom == hWndStartDate || Date->nmhdr.hwndFrom == hWndStartTime)
         {
            DateTime_SetRange(hWndStartDate, GDTR_MAX, &st);
            DateTime_SetRange(hWndStartTime, GDTR_MAX, &st);
         }
         break;

      case DTN_DROPDOWN :
         MonthCal_SetFirstDayOfWeek(DateTime_GetMonthCal(hWndStartDate), 0);
         break;
      }
      break;

   case WM_COMMAND :
      if (IDCANCEL == LOWORD(wParam))
      {  
         DateTime_GetSystemtime(hWndStartDate, &st);
         DateTime_GetSystemtime(hWndStartTime, &st);
         break;
      }
      break;

   case WM_CLOSE :
      DestroyWindow(hwnd);
      break;

   case WM_DESTROY :
      PostQuitMessage(0);
      break;
   }
   return DefWindowProc(hwnd, iMsg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
   MSG                  msg;
   WNDCLASS             wndclass;
   HWND                 hwnd;
   INITCOMMONCONTROLSEX icex;
   char                 szAppName[] = "DatePickerApp";

   wndclass.style         = 0;
   wndclass.lpfnWndProc   = WndProc;
   wndclass.cbClsExtra    = 0;
   wndclass.cbWndExtra    = 0;
   wndclass.hInstance     = hInstance;
   wndclass.hIcon         = LoadIcon(hInstance, szAppName);
   wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
   wndclass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
   wndclass.lpszMenuName  = szAppName;
   wndclass.lpszClassName = szAppName;
   RegisterClass(&wndclass);

   icex.dwSize = sizeof(icex);
   icex.dwICC = ICC_DATE_CLASSES;
   InitCommonControlsEx(&icex);

   hwnd = CreateWindow(szAppName,
      szAppName,
      WS_OVERLAPPEDWINDOW | WS_VISIBLE,
      0, 0, 400, 400,
      NULL, NULL, hInstance, szCmdLine);

   while (GetMessage(&msg, NULL, 0, 0))
   {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
   }
   return (int)msg.wParam;
}
Articles
info@win32apicode.com