萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 編程語言綜合 >> 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"


CMainFrame* pMF=(CMainFrame*)AfxGetApp()->m_pMainWnd; //先通過獲取當前框架指針
CView * active = pMF->GetActiveView();//才能獲取當前視類指針
運行此出錯的,需要改成CMainFrame * hwd = (CMainFrame *)AfxGetMainWnd();

copyright © 萬盛學電腦網 all rights reserved