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

推荐订阅源

Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
B
Blog
Martin Fowler
Martin Fowler
WordPress大学
WordPress大学
爱范儿
爱范儿
博客园_首页
博客园 - 聂微东
量子位
V
Visual Studio Blog
aimingoo的专栏
aimingoo的专栏
T
The Blog of Author Tim Ferriss
J
Java Code Geeks
小众软件
小众软件
大猫的无限游戏
大猫的无限游戏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
Netflix TechBlog - Medium
F
Fortinet All Blogs
The Cloudflare Blog
T
Tailwind CSS Blog
G
Google Developers Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
腾讯CDC

博客园 - xiaobin80

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