Using Common Controls

HomeOverviewHow Do I

It’s recommended that your Windows 95 application use the common controls where applicable. Visual C++’s dialog editor offers the common controls on its control palette, and MFC provides classes for all the Windows common controls.

One place in the DRAWCLI sample where a common control can be used is in the dialog box for specifying the thickness of the drawing pen. DRAWCLI formerly used a simple edit control in which the user typed the pen thickness; now DRAWCLI adds a spin control, allowing the user to use the mouse or the arrow keys to increment or decrement the pen thickness.

You can add a spin control to your application by using the Visual C++ dialog editor; associate the spin control with an edit control by checking the Auto Buddy and Set Buddy Integer properties on the control’s property page. Next, use the Member Variables tab of the ClassWizard dialog to add a member variable for the spin control. This causes ClassWizard to do two things:

You must manually add the statement #include <afxcmn.h> to your STDAFX.H file to include MFC’s common control support in an existing application. If you generate a new application with AppWizard, AppWizard now inserts this statement into STDAFX.H automatically.

Finally, override the OnInitDialog member function of the dialog class to initialize the spin control. Here’s what the function looks like in DRAWCLI:

BOOL CRectDlg::OnInitDialog() 
{
   CDialog::OnInitDialog();
   
   m_SpinCtrl.SetRange(0, 100);
   m_SpinCtrl.SetBase(10);
   m_SpinCtrl.SetPos(1);
   return TRUE;  
}

This function sets the initial, minimum, and maximum values for the spin control. It also sets the spin control to use decimal notation.

For more information on using MFC’s common control classes, see MFC: Windows 95 Support in Visual C++ Programmer’s Guide and the descriptions of CAnimateCtrl, CHeaderCtrl, CHotKeyCtrl, CListCtrl, CProgressCtrl, CSliderCtrl, CSpinButtonCtrl, CStatusBarCtrl, CTabCtrl, CToolBarCtrl, CToolTipCtrl, and CTreeCtrl in the Class Library Reference. For a demonstration of various common controls, see the CMNCTRL1, CMNCTRL2, and FIRE sample applications in the MFC samples.