Month Calender Control Example

To create a Month Calendar control specify MONTHCAL_CLASS 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.

#include <windows.h> #include <commctrl.h> #include <tchar.h> #pragma comment(lib, "comctl32.lib") #define ID_MONTHCAL 100 #define ID_STATIC 101 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; ZeroMemory(&wc, sizeof(WNDCLASS)); wc.lpszClassName = TEXT("myWindowClass"); wc.hInstance = hInstance; wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE); wc.lpfnWndProc = WndProc; wc.hCursor = LoadCursor(NULL, IDC_ARROW); RegisterClass(&wc); hwnd = CreateWindow( wc.lpszClassName, TEXT("Calendar Control"), WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 700, 300, NULL, NULL, hInstance, NULL ); while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_CREATE: CreateControls(hwnd); break; case WM_NOTIFY: { LPNMHDR lpNmHdr; lpNmHdr = (LPNMHDR)lParam; if(lpNmHdr->idFrom == ID_MONTHCAL && lpNmHdr->code == MCN_SELECT) { GetSelectedDate(hMonthCal,hStat); } } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd,msg,wParam,lParam); } return 0; } void CreateControls(HWND hwnd) { INITCOMMONCONTROLSEX icex; icex.dwSize = sizeof(INITCOMMONCONTROLSEX); icex.dwICC = ICC_DATE_CLASSES; InitCommonControlsEx(&icex); hStat = CreateWindow( TEXT("STATIC"), TEXT(""), WS_CHILD | WS_VISIBLE, 100, 230, 280, 30, hwnd, (HMENU)ID_STATIC, NULL, NULL ); hMonthCal = CreateWindow( MONTHCAL_CLASS, TEXT(""), WS_BORDER | WS_CHILD | WS_VISIBLE | MCS_NOTODAYCIRCLE, 100, 20, 200, 200, hwnd, (HMENU)ID_MONTHCAL, NULL, NULL ); } void GetSelectedDate(HWND hMonthCal, HWND hStat) { SYSTEMTIME time; TCHAR buf[50]; TCHAR date[100]; ZeroMemory(&time,sizeof(SYSTEMTIME)); SendMessage( hMonthCal, MCM_GETCURSEL, 0, (LPARAM)&time ); 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 ); }