A Map of Messages

Started by sukishan, Jul 12, 2009, 05:22 PM

Previous topic - Next topic

sukishan

A Map of Messages

For the compiler to manage messages, they should be included in the class definition. The list of messages starts on a section driven by, but that ends with, the DECLARE_MESSAGE_MAP macro. The section can be created as follows:

#include <AFXWIN.H>
class CSimpleFrame : public CFrameWnd
{
public:
    CSimpleFrame();

    DECLARE_MESSAGE_MAP()
};

The DECLARE_MESSAGE_MAP macro should be provided at the end of the class definition. The actual messages (as we will review them shortly) should be listed just above the DECLARE_MESSAGE_MAP line. This is just a rule. In some circumstances, and for any reason, you may want, or have, to provide one message or a few messages under the DECLARE_MESSAGE_MAP line.

To implement the messages, you should/must create a table of messages that your program is using. This table uses two delimiting macros. It starts with a BEGIN_MESSAGE_MAP and ends with a END_MESSAGE_MAP macro. The BEGIN_MESSAGE_MAP macro takes two arguments, the name of your class and the MFC class you derived your class from. An example would be:

BEGIN_MESSAGE_MAP(CSimpleFrame, CFrameWnd)Like the DECLARE_MESSAGE_MAP macro, END_MESSAGE_MAP takes no argument. Its job is simply to specify the end of the list of messages. The table of messages can be created as follows:

Code#include <AFXWIN.H>
#include "resource.h"

class CMainFrame : public CFrameWnd
{
public:
    CMainFrame ();

    DECLARE_MESSAGE_MAP()
};

CMainFrame::CMainFrame()
{
    LoadFrame(IDR_MAINFRAME);
}

class CMainApp: public CWinApp
{
public:
    BOOL InitInstance();
};

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)

END_MESSAGE_MAP()

BOOL CMainApp::InitInstance()
{
    m_pMainWnd = new CMainFrame ;
    m_pMainWnd->ShowWindow(SW_SHOW);
    m_pMainWnd->UpdateWindow();

    return TRUE;
}

CMainApp theApp;There are various categories of messages the operating system receives. Some of them come from the keyboard, some from the mouse, and some others from various other origins. For example, some messages are sent by the application itself while some other messages are controlled by the operating system.
A good beginning makes a good ending