A month calendar is primarily a Windows control. It gets its primary properties from the CMonthCalCtrl class. Based on this, when creating it, you can specify its location, its size, and other characteristics. When the user clicks the calendar control, one date is selected. As mentioned in our description, you can give the control the ability to display more than one month. To make this possible, when visually creating the control, set its width to have enough space. In the same way, you can increase the height to display many months. This allows you to display as many months as you judge necessary. Here is an example:
At any time, a particular date is selected and has an ellipse with the same color as the background of the title bar. By default, the selected date is today's date. When the user clicks the calendar, a date is selected. To find out what date the user selected, you can access the CMonthCalCtrl::GetCurSel() member function. It is overloaded in three versions whose syntaxes are: BOOL GetCurSel(COleDateTime& refDateTime) const; BOOL GetCurSel(CTime& refDateTime) const; BOOL GetCurSel(LPSYSTEMTIME pDateTime) const; Here is an example: void CExercise1Dlg::OnRetrieveBtn() { // TODO: Add your control notification handler code here UpdateData(); CTime tme = this->m_dtpCurrent.GetCurrentTime(); this->m_Result.Format("%s", tme.Format("%A, %B %d, %Y")); UpdateData(FALSE); } When the date of a month calendar has been changed, the control sends an MCN_SELCHANGE message. The event of this message is applied through a structure named NMSELCHANGE: typedef struct tagNMSELCHANGE { NMHDR nmhdr; SYSTEMTIME stSelStart; SYSTEMTIME stSelEnd; } NMSELCHANGE, *LPNMSELCHANGE;
When the user clicks the month calendar control, one date is selected. To control whether the user can select one or more dates, at design time, set the value of the Multi Select property accordingly. For example, if you want the user to select a range of dates on the control, set the Muilti Select property to true. The month calendar may appear as follows: To programmatically allow the user to select more than one day, apply the MCS_MULTISELECT style: BOOL CExercise1Dlg::OnInitDialog() { CDialog::OnInitDialog(); // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here ctlCalendar->Create(WS_TABSTOP | WS_CHILD | WS_VISIBLE | WS_BORDER | MCS_NOTODAYCIRCLE | MCS_MULTISELECT, CPoint(20, 20), this, 0x224); return TRUE; // return TRUE unless you set the focus to a control } With this property set, the user can select many days in the calendar. You also can programmatically select a range of days. To to this, you can call the CMonthCalCtrl::SetSelRange() member function. It comes in three versions whose syntaxes are: BOOL SetSelRange(const COleDateTime& pMinRange, const COleDateTime& pMaxRange); BOOL SetSelRange(const CTime& pMinRange, const CTime& pMaxRange); BOOL SetSelRange(const LPSYSTEMTIME pMinRange, const LPSYSTEMTIME pMaxRange); After the user has selected a range date on the
calendar, to find out what that range is, you can call the
CMonthCalCtrl::GetSelRange() member function. BOOL SetRange(const COleDateTime* pMinRange, const COleDateTime* pMaxRange); BOOL SetRange(const CTime* pMinRange, const CTime* pMaxRange); BOOL SetRange(const LPSYSTEMTIME pMinRange, const LPSYSTEMTIME pMaxRange); The first argument, nMinRange is the starting date of the desired range. The second argument, nMaxRange, is the end desired date the user can navigate to.
Under the title bar, the short names of week days display, using the format set in the Control Panel. In US English, the first day of the week is Sunday. If you want to start with a different day, call the SetFirstDayOfWeek() member function. Its syntax is: BOOL SetFirstDayOfWeek(int iDay, int* lpnOld = NULL); The first argument, and the only one required, must be an integer of the following values:
If the calendar is already functioning, to find what its first day of the week is, you can call the GetFirstDayOfWeek() member function. Its syntax is: iint GetFirstDayOfWeek(BOOL* pbLocal = NULL) const; This member function returns an integer that is one of the values in the above table.
The month calendar control is used to let the user know today's date in two ways. On the calendar, today's date is circled by a hand-drawn ellipse. In the bottom section of the calendar, today's date is also displayed as a sentence. At design time, to display or hide the indication of today's date, use the No Today property. If it is checked or set to True, which is the default in MFC, a circled label would not appear at the bottom of the control. If it is unchecked or set to False, a circled label would not appear at the bottom of the control as seen in the previous screenshots. As mentioned already, by default, the today label at the bottom of the control appears. To programmatically hide the today label, you can apply the MCS_NOTODAY style. Here is an example: BOOL CExercise1Dlg::OnInitDialog() { CDialog::OnInitDialog(); // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here ctlCalendar->Create(WS_TABSTOP | WS_CHILD | WS_VISIBLE | WS_BORDER | MCS_NOTODAY, CPoint(20, 20), this, 0x224); return TRUE; // return TRUE unless you set the focus to a control } We also mentioned that today date appears with a circle. If you want to hide just the circle, apply the MCS_NOTODAYCIRCLE style: BOOL CExercise1Dlg::OnInitDialog() { CDialog::OnInitDialog(); // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here ctlCalendar->Create(WS_TABSTOP | WS_CHILD | WS_VISIBLE | WS_BORDER | MCS_NOTODAYCIRCLE, CPoint(20, 20), this, 0x224); return TRUE; // return TRUE unless you set the focus to a control }
To make it a highly visual object, a calendar uses different colors to represent the background, week days, the background of the title bar, the text of the title bar, the text of the days of the previous month, and the text of the days of the subsequent month. Of course, you can programmatically change these colors. Although any color is allowed in any category, you should make sure that the calendar is still reasonably appealing and usable. To change the colors of the month calendar control, you can call the CMonthCalCtrl::SetColor() member function. Its syntax is: COLORREF SetColor(int nRegion, COLORREF ref);
By default, the title of the month calendar control appears on top of a blue background. If you want a different color, pass the nRegion argument as MCSC_TITLEBK, and pass the color of your choice as the ref argument. Here is an example:
public:
CMonthCalCtrl m_MonthCalendar;
afx_msg void OnBnClickedMonthCal();
};
---------------------------- . . . No Change void CExerciseDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); DDX_Control(pDX, IDC_MONTH_CALENDAR, m_MonthCalendar); } BEGIN_MESSAGE_MAP(CExerciseDlg, CDialogEx) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_MONTH_CAL, &CExerciseDlg::OnBnClickedMonthCal) END_MESSAGE_MAP() // CExerciseDlg message handlers . . . No Change void CExerciseDlg::OnBnClickedMonthCal() { // TODO: Add your control notification handler code here m_MonthCalendar.SetColor(MCSC_TITLEBK, RGB(205, 50, 0)); }
By default, the labels on the title bar display in a white color. To change the color used to paint the text of the labels, you can pass the nRegion argument as MCSC_TITLETEXT, and pass the color of your choice as the ref argument. Here is an example: void CExerciseDlg::OnBnClickedMonthCal()
{
// TODO: Add your control notification handler code here
m_MonthCalendar.SetColor(MCSC_TITLEBK, RGB(205, 50, 0));
m_MonthCalendar.SetColor(MCSC_TITLETEXT, RGB(255, 255, 45));
}
The names of weekdays use the same color as the color set when passing the MCSC_TITLETEXT value as the nRegion argument to the SetColor() member function. Under the names of the week, there is a horizontal line used as the separator. By default, this line separator is painted in black but it uses the same color as the numeric values of the days of the selected month. Under the line separator, the numeric days of the month are listed. By default, the numeric days of the control display above a white background which is the Window system color. To change the background color of the area where the numeric days appear, pass the nRegion of the SetColor() member function as MCSC_MONTHBK, and pass the color of your choice as the ref argument. Here is an example: void CExerciseDlg::OnBnClickedMonthCal()
{
// TODO: Add your control notification handler code here
m_MonthCalendar.SetColor(MCSC_TITLEBK, RGB(205, 50, 0));
m_MonthCalendar.SetColor(MCSC_TITLETEXT, RGB(255, 255, 45));
m_MonthCalendar.SetColor(MCSC_MONTHBK, RGB(245, 225, 220));
}
The numbers of the days of the month display in two colors. The real days of the selected month display, by default, in a black color as the WindowText system color. To change this color, pass the nRegion of the SetColor() member function as MCSC_TRAILINGTEXT, and pass the color of your choice as the ref argument. Here is an example: void CExerciseDlg::OnBnClickedMonthCal()
{
// TODO: Add your control notification handler code here
m_MonthCalendar.SetColor(MCSC_TITLEBK, RGB(205, 50, 0));
m_MonthCalendar.SetColor(MCSC_TITLETEXT, RGB(255, 255, 45));
m_MonthCalendar.SetColor(MCSC_TRAILINGTEXT, RGB(0, 205, 255));
}
Under the names of the week and their line separator, the numeric days of the month are listed. These days display in a different color. To specify the color of the days of the current month, pass the nRegion of the SetColor() member function as MCSC_TEXT, and pass the color of your choice as the ref argument. Here is an example: void CExerciseDlg::OnBnClickedMonthCal()
{
// TODO: Add your control notification handler code here
m_MonthCalendar.SetColor(MCSC_TITLEBK, RGB(205, 50, 0));
m_MonthCalendar.SetColor(MCSC_TITLETEXT, RGB(255, 255, 45));
m_MonthCalendar.SetColor(MCSC_TEXT, RGB(15, 5, 255));
m_MonthCalendar.SetColor(MCSC_TRAILINGTEXT, RGB(0, 205, 255));
}
|
|
|||||||||||||||||||||||||||||||||||||||||||||||
|