How To Derive From Classes not Listed in ClassWizard

This article was previously published under Q99161
This article has been archived. It is offered "as is" and will no longer be updated.
SUMMARY
Microsoft ClassWizard supports deriving classes only from the classeslisted in the Class Type field of the Add Class dialog box. To createa class derived from a previously derived class or to derive fromanother class based on the Microsoft Foundation Class Library CWndclass that is not listed in the Class Type field, a few extra stepsare required. The text below presents these steps.
MORE INFORMATION
For example, suppose the following derivation tree is desired:
   MyDerivedClass         ^         |     MyBaseClass         ^         |      CDialog				
or, it is desirable to derive a class from a class based on CWnd thatis not listed in the Class Type field in the Add Class dialog box,such as CFileDialog. There is no predefined method to create this typeof class hierarchy using the ClassWizard. However, by using theparsing techniques ClassWizard uses, you can create these classhierarchies.

The steps below use CDialog as the default class type. Beforeproceeding, determine which predefined class type is closest to thedesired base class. For example, CFileDialog is similar to CDialog. Ifnone of the classes correspond closely to your desired class, use thegeneric CWnd class.

To create a class with multiple levels of derivation, perform thefollowing three steps:

  1. Use ClassWizard to create MyDerivedClass, deriving it from CDialog (or another appropriate predefined class).
  2. Use ClassWizard to create MyBaseClass, deriving it from CDialog (or another appropriate predefined class).
  3. Edit the code generated for MyDerivedClass and replace all references to CDialog with MyBaseClass. This step is very important; many errors occur if this is not done correctly and these errors may be difficult to track down.
To create a class based on a class based on CWnd that is not supportedby ClassWizard, perform the following two steps:

  1. Use ClassWizard to create MyDerivedClass, deriving it from CDialog (or another appropriate predefined class based on CWnd).
  2. Edit the code generated for MyDerivedClass and replace all references to CDialog with the name of the class from which you are deriving this class, for example, CFileDialog. This step is very important; many errors occur if this is not done correctly and these errors may be difficult to track down.
Then, for either type of class, perform the following three steps:

  1. Delete the project .CLW file.
  2. Start App Studio, load your project .RC file, and activate Class Wizard.
  3. Because the project does not have a .CLW file, ClassWizard prompts to generate a .CLW file. Choose Yes to generate the file. NOTE: You must generate this file in App Studio. If you attempt to generate the file in Visual Workbench, VWB instructs you to generate the file in App Studio.
Once App Studio has created the .CLW file, the base class of thederived class has been changed successfully. To verify this, view theclass in ClassWizard and see the data in the Class Info dialog box.

For classes created using multiple levels of derivation, you can useClassWizard to pass system messages, such as WM_INITDIALOG, to thebase class as well. To do this, perform the following nine steps:

  1. Start ClassWizard.
  2. Select the MyDerivedClass class.
  3. Select MyDerivedClass in the Object IDs window.
  4. Select the WM_INITDIALOG message in the Messages window.
  5. Choose Add Function to add a function skeleton that calls the OnInitDialog() function in MyBaseClass.
  6. Select the MyBaseClass class.
  7. Select MyBaseClass in the Object IDs window.
  8. Select the WM_INITDIALOG message in the Messages window.
  9. Choose Add Function to add a function skeleton that calls the OnInitDialog() function in CDialog. This step is required only once. If you derive additional classes from the base class, you do not need to redo this operation.
Properties

Article ID: 99161 - Last Review: 01/09/2015 05:01:20 - Revision: 3.1

  • The ClassWizard
  • kbnosurvey kbarchive kbhowto kbwizard KB99161
Feedback