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
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
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 Class Library Reference. The following example shows how you might write an override of OnInitDialog to call SubclassDlgItem:
BOOL CMyDialog::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.