Advanced Common Controls
In addition to the standard common controls, Windows includes a set of advanced common controls, including the date and time picker, the month calendar, the internet protocol address control, and the extended combo box control. Example Ex08b uses each of these common controls. Figure 8-2 shows the dialog box from that example. You can refer back to that example as you read the descriptions that follow.
Figure 8-2: Advanced common controls in a dialog box.
The Date and Time Picker
A common field on a dialog box is a place for the user to enter a date and time. Before there was a date and time picker, developers had to either use a third-party control or subclass an MFC edit control to do significant data validation to ensure that the entered date was valid. The date and time picker control prompts the user for a date or time while offering the developer a wide variety of styles and options. For example, dates can be displayed in short formats (8/ 14/68) or long formats (August 14, 1968). A time mode lets the user enter a time using a familiar hours/minutes/seconds AM/PM format.
The control also lets you decide if you want the user to select the date using in-place editing, a pull-down calendar, or a spin button. Several selection options are available, including single and multiple select (for a range of dates) and the ability to turn on and off the "circling" in red ink of the current date. The control even has a mode that lets the user select "no date" via a check box. In Figure 8-2, the first four controls on the left illustrate the variety of configurations available with the date and time picker control.The MFC class CDateTimeCtrl provides the MFC interface to the date and time picker common control. This class provides a variety of notifications that enhance the programmability of the control. CDateTimeCtrl provides member functions for dealing with either CTime or COleDateTime time structures.You set the date and time in a CDateTimeCtrl using the SetTime member function. You can retrieve the date and time using the GetTime function. You can create custom formats using the SetFormat member function and change a variety of other configurations using the CDateTimeCtrl interface.
CTime vs. COleDateTime
Most "longtime" MFC developers are accustomed to using the CTime class. However, because CTime's valid dates are limited to dates between January 1, 1970, and January 18, 2038, many developers need an alternative. One popular alternative is COleDateTime, which is provided for OLE automation support and handles dates from 1 January 100 through 31 December 9999. Both classes have pros and cons. For example, CTime handles all the issues of daylight saving time, while COleDateTime does not.Many developers choose COleDateTime because of its much larger range. Any application that uses CTime will need to be reworked in approximately 40 years because the maximum value is the year 2038. The class you decide to use must depend on your particular needs and the potential longevity of your application.
The Month Calendar
The large display at the bottom left of Figure 8-2 is a month calendar. Like the date and time picker control, the month calendar control lets the user select a date. However, the month calendar control can also be used to implement a small personal information manager in your applications. You can show as many months as you have room for—from one month to a year's worth of months, if you want. Ex08b uses the month calendar control to show only two months.The month calendar control supports single or multiple selection and allows you to display a variety of options, such as numbered months and a circled "today's date." Notifications for the control let the developer specify which dates are in boldface. It is entirely up to the developer to decide what boldface dates might represent. For example, you could use the bold feature to indicate holidays, appointments, or unusable dates. The MFC class CMonthCalCtrl implements this control.To initialize the CMonthCalCtrl class, you can call the SetToday member function. CMonthCalCtrl provides members that deal with both CTime and COleDateTime, including SetToday.
The Internet Protocol Address Control
If you write an application that uses any form of Internet or TCP/IP functionality, you might need to prompt the user for an Internet Protocol (IP) address. The common controls include an IP address edit control, as shown in the top right of Figure 8-2. In addition to letting the user enter a 4-byte IP address, this control performs an automatic validation of the entered IP address. CIPAddressCtrl provides MFC support for the IP address control.An IP address consists of four "fields," as shown in Figure 8-3. The fields are numbered from left to right.
Figure 8-3: The fields of an IP address control.
To initialize an IP address control, you call the SetAddress member function in your OnInitDialog function. SetAddress takes a DWORD, with each byte in the DWORD representing one of the fields. In your message handlers, you can call the GetAddress member function to retrieve a DWORD or a series of BYTES to retrieve the various values of the four IP address fields.
The Extended Combo Box
The "old-fashioned" combo box was developed in the early days of Windows. Its inflexible design has been the source of much developer confusion. With the common controls, Microsoft has released a much more flexible version of the combo box called the extended combo box.The extended combo box gives the developer much easier access to and better control over the edit control portion of the combo box. In addition, the extended combo box lets you attach an image list to the items in the combo box. You can display graphics in the extended combo box easily, especially compared with the old days of using owner-drawn combo boxes. Each item in the extended combo box can be associated with three images: a selected image, an unselected image, and an overlay image. You can use these images to provide a variety of graphical displays in the combo box, as we'll see shortly in the Ex08b sample. The two combo boxes on the right in Figure 8-2 are both extended combo boxes. The MFC CComboBoxEx class provides comprehensive extended combo box support.Like the list control introduced earlier in this chapter, CComboBoxEx can be attached to a CImageList that will automatically display graphics next to the text in the extended combo box. If you're familiar with CComboBox, CComboBoxEx might cause some confusion: Instead of containing strings, the extended combo box contains items of type COMBOBOXEXITEM, a structure that consists of the following fields:
UINT mask A set of bit flags that specify which operations are to be performed using the structure. For example, set the CBEIF_IMAGE flag if the image field is to be set or retrieved in an operation.
INT_PTR iItem The extended combo box item number. Like the older style of combo box, the extended combo box uses zero-based indexing.
LPSTR pszText The text of the item.
int cchTextMax The length of the buffer available in pszText.
int iImage A zero-based index into an associated image list.
int iSelectedImage An index of the image in the image list to be used to represent the "selected" state.
int iOverlay An index of the image in the image list to be used to overlay the current image.
int iIndent The number of 10-pixel indentation spaces.
LPARAM lParam A 32-bit parameter for the item.
You'll see how to use this structure in the upcoming Ex08b example.