Detailed description of the use of the calendar control in Visual C ++

Source: Internet
Author: User
Tags month name
Preface:

Controls play an important role in Visual C ++ programming learning. I have read this article about calendar control learning on a foreign English website. Although the content seems simple, I still learned something after reading it. I feel that the author of the original article is very detailed and easy to understand about the use of some of the less commonly used calendar control methods. It is amazing in the plain, and it is a "deep mining" for the calendar control ", it should be very helpful for beginners of VC, so they will translate it and introduce it to domestic readers and friends.

  1. Introduction to calendar controls

Win32 API provides a date display color calendar control. The date display mode is based on the regional settings in the control panel or the user's operating system. The calendar controls are as follows:


Figure 1 calendar display Effect

The title bar of this commonly used calendar control contains two buttons and two labels. The button on the left allows you to click to select the previous month, and the label on the left shows the selected month, the label on the right shows the year in the current date. The button on the right allows you to select the next month. The calendar control can be configured to display multiple months, which is a specific example:


Figure 2. Calendar Control for multiple months

If you want the calendar control to display multiple months, the number of buttons will be increased or decreased through the list of the previous month or the next month. For example, if the widget is displaying July 15, April or July 15, May, you click the button on the left to display July 15, March or July 15, April. If you click the button on the right, the widget displays July 15, May and July 15, June. In addition, if you select any month in the current year, you can click the month name in the title box. A month list is displayed for you to select. Details:


Figure 3. display the month list

If you want to change the year, you can click the year label and the rotate button is displayed. You can change the year by clicking the up or down button of the rotate button, you can also use the up and down arrows on the keyboard to change the year.


Figure 4. Change the year of the calendar Control

Under the title bar, the abbreviation of the week is displayed in the control panel format. In the English area, the first day of each week is usually Sunday. Developers can change the first day settings in the control.

Control, the selected date has a circle. You can click the date to select a date in the control. In most regions, dates are displayed as numbers on a white background (the background color and any color on the control can be changed through a program ). By default, the control displays the current date of an elliptical circle. You can use the title bar button, month, and year labels to change the date. If the current date is not displayed, you can click the label at the bottom of the control to display the current date. (If you are a programmer, you can hide the label at the bottom of the control that shows today's date ).

  2. Create a calendar Control

You can create calendar controls in windows, dialog frameworks, toolbar, and any other container windows. The calendar control corresponds to the CmonthCalCtrl class. to dynamically create a calendar control, you must declare a CmonthCalCtrl variable or pointer to CmonthCalCtrl. The Code is as follows:

// Exercise1Dlg. h: header file
Class CExercise1Dlg: public CDialog
{
// Construction
Public:
CExercise1Dlg (CWnd * pParent = NULL); // standard constructor
Private:
CMonthCalCtrl * ctlCalendar;
};

Like the classes corresponding to other controls in MFC, The CmonthCalCtrl class provides a Create () method to dynamically Create a calendar control in the container window. The Code is as follows:

CExercise1Dlg: CExercise1Dlg (CWnd * pParent/* = NULL */)
: CDialog (CExercise1Dlg: IDD, pParent)
{
// {AFX_DATA_INIT (CExercise1Dlg)
//} AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
M_hIcon = AfxGetApp ()-> LoadIcon (IDR_MAINFRAME );
CtlCalendar = new CMonthCalCtrl;
}
//////////////////////////////////////// /////////////////////////////////////
// CExercise1Dlg message handlers
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, CPoint (20, 20), this, 0x224 );
Return TRUE; // return TRUE unless you set the focus to a control
}

3. Calendar control attribute operations

After you create a calendar control in the dialog box or window, it only displays the current month and only one month. This is because, according to the default design, the length, width, and height of a widget can only accommodate one month's date. to display the widget for two months, increase the width of the widget (similarly, you can also increase the height to display two months ).

