Compartilhar via


Deriving Controls from a Standard Control

As with any CWnd-derived class, you can modify a control's behavior by deriving a new class from an existing control class.

To create a derived control class

  1. Derive your class from an existing control class and optionally override the Create member function so that it provides the necessary arguments to the base-class Create function.

  2. Provide message-handler member functions and message-map entries to modify the control's behavior in response to specific Windows messages. See Mapping Messages to Functions.

  3. Provide new member functions to extend the functionality of the control (optional).

Using a derived control in a dialog box requires extra work. The types and positions of controls in a dialog box are normally specified in a dialog-template resource. If you create a derived control class, you cannot specify it in a dialog template since the resource compiler knows nothing about your derived class.

To place your derived control in a dialog box

  1. Embed an object of the derived control class in the declaration of your derived dialog class.

  2. Override the OnInitDialog member function in your dialog class to call the SubclassDlgItem member function for the derived control.

SubclassDlgItem "dynamically subclasses" a control created from a dialog template. When a control is dynamically subclassed, you hook into Windows, process some messages within your own application, then pass the remaining messages on to Windows. For more information, see the SubclassDlgItem member function of class CWnd in the MFC Reference. The following example shows how you might write an override of OnInitDialog to call SubclassDlgItem:

BOOL CSubDialog::OnInitDialog()
{
   CDialog::OnInitDialog();

   m_wndMyBtn.SubclassDlgItem(IDC_MYBTN, this);

   return TRUE;
}

Because the derived control is embedded in the dialog class, it will be constructed when the dialog box is constructed, and it will be destroyed when the dialog box is destroyed. Compare this code to the example in Adding Controls By Hand.

See Also

Reference

Making and Using Controls

Concepts

Controls (MFC)