Share via


Setting the Day State of a Month Calendar Control

OverviewSample

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 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

  1. Using ClassWizard, add a notification handler for the MCN_GETDAYSTATE message to the m_monthcal object.

  2. In the body of the handler, add the following code:

    NMDAYSTATE* pDayState= (NMDAYSTATE *)pNMHDR;
    
    #define BOLDDAY(ds,iDay) if(iDay>0 && iDay<32)\
                                (ds)|=(0x00000001<<(iDay-1))
    MONTHDAYSTATE mdState[3];
    INT i, iMax;
    

    The example converts the pNMHDR pointer to the proper type and defines a helpful macro that sets specific bits in the MONTHDAYSTATE bitfield.

  3. After adding the previous code example, add the following code:

    iMax=pDayState->cDayState;
    
    for(i=0;i<iMax;i++)
    {
        mdState[i] = (MONTHDAYSTATE)0;
        BOLDDAY(mdState[i],15);
     }
    pDayState->prgDayState = mdState;
    

    The example first determines how many months of information are being requested (pDayState->cDayState). For each month, the current bitfield (mdState) is initialized to zero and then the needed dates are set (in this case, the 15th of each month). Finally, the pDayState->prgDayState member is set, completing the MCN_DAYSTATE handler.

See Also   Windows Common Controls and MFC Classes