The CMNCTRLS demo program shows how to use every common control. You can get this program from the online help by searching for CMNCTRLS. Copy, compile, and run this program. This document refers to the standard, and Common controls.
Basic Guidelines for Adding Controls
Adding a control at design time
Almost all controls can be added to a form (like a CFormView or CDialog) using the resource editor. The normal tool pallete that appears has the standard Windows controls (Edit Box, Button, ListBox, etc.), and the earlier Common Controls (Sliders, Tree, List, etc.). Some of the newer Common Controls are available in the COMCTxxx.OCX file, such as the Animation Control.
In order to use the newer Common Controls, you will need to add the OCX to your project. You do this by doing the following:
- Open your project
- Click the Project menu choice
- Click the Add To Project... menu choice
- Click the Components and Controls menu choice. A dialog will appear.
- In the Components and Controls Gallery dialog, cdouble-click Registered ActiveX Controls.
- You can now select the desired control to add. Click OK to any further prompts.
- You will now see the new control appear in your tool pallete. Drop it on your form like usual.
Adding a control at runtime
While it's more work, you can also add controls at run time, rather than design time. The basic steps involded in doing this are:
- Create a data member for your form, that is the MFC class for the desired control.
(For example, CAnimateCtrl for the animated control)
- Call the Create function for the data member in OnInitDialog if it's in a dialog, or OnCreate if it's in a form
Each control class has it's own unique Create syntax.
Responding to events from the control
Once a control is on your form, you can use ClassWizard to have the dialog or form respond to events/messages that the child sends to it's parent (the dialog or form). In order to do this, go to ClassWizard, and select the MessageMap tab, then:
- Make sure that the class for the dialog/form is selected in ClassName combobox
- Make sure that the ID for the desired control is selected in the Object IDs list
- Highlight the desired event in the Messages list (this changes for each control type)
- Click the Add Function button.
- Verify the new function's name (it helps to assign a good anme to the control before starting), and click OK.
The new function will now be called whenever the child sends that message to the parent dialog/form. A typical example here, is to handle the BN_CLICKED message for a button control.
If you look at the BEGIN_MESSAGE_MAP list, you will note how VC++ implements the message map to call the function when needed. There is also a more dynamic method of handling child messages by modifying this message map manually, but this option is seldomly needed. See ON_COMMAND and ON_COMMAND_RANGE in your help files for more information.
Interacting with the control
In order to interact with the control, you will want to have an MFC object that is attached to it. The type of interaction, again, depends on the control type. For example, you might want to place text into an edit box, or get the current value of a slider control, or hide a particular check box item.
There are two ways to get an MFC class for a control, at design-time with ClassWizard, or runtime with GetDlgItem.
Design Time with ClassWizard
In order to create an MFC class object for a control at design time, go to ClassWizard, then:
- Select the Member Variables tab.
- Make sure the dialog/form class is highlighted in ClassName
- Highlight the desired control in the Control IDs list
- Click the Add Variable button.
- In the next dialog that appears, enter a good Member Variable Name for the object.
- In the Category section, make sure the Control is highlighted. *
- In Variable Type, make sure the MFC class type you want for the control is selected.
- Click OK.
* - The Category type permits you to select between two types of categories: Value or Control. If you select Value, then the variable will be a simple data class, capable of holding the value for the control. The Control category however, is a more complex class that not only permits you to interact with the value of the control, but also provides more flexibility in manipulating the control itself. For example, a Value category for an edit box will be a CString, that you can get or set, but a Control category will be a CEdit class, permitting you to (in addition) move, hide, or resize the edit control itself.
Runtime with GetDlgItem
The method above permits you to associate an MFC object with a control without writting any code. But, you can also save memory space (although, very little), by creating the control at runtime. To do this, you can use the GetDlgItem function, and type-cast it's return value to the appropriate type. For example:
CEdit* pEdit = (CEdit*) GetDlgItem( IDC_FIRSTNAME);
Since GetDlgItem returns a CWnd*, you can use any of the CWnd functions without a typecast, like:
Control MFC Class Description animation CAnimateCtrl Displays successive frames of an AVI video clip. AVI plays in seperate thread, and can be loaded from resource or a seperate file. button CButton Pushbuttons; also used for check boxes, radio buttons, and group boxes combo box CComboBox Combination of an edit box and a list box. Also has a style where user an not type in edit box. edit box CEdit Boxes for entering text header CHeaderCtrl Button that appears above a column of text; controls width of text displayed hotkey CHotKeyCtrl Invisible window that enables user to create a "hot key" to perform an action quickly image list CImageList Collection of images used to manage large sets of icons or bitmaps (image list isn't really a control; it supports lists used by other controls) list CListCtrl Window that displays a list of text with icons. The appearance has several varions, such as snaking columns or columnar, but not used for reports. list box CListBox Box that contains a list of strings progress CProgressCtrl Window that indicates progress of a long operation rich edit CRichEditCtrl Window in which user can edit with character and paragraph formatting (see Classes Related to Rich Edit Controls). The CRichEditView class is a view whose single, main control is a CRichEdit. scroll bar CScrollBar Scroll bar used as a control inside a dialog box (not on a window). Scrollbars can be added to a window as a style option. slider CSliderCtrl Window containing a slider control with optional tick marks spin button CSpinButtonCtrl Pair of arrow buttons user can click to increment or decrement a value static-text CStatic Text for labeling other controls. To create a variable for a static control, you must change it's ID from the default of IDC_STATIC. Static controls can contain bitmaps as well as text. status bar CStatusBarCtrl Window for displaying status information, similar to MFC class CStatusBar. AppWizard will normally add one of these to your new projects. tab CTabCtrl Analogous to the dividers in a notebook; used in "tab dialog boxes" or property sheets toolbar CToolBarCtrl Window with command-generating buttons, similar to MFC class CToolBar. AppWizard will normally add one of these to your new projects. tool tip CToolTipCtrl Small pop-up window that describes purpose of a toolbar button or other tool tree CTreeCtrl Window that displays a hierarchical list of items.