Month Calender Control

To create a month calendar control specify MONTHCAL_CLASSW as the window class and register the class by specifying the ICC_DATE_CLASSES bit flag in the accompanying INITCOMMONCONTROLSEX structure.

When an event occurs in the month calendar control, the WM_NOTIFY message is sent to the parent window. The lParam contains a pointer to an NMHDR structure that contains the notification code and additional information.


//month calender control example
#include <windows.h>
#include <commctrl.h>
#include <tchar.h>
#pragma comment(lib, "comctl32.lib")

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void CreateControls(HWND);
void GetSelectedDate(HWND, HWND);

HWND hStat;
HWND hMonthCal;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow) {

HWND hwnd;
MSG msg;
WNDCLASS wc = {0};
wc.lpszClassName = TEXT("myWindowClass");
wc.hInstance = hInstance ;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc ;
wc.hCursor = LoadCursor(0, IDC_ARROW);
RegisterClass(&wc);
hwnd = CreateWindow(wc.lpszClassName, TEXT("Calendar Control"),WS_OVERLAPPEDWINDOW | WS_VISIBLE,100, 100, 700, 300, 0, 0, hInstance, 0);
while (GetMessage(&msg, NULL, 0, 0)) {
DispatchMessage(&msg);
}
return (int) msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
LPNMHDR lpNmHdr;
switch(msg) {
case WM_CREATE:
CreateControls(hwnd);
break;
case WM_NOTIFY:
lpNmHdr = (LPNMHDR) lParam;
//MCN_SELECT is sent by a month calendar control when the user makes an explicit date selection
if (lpNmHdr->code == MCN_SELECT) {
GetSelectedDate(hMonthCal, hStat);
}

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

void CreateControls(HWND hwnd) {
// load common control class ICC_USEREX_CLASSES from the dynamic-link library (DLL).
INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(icex);
icex.dwICC = ICC_DATE_CLASSES;
InitCommonControlsEx(&icex);
//create static control
hStat = CreateWindow(TEXT("static"), TEXT(""), WS_CHILD | WS_VISIBLE, 100, 230, 280, 30,hwnd, NULL, NULL, NULL);
//create calender control
hMonthCal = CreateWindow(MONTHCAL_CLASS, TEXT(""), WS_BORDER | WS_CHILD | WS_VISIBLE | MCS_NOTODAYCIRCLE, 100, 20, 200, 200, hwnd, NULL, NULL, NULL);
}

//copy selected value into static control
void GetSelectedDate(HWND hMonthCal, HWND hStat) {
SYSTEMTIME time;
const int dsize = 40;
TCHAR buf[dsize];
TCHAR date[dsize];
ZeroMemory(&time, sizeof(SYSTEMTIME));
SendMessage(hMonthCal, MCM_GETCURSEL, 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);
SetWindowText(hStat,date);
}