Share via


Adding an MFC Class

OverviewHow Do I

To add classes derived from MFC (Microsoft Foundation Class Library) classes to your project, you can use any of the following tools: ClassView, WizardBar, ClassWizard, or the New Class command on the Insert menu. You simply specify the new class's name, select the base class from which it is to be derived, and select the ID of the dialog box with which it is to be associated (if any). WizardBar creates a header file and an implementation file for the new class.

Note   You can add MFC classes to an ATL COM application if you initially created the application with MFC support.

To add an MFC class to your project

  1. Open the New Class dialog box as described in the New Class dialog box.

  2. In the Class type drop-down list, select MFC Class.

  3. In the Name box, specify the name of the new class.

    The name you specify is reflected in the read-only File name box, where the .cpp file is specified. By default, the header file and the implementation file have the same name as the class file.

    • To change the names of the header and implementation files for this class, click Change and type the names in the Change Files dialog box.

    • To add the new class to existing files, click Change, then click the Browse button and locate the files you wish to add it to.

  4. Choose a base class from which to derive the new class. There are two kinds of MFC classes:

    • Those, such as CButton, that do not require a resource ID.

      Classes such as CButton or CEdit that are derived from base classes other than CDaoRecordView, CDialog, CFormView, CPropertyPage, or CRecordView do not require a resource ID. Because these classes do not require a resource ID, the New Class dialog's Dialog ID drop-down list is not active.

    • Those, such as CDialog, that do require a resource ID.

      For classes that require a resource ID (classes derived from CDaoRecordView, CDialog, CFormView, CPropertyPage, or CRecordView), it is more efficient if you first use the dialog editor to create the resource and its ID, then use ClassWizard to create a class associated with that resource ID.

      However, if you create the class first, then the resource, you can later associate the class to the resource using the procedure described in Associate an Existing Class with a Resource.

      For information on using the resource editors, see Resource Editor Topics (Specific to Visual C++).

      For information about creating database classes (CRecordView, CRecordset) and OLE classes, see the article ClassWizard: Database Support.

  5. For dialog-based classes, select the resource with which the class is to be associated from the Dialog ID drop-down list.

  6. Select the type of Automation support: None, Automation, or Createable by type ID. (Note that the base class must support Automation in order for any Automation options to be available.)

    • Select None if you want the new class to have no Automation capability.

    • Select Automation if you want to expose the capabilities of the new class through Automation.

      If you select this option, the newly created class will be available as a programmable object by automation client applications, such as Microsoft Visual Basicâ„¢ and Microsoft Excel. This option is available only for some classes.

    • Select Createable by type ID if you want to allow other applications to create objects of this class using Automation.

      With this option, automation clients can directly create an Automation object. The type ID in the text box is used by the client application to specify the object to be created; it is system-wide and must be unique. This option is available only for some classes.

    Note   Use ClassWizard's Automation tab to add Automation methods and properties to an existing class. These methods and properties define a dispatch interface that Automation clients can use.

  7. Click OK to create the new class and add it to the project.

    The new class immediately appears in ClassView, and you can view its header and implementation files in the FileView pane of the project workspace.

See Also   Adding an ATL Class, Adding a Generic Class