消息映射宏 [英] Message map macros

查看:32
本文介绍了消息映射宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您什么时候使用 ON_COMMAND,我们什么时候使用 ON_MESSAGE.它们之间有什么区别.

解决方案

ON_COMMAND 专门用于处理命令消息(即WM_COMMAND),比如点击按钮/菜单项/工具栏按钮.

ON_MESSAGE 更通用,可用于任何 Windows 消息.它通常用于未提供特定消息映射宏的不太频繁处理的消息.您也可以使用 ON_MESSAGE 来处理 ON_COMMAND 消息,但您必须自己提取消息信息(即命令 ID).

示例:

看这里:

在消息映射中:

ON_MESSAGE( WM_COMMAND, OnMyCommand )

处理程序:

LRESULT CMyWnd::OnMyCommand( WPARAM wParam, LPARAM lParam ){//... 在这里处理消息int commandId = LOWORD(wParam);开关(命令ID){案例 ID_HELLOCOMMAND:MessageBox(0, "你好!", "ID_HELLO_COMMAND", MB_OK);休息;//... 这里的其他命令}返回0L;}

免责声明: 由于 MFC 的消息泵机制,您可能需要做的比上面显示的要多一些.最佳人选:Jeff Prosise

When do you use ON_COMMAND and when do we use ON_MESSAGE. What are the differences between them.

解决方案

ON_COMMAND is specifically used to handle a command message (i.e. WM_COMMAND) like the click of a button/menu item/toolbar button.

ON_MESSAGE is more generic and can be used for any windows message. It is usually used for less frequently handled messages for which specific message map macros have not been provided. You can use ON_MESSAGE to handle ON_COMMAND messages as well but you will have to extract message information (i.e. command ID) yourself.

Example:

See here:

In the message map:

ON_MESSAGE( WM_COMMAND, OnMyCommand )

The handler:

LRESULT CMyWnd::OnMyCommand( WPARAM wParam, LPARAM lParam ) 
{
   // ... Handle message here
   int commandId = LOWORD(wParam);

   switch(commandId){
   case ID_HELLOCOMMAND:
       MessageBox(0, "Hello there!", "ID_HELLO_COMMAND", MB_OK);
       break;
   // ... other commands here
   }

   return 0L;
}

Disclaimer: Owing to MFC's message pump mechanism, you may have to do a bit more than what's shown above. The best man to ask: Jeff Prosise

这篇关于消息映射宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