vc++之自定义消息案例研习

来源:互联网 发布:淘宝上比较好的眼镜店 编辑:程序博客网 时间:2024/05/22 06:32

一 案例:

消息映射、循环机制是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

 

二 自定义消息的发送与接收

 

说明:
以下用一个自创的对话框类(MyMessageDlg)向视图类(MessageTestView)
发送自定义消息为例,说明这两种不同方法的自定义消息的


总结:
消息传递的方法一:使用ON_MESSAGE
使用ON_MESSAGE响应消息,必须配合定义消息#define WM_MY_MESSAGE (WM_USER+100)

对于发送消息者-MyMessageDlg,
在其MyMessageDlg.h中,定义#define WM_MY_MESSAGE (WM_USER+100)
在其MyMessageDlg.cpp中要先添加:#include "MainFrm.h"
因为使用了CMainFrame*定义对象。
并且要有测试消息的函数:
void MyMessageDlg::OnButtonMsg()
{
    // TODO: Add your control notification handler code here
    CMainFrame* pMF=(CMainFrame*)AfxGetApp()->m_pMainWnd;  //先通过获取当前框架指针
    CView * active = pMF->GetActiveView();//才能获取当前视类指针
    if(active != NULL)  //获取了当前视类指针才能发送消息
    active->PostMessage(WM_MY_MESSAGE,0,0);   //使用PostMessage发送消息
}

对于消息的接受者-MessageTestView,
在其MessageTestView.h中,也要定义#define WM_MY_MESSAGE (WM_USER+100)
并定义消息映射函数-OnMyMessage()
protected:
 //{{AFX_MSG(CMessageTestView)
 afx_msg LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam);
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
在其MessageTestView.cpp中,
先要声明响应消息:
BEGIN_MESSAGE_MAP(CMessageTestView, CEditView)
 //{{AFX_MSG_MAP(CMessageTestView)
 ON_MESSAGE(WM_MY_MESSAGE, OnMyMessage)
 //}}AFX_MSG_MAP
再添加消息响应的函数实现:
LRESULT CMessageTestView::OnMyMessage(WPARAM wParam, LPARAM lParam)
{
 MessageBox("OnMyMessage!");
 return 0;
}


消息传递的方法二:使用ON_REGISTERED_MESSAGE
使用ON_REGISTERED_MESSAGE注册消息,必须配合
static UINT WM_MY_MESSAGE=RegisterWindowMessage("Message");

对于消息的发送者-MyMessageDlg,
在其MyMessageDlg.h中,只要
定义static UINT WM_MY_MESSAGE=RegisterWindowMessage("Message");
就可以了。
在其MyMessageDlg.cpp中要先添加:#include "MainFrm.h"
因为使用了CMainFrame*定义对象。
并且要有测试消息的函数:
void MyMessageDlg::OnButtonMsg()
{
    // TODO: Add your control notification handler code here
    CMainFrame* pMF=(CMainFrame*)AfxGetApp()->m_pMainWnd;  //先通过获取当前框架指针
    CView * active = pMF->GetActiveView();//才能获取当前视类指针
    if(active != NULL)  //获取了当前视类指针才能发送消息
    active->PostMessage(WM_MY_MESSAGE,0,0);   //使用PostMessage发送消息
}

对于消息的接收者-MessageTestView,
在其MessageTestView.h中不要定义
static UINT WM_MY_MESSAGE=RegisterWindowMessage("Message");
应该把这个定义放到MessageTestView.cpp中,要不会出现: redefinition
在其MessageTestView.h中只要定义消息映射函数
protected:
 //{{AFX_MSG(CMessageTestView)
 afx_msg LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam);
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
在其MessageTestView.cpp中,先定义
static UINT WM_MY_MESSAGE=RegisterWindowMessage("Message");
接着注册消息:
BEGIN_MESSAGE_MAP(CMessageTestView, CEditView)
 //{{AFX_MSG_MAP(CMessageTestView)
        ON_REGISTERED_MESSAGE(WM_MY_MESSAGE,OnMyMessage)
 //}}AFX_MSG_MAP
最后添加消息响应的函数实现:
LRESULT CMessageTestView::OnMyMessage(WPARAM wParam, LPARAM lParam)
{
 MessageBox("OnMyMessage!");
 return 0;
}
----------------------------------------------------------------
比较两种方法,只是略有不同。但也要小心谨慎,以免出现接收不到消息的情况。

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

其他注意事项:

发送消息的-MyMessageDlg.cpp前也要定义
static UINT WM_MY_MESSAGE=RegisterWindowMessage("Message");

接受消息的-MessageTestView.cpp前也要定义
static UINT WM_MY_MESSAGE=RegisterWindowMessage("Message");

RegisterWindowMessage("Message")中""的内容是什么不重要,写什么都可以,单必须
发送者与接受者是一样的内容,例如:"Message"

原创粉丝点击