Udostępnij za pośrednictwem


Accessing the Embedded Month Calendar Control

The embedded month calendar control object can be accessed from the CDateTimeCtrl object with a call to the GetMonthCalCtrl member function.

Note

The embedded month calendar control is used only when the date and time picker control does not have the DTS_UPDOWN style set.

This is useful if you want to modify certain attributes before the embedded control is displayed. To accomplish this, handle the DTN_DROPDOWN notification, retrieve the month calendar control (using CDateTimeCtrl::GetMonthCalCtrl), and make your modifications. Unfortunately, the month calendar control is not persistent.

In other words, when the user requests the display of the month calendar control, a new month calendar control is created (before the DTN_DROPDOWN notification). The control is destroyed (after the DTN_CLOSEUP notification) when dismissed by the user. This means that any attributes you modify, before the embedded control is displayed, are lost when the embedded control is dismissed.

The following example demonstrates this procedure, using a handler for the DTN_DROPDOWN notification. The code changes the background color of the month calendar control, with a call to SetMonthCalColor, to gray. The code is as follows:

void CMyDialog::OnDtnDropdownDatetimepicker1(NMHDR *pNMHDR, LRESULT *pResult)
{
   UNREFERENCED_PARAMETER(pNMHDR);

   //set the background color of the month to gray
   COLORREF clr= RGB(100, 100, 100);

   m_DateTimeCtrl.SetMonthCalColor(MCSC_MONTHBK, clr);

   *pResult = 0;
}

As stated previously, all modifications to properties of the month calendar control are lost, with two exceptions, when the embedded control is dismissed. The first exception, the colors of the month calendar control, has already been discussed. The second exception is the font used by the month calendar control. You can modify the default font by making a call to CDateTimeCtrl::SetMonthCalFont, passing the handle of an existing font. The following example (where m_dtPicker is the date and time control object) demonstrates one possible method:

//create and initialize the font to be used
LOGFONT logFont = {0};
logFont.lfHeight = -12;
logFont.lfWeight = FW_NORMAL;
logFont.lfCharSet = DEFAULT_CHARSET;
 _tcscpy_s(logFont.lfFaceName, _countof(logFont.lfFaceName), 
           _T("Verdana"));

m_MonthCalFont.CreateFontIndirect(&logFont);
m_DateTimeCtrl.SetMonthCalFont(m_MonthCalFont);

Once the font has been changed, with a call to CDateTimeCtrl::SetMonthCalFont, the new font is stored and used the next time a month calendar is to be displayed.

See Also

Concepts

Controls (MFC)

Reference

Using CDateTimeCtrl