登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

being23

写给未来的自己

 
 
 
 
 

日志

 
 
关于我

真正的坚定,就是找到力量去做自己喜欢的事情,并为之努力,这样才会觉得生活是幸福的。

2011.07.02  

2011-07-02 10:05:24|  分类: Tech |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

来自:MFC中自定义消息  MFC 自定义消息四步曲

 

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

自定义消息的步骤如下:
(1)建立Single Document的MFC Application,工程名为:MyMessage
(2)自定义消息:
第一步:定义消息
在Resource.h中添加如下代码(一般可以加在stdafx.h或resource.h或×××app.h,他们都是全局变量头文件所以都可以):

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

第二步:声明消息处理函数
选择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发送消息

SendMessage(WM_My_MESSAGE, wPARAM, lPARAM)

附: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

Technorati 标签: MFC
  评论这张
 
阅读(295)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018