The Message Map

add F1 feature

2025-11-13

Written by: xiaobin

The message map is MFC’s way of avoiding the lengthy vtables that would be required if every class had a virtual function for every possible message it might receive. Any class derived from CCmdTarget can contain a message map.

  1. Declare the message map by adding a DECLARE_MESSAGE_MAP statement to the class declaration.
  2. Implement the message map by placing macros identifying the messages that the class will handle between calls to BEGIN_MESSAGE_MAP and END_MESSAGE_MAP.
  3. Add member functions to handle the messages.
DECLARE_MESSAGE_MAP()
BEGIN_MESSAGE_MAP(CMyApp, CWinApp)
    ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()

begin message map

BEGIN_MESSAGE_MAP( theClass, baseClass )

Parameters:

Specifies the name of the class whose message map this is.
Specifies the name of the base class of theClass.

Modify CMyApp

MyApp.cpp

#include "stdafx.h"

Copy stdafx.h and targetver.h to the project directory.

afxwin

MyApp.h

Use

#ifndef __AFXWIN_H__
    #error "include 'stdafx.h' before including this file for PCH"
#endif

instead of

#include <afxwin.h>

Ref