Subclassing Controls


Click here to change the theme.

Although it is not made clear in MFC documentation and such, the typical way to subclass a control is by simply using the ClassWizard to generate the code to subclass the control. Many of us are not aware that the code ClassWizard generates for "Control" category variables does this.

I will use an edit control as an example but the following applies to all common controls. For an edit control you have at least two categories to choose from. If you choose "Value" for "Category" then the member variable type can be a "CString" and the control is not subclassed for it. If you choose "Control" for "Category" then the member variable type can be "CEdit" and the control is subclassed using the object created with that class. If you create a class derived from CEdit then your derived class gets added to the list of classes that a "Control" category member variable can use for member variables. If you create a "Control Category" member variable using your derived class as the Variable type then ClassWizard generates code that subclasses the edit control using your derived class.

Add Member Variable Dialog