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

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Jina AI
Jina AI
博客园 - 司徒正美
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
J
Java Code Geeks
博客园 - 聂微东
酷 壳 – CoolShell
酷 壳 – CoolShell
爱范儿
爱范儿
美团技术团队
腾讯CDC
博客园 - Franky
MyScale Blog
MyScale Blog
人人都是产品经理
人人都是产品经理
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
aimingoo的专栏
aimingoo的专栏
博客园_首页
V
V2EX
Martin Fowler
Martin Fowler
T
The Blog of Author Tim Ferriss

博客园 - Xulei

解决IIS "The page cannot be found"问题 这几天的一些总结,先发在这,以后整理 Blog文章汇总 电脑的脉搏—时钟频率的来龙去脉 通过Office 2007发布Blog WindowsXP安装文件集成Service Pack 3 REHL5.2下安装VMWare Tools和codeblocks Linux中简单配置Samba服务器 cout 控制数值的输出精度 Linux修改IP和DNS Linux 中修改主机hostname Linux的学习路径 使用MSXML解析XML文件 超酷超炫Linux: Linsta "媲美" Vista 外企面试官最爱提的10个问题 The solution of "ERROR - ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务" VMware "VMnet0 is not running"错误的解决方法 精简版ORACLE客户端程序 extern "C"的用法解析
MFC中自定义消息
Xulei · 2007-11-22 · via 博客园 - Xulei

消息映射、循环机制是Windows程序运行的基本方式。VC++ MFC 中有许多现成的消息句柄,可当我们需要完成其它的任务,需要自定义消息,
就遇到了一些困难。在MFC ClassWizard中不允许添加用户自定义消息,所以我们必须手动在程序中添加相应代码,以便可以象处理其它消息一样处理自定义消息。

自定义消息的步骤如下:
(1)建立Single Document的MFC Application,工程名为:MyMessage
(2)自定义消息:
第一步:定义消息
在Resource.h中添加如下代码:

//推荐用户自定义消息至少是WM_USER+100,因为很多新控件也要使用WM_USER消息。
#define WM_MY_MESSAGE (WM_USER+100)

第二步:声明消息处理函数
选择CMainFrame类中添加消息处理函数
在MainFrm.h文件中,类CMainFrame内,声明消息处理函数,代码如下:

protect:
    afx_msg LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam); 

第三步:实现消息处理函数
在MainFrm.cpp文件中添加如下代码:

LRESULT CMainFrame::OnMyMessage(WPARAM wParam, LPARAM lParam)
{
      
//TODO: Add your message handle code
      return 0;
}

 第四步:在CMainFrame类的消息块中,使用ON_MESSAGE宏指令将消息映射到消息处理函数中

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
    ON_WM_CREATE()
    ON_MESSAGE(WM_MY_MESSAGE,OnMyMessage)
    
//ON_REGISTERED_MESSAGE (WM_MY_MESSAGE,OnMyMessage)
END_MESSAGE_MAP()

 如果用户需要一个定义整个系统唯一的消息,可以调用SDK函数RegisterWindowMessage定义消息:
在Resource.h中将代码

#define WM_MY_MESSAGE (WM_USER+100)

替换为:

static UINT WM_MY_MESSAGE=RegisterWindowMessage(_T("User"));

并使用ON_REGISTERED_MESSAGE宏指令取代ON_MESSAGE宏指令,其余步骤同上。
注:如果仍然使用ON_MESSAGE宏指令,compile可以通过,但是无法响应消息。
当需要使用自定义消息时,可以在相应类中的函数中调用函数PostMessage或SendMessage发送消息PoseMessage(WM_MY_MESSAGE,O,O)。

附:RegisterWindowMessage函数说明

RegisterWindowMessage Function

--------------------------------------------------------------------------------

The RegisterWindowMessage function defines a new window message that is guaranteed to be unique throughout the system.
The message value can be used when sending or posting messages.

Syntax 

 UINT RegisterWindowMessage(          LPCTSTR lpString
    );
Parameters

  lpString
 [in] Pointer to a null-terminated string that specifies the message to be registered.
Return Value

If the message is successfully registered, the return value is a message identifier in the range 0xC000 through 0xFFFF.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

The RegisterWindowMessage function is typically used to register messages for communicating between two cooperating applications.

If two different applications register the same message string, the applications return the same message value.
The message remains registered until the session ends.
//如果两个Application使用相同的string注册message,他们将等到相同的message值,也就是得到相同的message