3.5 Arrange for Communication with Windows

This section explains the third step in writing Hello: set up the mechanism by which Hello responds to Windows messages.

·To set up window message hooks for the main window:

1.Add the following message map for the new window class to your HELLO.CPP file:

BEGIN_MESSAGE_MAP(CMainWindow, CFrameWnd)

ON_WM_PAINT()

ON_COMMAND(IDM_ABOUT, OnAbout)

END_MESSAGE_MAP()

You can put the message map with your code for the main window class. The message map connects specific Windows messages with member functions of your window class provided as message handlers. The message map is really a part of your window class, so writing it is part of writing the class. It's important to put the message map in the HELLO.CPP file rather than the HELLO.H file to ensure that the macros comprising the map are not invoked more than once. The macros create code and therefore allocate memory. Thus they must not be included in more than one module.

You already added the DECLARE_MESSAGE_MAP macro to your
CMainWindow
class declaration. Any window class you write requires this macro as part of its declaration. The message map in HELLO.CPP is the implementation corresponding to the message-map declaration.

2.Add message-handler functions for the Windows messages you need to process.

For Hello, you'll add two message-handler member functions to the
CMainWindow
class. The code for this step is given later in the tutorial, in “Paint the Window” on page 101 and “Add an About Dialog Box” on page 105.

The Microsoft Foundation Class Library provides default behavior for all messages, but you must provide CMainWindow member functions to override the default behavior and handle the menu command for the About dialog box and to handle the WM_PAINT message, which signals your window to paint its contents.

To continue the tutorial, see “Paint the Window” on page 101. For more information about the code you just added, see the following discussion.