vc自定義消息的發送與接收的方法實現

 

vc自定義消息的發送與接收的方法實現

以下用一個自創的對話框類(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中要先添加:#i nclude "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中要先添加:#i nclude "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"

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章