3.4 Put a Window on the Screen

This section explains the second step in writing Hello: create a window. In this section you'll add more code to the HELLO.H and HELLO.CPP files that you started in the previous section, in order to create Hello's main window class.

·To write Hello's main window class:

1.Derive a main window class from the Microsoft Foundation class CFrameWnd. Add the following declaration of class CMainWindow to your HELLO.H file:

class CMainWindow : public CFrameWnd
{
public:
// Constructor
//
CMainWindow();

// Handler function for painting messages
//
afx_msg void OnPaint(); // For WM_PAINT message

// Handler function for About dialog
//
afx_msg void OnAbout();

// Macro to declare a message map
//
DECLARE_MESSAGE_MAP()
};

Put this declaration just below the preprocessor directives at the top of the file.

The afx_msg modifier is similar to the virtual keyword of C++. Member functions prefixed with afx_msg are prototyped in class CWnd and can be overridden in derived window classes. They tie into the message map associated with the Microsoft Foundation window class.

At this point, your HELLO.H file is complete. You can check it against Listing 1.

2.Add the following CMainWindow constructor definition to your HELLO.CPP file:

CMainWindow::CMainWindow()
{
LoadAccelTable( "MainAccelTable" );
Create( NULL,, "Hello Foundation Application",
WS_OVERLAPPEDWINDOW, rectDefault,

NULL, "MainMenu" );
}

Put the constructor in a section of the file for CMainWindow member functions. Keep this section separate from the section that contains
the InitInstance definition. The HELLO.CPP file in Listing 2 puts all CMainWindow code above all CTheApp code.

To continue the tutorial, see “Arrange for Communication with Windows” on page 94. For more information about the code you just added, see the discussion below.