惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
罗磊的独立博客
IT之家
IT之家
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
月光博客
月光博客
博客园 - Franky
博客园 - 聂微东
博客园_首页
爱范儿
爱范儿
量子位
博客园 - 三生石上(FineUI控件)
G
Google Developers Blog
Martin Fowler
Martin Fowler
小众软件
小众软件
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Y
Y Combinator Blog
Vercel News
Vercel News
腾讯CDC
Microsoft Azure Blog
Microsoft Azure Blog
Hugging Face - Blog
Hugging Face - Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Cloudflare Blog
Engineering at Meta
Engineering at Meta

博客园 - xiaobin80

Hello MFC - The last little bit of work Hello MFC - Add Resource and About Dialog my first mfc application Development Environment Preparation Clang IDE的选择在Windows Visual Studio 2017 and Fx EOL 跟我学python(5)- 匹配数字 跟我学python(4)- 正则 跟我学python(3)--- 带shell功能的hello world 跟我学python(2)- 第一个程序 跟我学python --- 搭建开发平台 Debian server 安装 debian 与 ubuntu 之 - sudo Try Value 计算文件CRC32数值 docker ce on Debian 使用hugo在gitee上写blog spring cloud gateway - RequestRateLimiter postman使用 改进的冒泡算法 Create User - mysql COM调用 – VB、PB J-Link clone问题 修复山寨版的J-Link
Hello MFC - The Message Map
xiaobin80 · 2026-09-17 · via 博客园 - xiaobin80

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
DECLARE_MESSAGE_MAP()
  • impl
BEGIN_MESSAGE_MAP(CMyApp, CWinApp)
    ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()

begin message map

BEGIN_MESSAGE_MAP( theClass, baseClass )

Parameters:

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

Modify CMyApp

MyApp.cpp

  • OnHelp()
#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>

Example

  • MyApp.h
#pragma once

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

class CMyApp :
	public CWinApp
{
public:
	CMyApp();

// Overrides
public:
	virtual BOOL InitInstance();

	// Implementation

	DECLARE_MESSAGE_MAP()
};
  • MyApp.cpp
#include "stdafx.h"
#include "MyApp.h"
#include "MainWindow.h"

BEGIN_MESSAGE_MAP(CMyApp, CWinApp)
	ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()

CMyApp::CMyApp()
{
}


BOOL CMyApp::InitInstance()
{
	m_pMainWnd = new CMainWindow();
	m_pMainWnd->ShowWindow(SW_SHOW);
	m_pMainWnd->UpdateWindow();
	return TRUE;
}

CMyApp myApp;

Ref