As a visual object, the calendar control can use different colors to represent the background, Sunday, title text, title background, and so on. Developers can naturally use programs to change these colors. Of course, the control affinity is not affected. To change the color of the calendar control, call the CMonthCalCtrl: SetColor () method. The syntax of this method is:

COLORREF SetColor (int nRegion, COLORREF ref );

By default, the title bar of the control displays a blue background. to change it, you must pass the MCSC_TITLEBK value to the nRegion parameter and the color you want to display to the ref parameter. If you want to change the text color on the title bar, you must pass the MCSC_TITLETEXT value to the nRegion parameter.


Figure 5. Change the title bar color of the control

As mentioned above, Sunday is shown below the title bar. in English-speaking countries, the first day of a week is Sunday. If you want to change the first day of a week, you can call the SetFirstDayOfWeek () function (), its syntax is:

BOOL SetFirstDayOfWeek (int iDay, int * lpnOld = NULL );

The first parameter must be the following INTEGER:

Value Weekday
0 Monday
1 Tuesday
2 Wednesday
3 Thursday
4 Friday
5 Saturday
6 Sunday

If you want to know which day of the calendar control is set to the first day on Sunday, you can call the function: GetFirstDayOfWeek (). Its syntax is:

Int GetFirstDayOfWeek (BOOL * pbLocal = NULL) const;

This function returns an integer with the same meaning as the previous table.

The color used by Sunday name is the same as that used when MCSC_TITLETEXT is passed using SetColor () function. It is a horizontal split line under Sunday, which is black by default, but here it is a color with the selected date. In the split line, the date list is displayed. The default background is Windows white. to change it, you need to pass the MCSC_MONTHBK value to the nRegion parameter and the color you want to display to the ref parameter.

The number of the date is displayed in two colors. The date in the selected month is in black. To change this color, you can pass the MCSC_TRAILINGTEXT value to the nRegion parameter, pass the color you want to display to the ref parameter.


Figure 6. Change the date display color of the calendar Control

The date list under the split line is displayed in two colors. to specify the color of the date in the current month, you can pass the MCSC_TEXT value to the nRegion parameter and the color you want to display to the ref parameter.

The calendar control is used to show users the current date in two forms. One is to circle the current date in an ellipse in the list, and the other is to display the date in a sentence at the bottom. When creating the control, setting the "today" attribute to "NO" will not display the date of today.


Figure 7. Calendar controls that do not display the "Today" tab

As described above, the control displays the current date by default. You can use the MCS_NOTODAY program to hide this label. The Code is as follows:

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 noticed that the current date is still in an elliptical circle. To hide it, use the MCS_NOTODAYCIRCLE type. The Code is as follows:

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 obtain the date selected in the current calendar control, you can use the method CMonthCalCtrl: GetCurSel (). This method has three versions that are reloaded:

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 );
}

To control whether users can select more than two dates, you can set multiple selection attributes when creating the control. For example, if you want the user to select a certain range of dates in the control, you can set multiple selection attributes to true. To dynamically set the multi-date selection, apply the MCS_MULTISELECT attribute, the Code is as follows:

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
}


Figure 8. Calendar control with multiple selected dates

Through attribute settings, you can select multiple dates in the calendar control. Of course, you can also use dynamic programming to select multiple dates. At this time, you can call the CMonthCalCtrl: SetSelRange () method, it has three different versions. The syntax is:

BOOL SetSelRange (const COleDateTime & pMinRange, const COleDateTime & pMaxRange );
BOOL SetSelRange (const CTime & pMinRange, const CTime & pMaxRange );
BOOL SetSelRange (const LPSYSTEMTIME pMinRange, const LPSYSTEMTIME pMaxRange );

To obtain the selectable range of a calendar control, call the CMonthCalCtrl: GetSelRange () method.

To control the date range that users can select, you can call the CMonthCalCtrl: SetRange () method, which also has three different forms:

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 parameter nMinRange is the start date of the selection range, and the parameter nMaxRange is the maximum date available for selection.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.