Setting the Day State of a Month Calendar Control
The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.
The latest version of this topic can be found at Setting the Day State of a Month Calendar Control.
One of the attributes of a month calendar control is the ability to store information, referred to as the day state of the control, for each day of the month. This information is used to emphasize certain dates for the month currently displayed.
Note
The CMonthCalCtrl
object must have the MCS_DAYSTATE style to display day state information.
Day state information is expressed as a 32-bit data type, MONTHDAYSTATE. Each bit in a MONTHDAYSTATE bit field (1 through 31) represents the state of a day in a month. If a bit is on, the corresponding day will be displayed in bold; otherwise it will be displayed with no emphasis.
There are two methods for setting the day state of the month calendar control: explicitly with a call to CMonthCalCtrl::SetDayState or by handling the MCN_GETDAYSTATE notification message.
Handling the MCN_GETDAYSTATE Notification Message
The MCN_GETDAYSTATE message is sent by the control to determine how the days within the visible months should be displayed.
Note
Because the control caches the previous and following months, in respect to the visible month, you will receive this notification every time a new month is chosen.
To properly handle this message, you must determine how many months day state information is being requested for, initialize an array of MONTHDAYSTATE structures with the proper values, and initialize the related structure member with the new information. The following procedure, detailing the necessary steps, assumes that you have a CMonthCalCtrl
object called m_monthcal
and an array of MONTHDAYSTATE objects, mdState
.
To handle the MCN_GETDAYSTATE notification message
Using the Properties window, add a notification handler for the MCN_GETDAYSTATE message to the
m_monthcal
object (see Mapping Messages to Functions).In the body of the handler, add the following code:
LPNMDAYSTATE pDayState = reinterpret_cast<LPNMDAYSTATE>(pNMHDR); int iMax = pDayState->cDayState; for(int i = 0; i < iMax; i++) { pDayState->prgDayState[i] = (MONTHDAYSTATE)0; // init to 0 pDayState->prgDayState[i] |= 0x01 << 14; // set 15th bit to 1 }
The example converts the
pNMHDR
pointer to the proper type, then determines how many months of information are being requested (pDayState->cDayState
). For each month, the current bitfield (pDayState->prgDayState[i]
) is initialized to zero and then the needed dates are set (in this case, the 15th of each month).