Skip to content

Calendar Control

A standalone Win32 program that creates a month calendar common control in its own window.

Published Sep 1, 1995

The full recipe for using the MONTHCAL_CLASS common control outside of a dialog: register your own window class, call InitCommonControlsEx with ICC_DATE_CLASSES so the calendar class is loaded, create the calendar as a child window, and wire up a File/Exit menu. It also includes a FindFirstInstance helper that uses FindWindow to keep only one copy of the app running. Use it when you need a date picker embedded in a plain window rather than a dialog template.

#include <windows.h>     // Windows Specific Header File
#include <stdio.h>
#include <stdlib.h>

#define ID_Calendar1 101
#define ID_Edit 102
#define ID_Exit 103

static  INITCOMMONCONTROLSEX  icex;
static  HANDLE  hInstance;
static  MSG  Msg;
static  HANDLE  Form1;
static  HANDLE  Calendar1;
static  char   AppName [2048];
static  char   Caption [2048];
static  HANDLE  MainMenu;
static  HANDLE  FileMenu;

extern int WINAPI WinMain (HWND, HWND, char *, int );
extern LRESULT CALLBACK WndProc (HANDLE, UINT, UINT, int );
extern void FormLoad (HANDLE);
extern int FindFirstInstance (char *);

extern int WINAPI WinMain (HWND hInst, HWND hPrev, char *CmdParam, int CmdShow)
{
	static  WNDCLASS  Wc;
	memset(&Wc,0,sizeof(Wc));
	sprintf(AppName,"%s","CalendarDemo");
	hInstance=hInst;
	if(FindFirstInstance(AppName))
	{
		return 0;
	}
	Wc.style=CS_HREDRAW|CS_VREDRAW;
	Wc.lpfnWndProc=WndProc;
	Wc.cbClsExtra=0;
	Wc.cbWndExtra=0;
	Wc.hInstance=hInst;
	Wc.hIcon=LoadIcon(NULL,IDI_WINLOGO);
	Wc.hCursor=LoadCursor(NULL,IDC_ARROW);
	Wc.hbrBackground=GetStockObject(WHITE_BRUSH);
	Wc.lpszMenuName=NULL;
	Wc.lpszClassName=AppName;
	RegisterClass(&Wc);

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

	FormLoad(hInst);

	while(GetMessage(&Msg,NULL,0,0))
	{
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}

	return Msg.wParam;
}


extern LRESULT CALLBACK WndProc (HANDLE hWnd, UINT Msg, UINT wParam, int lParam)
{

	for(;;)
	{
		// ========================================================================
		if(Msg==WM_COMMAND)
		{
			// ========================================================================
			PostQuitMessage(0);
			// ========================================================================
			break;
		}
		if(Msg==WM_DESTROY)
		{
			UnregisterClass(AppName,hInstance);
			PostQuitMessage(0);
			return 0;
			// ========================================================================
		}
		break;
	}
	return DefWindowProc(hWnd,Msg,wParam,lParam);
}

extern void FormLoad (HANDLE hInst)
{
	sprintf(Caption,"%s","Calendar Control");
	Form1=CreateWindow(AppName,Caption,DS_MODALFRAME|WS_POPUP|WS_VISIBLE|WS_CAPTION|WS_SYSMENU,0,0,215,235,NULL,(HMENU)NULL,hInst,NULL);
	Calendar1=CreateWindowEx(WS_EX_TOOLWINDOW,MONTHCAL_CLASS,NULL,WS_CHILD|WS_VISIBLE|WS_CLIPCHILDREN|WS_CLIPSIBLINGS,0,0,200,200,Form1,(HMENU)NULL,hInst,NULL);
	MainMenu=CreateMenu();
	FileMenu=CreateMenu();
	AppendMenu(FileMenu,MF_STRING,ID_Exit,"E&xit");
	InsertMenu(MainMenu,ID_Edit,MF_POPUP,(UINT)FileMenu,"File");
	SetMenu(Form1,MainMenu);
	UpdateWindow(Form1);
	ShowWindow(Form1,1);
}


extern int FindFirstInstance (char *ApplName)
{
	static  HWND  hWnd;
	memset(&hWnd,0,sizeof(hWnd));
	hWnd=FindWindow(ApplName,NULL);
	if(hWnd)
	{
		return   TRUE;
	}
	return FALSE;
}
An unhandled error has occurred. Reload 🗙