Deriving from Control Classes

One of the basic techniques of MFC programming to be familiar with is how to create a class derived from a MFC Common Control class, such as CEdit. The most frequent use of this is to override processing of messages, such as a WM_KEYDOWN message. The following is a modified version of the instructions in TN062: Message Reflection for Windows Controls. The following assumes that you already have a dialog box in an existing application.

You then can use the ClassWizard to add processing for messages, such as the WM_KEYDOWN message. Do this using the "Message Maps" tab of the ClassWizard. From there, first ensure that the chosen class name is your derived class, then for the object id, choose your derived class again. Then in the messages list box, choose whatever message you need to override, and then click on "Add Function".

You then can attach a member variable to each control in a dialog box that is the type that you derived from. For example, if you derived from CEdit, then you can process edit boxes using the derived class. Use ClassWizard to attach a member variable to a control. In the Add Member Variable dialog box, finish the variable name and choose “Control” for the category, then for the variable type, choose the class name you used for the derived class. Do this using one of the following two:

Also see:


See my Visual C++ Programmer Stuff page for more C++ stuff.