Skip to content

Common Controls - Calendar

A dialog demo for the date and time picker common controls, with the full ICC_ flag reference.

Published Nov 24, 2000

Shows how to bring up the SysMonthCal and time-picker common controls in a dialog. The important part is InitCommonControlsEx with ICC_DATE_CLASSES before any such control is created; the big comment block is my reference card of every ICC_ flag and which control classes each one loads, which saves a trip to the docs every time. The dialog procedure sets both pickers to the current system time on init and reads them back with MCM_GETCURSEL/TCM_GETCURSEL when OK is pressed.

#include <Windows.H>
#include <WindowsX.H>

#include <Windows.H>
#include <WindowsX.H>
#include <CommCtrl.H>
#include <Wincon.h>
#include <ShellAPI.H>
#include <Stdio.H>
#include <Stdlib.H>
//---------------------------------------------------------------------------

/* Library Headers Begin */
#include "Headers/NSWFL.H"
/* Library Headers End */

#include "Headers/Definitions.H"
#include "Headers/Functions.H"
#include "Dialogs/MainDialog.H"
//---------------------------------------------------------------------------

/////////////////////////////////////////////////////////////////////////////

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevious, LPSTR Cmd , int CmdShow)
{
	MainInstance = hInstance;

    /*
    ICC_ANIMATE_CLASS
    Load animate control class.

    ICC_BAR_CLASSES
    Load toolbar, status bar, trackbar, and ToolTip control classes.

    ICC_COOL_CLASSES
    Load rebar control class.

    ICC_DATE_CLASSES
    Load date and time picker control class.

    ICC_HOTKEY_CLASS
    Load hot key control class.

    ICC_INTERNET_CLASSES
    Load IP address class.

    ICC_LINK_CLASS
    Load a hyperlink control class.

    ICC_LISTVIEW_CLASSES
    Load list-view and header control classes.

    ICC_NATIVEFNTCTL_CLASS
    Load a native font control class.

    ICC_PAGESCROLLER_CLASS
    Load pager control class.

    ICC_PROGRESS_CLASS
    Load progress bar control class.

    ICC_STANDARD_CLASSES
    Load one of the intrinsic User32 control classes. The user controls include button, edit, static, listbox, combobox, and scrollbar.

    ICC_TAB_CLASSES
    Load tab and ToolTip control classes.

    ICC_TREEVIEW_CLASSES
    Load tree-view and ToolTip control classes.

    ICC_UPDOWN_CLASS
    Load up-down control class.

    ICC_USEREX_CLASSES
    Load ComboBoxEx class.

    ICC_WIN95_CLASSES
    Load animate control, header, hot key, list-view, progress bar, status bar, tab, ToolTip, toolbar, trackbar, tree-view, and up-down control classes.
*/
    INITCOMMONCONTROLSEX ICEX;

    ICEX.dwSize = sizeof(ICEX);
    ICEX.dwICC  = ICC_DATE_CLASSES;

    InitCommonControlsEx(&ICEX);

	DialogBox(MainInstance,MAKEINTRESOURCE(100),0,MainDialog);

	return 0;
}
BOOL CALLBACK MainDialog(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	static HWND Date_hWnd;
	static HWND Time_hWnd;

	for(;;)
	{
		if(Msg==WM_INITDIALOG)
		{
			MainDialog_hWnd = hDlg;

			SendMessage(MainDialog_hWnd, WM_SETTEXT, FALSE,(LPARAM)TitleCaption);
			SendMessage(MainDialog_hWnd, WM_SETICON, TRUE,(LPARAM)LoadIcon(GetWindowInstance(MainDialog_hWnd), MAKEINTRESOURCE(1)));

			Date_hWnd = GetDlgItem(MainDialog_hWnd, 1000);
            Time_hWnd = GetDlgItem(MainDialog_hWnd, 2000);

            SYSTEMTIME STD;
            GetSystemTime(&STD);
            SendMessage(Date_hWnd, TCM_SETCURSEL, (WPARAM) 0, (LPARAM) (LPSYSTEMTIME) &STD);

            SYSTEMTIME STT;
            GetSystemTime(&STT);
            SendMessage(Time_hWnd, TCM_SETCURSEL, (WPARAM) 0, (LPARAM) (LPSYSTEMTIME) &STT);

			CenterWindow(MainDialog_hWnd);
			break;
		}

		if(Msg==WM_COMMAND)
		{
			if(wParam==100) // Ok Button
			{
                //-------------

                char *Date = NULL;
                char *Time = NULL;

                //-------------

                SYSTEMTIME STD;
                SYSTEMTIME STT;

                /*
                typedef struct _SYSTEMTIME {
                    WORD wYear;
                    WORD wMonth;
                    WORD wDayOfWeek;
                    WORD wDay;
                    WORD wHour;
                    WORD wMinute;
                    WORD wSecond;
                    WORD wMilliseconds;
                } SYSTEMTIME;
                */

                //-------------

                SendMessage(Date_hWnd, MCM_GETCURSEL, (WPARAM) 0, (LPARAM) (LPSYSTEMTIME) &STD);

                SendMessage(Time_hWnd, TCM_GETCURSEL, (WPARAM) 0, (LPARAM) (LPSYSTEMTIME) &STT);

                //-------------

                Date = Get_Text(Date_hWnd);
                Time = Get_Text(Time_hWnd);

                MessageBox (NULL, Date , TitleCaption, 0 + MB_ICONASTERISK + MB_SYSTEMMODAL);
                MessageBox (NULL, Time , TitleCaption, 0 + MB_ICONASTERISK + MB_SYSTEMMODAL);

                //-------------
			}

			if(wParam==200) // Exit Button
			{
				EndDialog(MainDialog_hWnd,0);
			}
		}

		if(Msg==WM_CLOSE)
		{
			EndDialog(MainDialog_hWnd,0);
		}

		break;
	}
	return 0;
}
An unhandled error has occurred. Reload 🗙