14.2 Constructors for Derived Window Classes

When you derive your window class from a Microsoft Foundation base window class, you must at least implement a constructor for your window class. To initialize the class, the constructor can call the Create member function, which calls the Windows API function that actually create the window that is displayed on screen by Windows.

For example, the example program file HELLO.H contains a class declaration, a fragment of which is shown here, for a derived window class and its constructor. The example program file HELLO.CPP contains the definition for the constructor, also shown here.

// in HELLO.H

class CMainWindow : public CFrameWnd

{

public:

CMainWindow();

// other parts of declaration left out ...

};

// in HELLO.CPP

CMainWindow::CMainWindow()

{

LoadAccelTable( "MainAccelTable" );

Create( NULL, "Hello Foundation Application",

WS_OVERLAPPEDWINDOW, rectDefault, NULL, "MainMenu" );

}

The constructor in the example above loads an accelerator table resource for the window and then calls the Create member function, passing in arguments that specify various characteristics of the window.

If you want to add other child windows to your main frame window, you can create those windows in the frame window constructor.

Note:

If you are defining a window class from which you plan to derive other window classes, you should not call Create in the constructor of the base class, because in derived classes more than one window will be created by successive calls to base class constructors. Instead, you should implement Create in your base window class.