vc自定義消息的發(fā)送與接收方法

字號(hào):


    以下用一個(gè)自創(chuàng)的對(duì)話框類(lèi)(MyMessageDlg)向視圖類(lèi)(MessageTestView)
    發(fā)送自定義消息為例,說(shuō)明這兩種不同方法的自定義消息的
    消息傳遞的方法一:使用ON_MESSAGE
    使用ON_MESSAGE響應(yīng)消息,必須配合定義消息#define WM_MY_MESSAGE (WM_USER+100)
    對(duì)于發(fā)送消息者-MyMessageDlg,
    在其MyMessageDlg.h中,定義#define WM_MY_MESSAGE (WM_USER+100)
    在其MyMessageDlg.cpp中要先添加:#i nclude "MainFrm.h"
    因?yàn)槭褂昧薈MainFrame*定義對(duì)象。
    并且要有測(cè)試消息的函數(shù):
    void MyMessageDlg::OnButtonMsg()
    {
    // TODO: Add your control notification handler code here
    CMainFrame* pMF=(CMainFrame*)AfxGetApp()->m_pMainWnd; //先通過(guò)獲取當(dāng)前框架指針
    CView * active = pMF->GetActiveView();//才能獲取當(dāng)前視類(lèi)指針
    if(active != NULL) //獲取了當(dāng)前視類(lèi)指針才能發(fā)送消息
    active->PostMessage(WM_MY_MESSAGE,0,0); //使用PostMessage發(fā)送消息
    }
    對(duì)于消息的接受者-MessageTestView,
    在其MessageTestView.h中,也要定義#define WM_MY_MESSAGE (WM_USER+100)
    并定義消息映射函數(shù)-OnMyMessage()
    protected:
    //{{AFX_MSG(CMessageTestView)
    afx_msg LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam);
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    在其MessageTestView.cpp中,
    先要聲明響應(yīng)消息:
    BEGIN_MESSAGE_MAP(CMessageTestView, CEditView)
    //{{AFX_MSG_MAP(CMessageTestView)
    ON_MESSAGE(WM_MY_MESSAGE, OnMyMessage)
    //}}AFX_MSG_MAP
    再添加消息響應(yīng)的函數(shù)實(shí)現(xiàn):
    LRESULT CMessageTestView::OnMyMessage(WPARAM wParam, LPARAM lParam)
    {
    MessageBox("OnMyMessage!");
    return 0;
    }
    消息傳遞的方法二:使用ON_REGISTERED_MESSAGE
    使用ON_REGISTERED_MESSAGE注冊(cè)消息,必須配合
    static UINT WM_MY_MESSAGE=RegisterWindowMessage("Message");
    對(duì)于消息的發(fā)送者-MyMessageDlg,
    在其MyMessageDlg.h中,只要
    定義static UINT WM_MY_MESSAGE=RegisterWindowMessage("Message");
    就可以了。
    在其MyMessageDlg.cpp中要先添加:#i nclude "MainFrm.h"
    因?yàn)槭褂昧薈MainFrame*定義對(duì)象。
    并且要有測(cè)試消息的函數(shù):
    void MyMessageDlg::OnButtonMsg()
    {
    // TODO: Add your control notification handler code here
    CMainFrame* pMF=(CMainFrame*)AfxGetApp()->m_pMainWnd; //先通過(guò)獲取當(dāng)前框架指針
    CView * active = pMF->GetActiveView();//才能獲取當(dāng)前視類(lèi)指針
    if(active != NULL) //獲取了當(dāng)前視類(lèi)指針才能發(fā)送消息
    active->PostMessage(WM_MY_MESSAGE,0,0); //使用PostMessage發(fā)送消息
    }
    對(duì)于消息的接收者-MessageTestView,
    在其MessageTestView.h中不要定義
    static UINT WM_MY_MESSAGE=RegisterWindowMessage("Message");
    應(yīng)該把這個(gè)定義放到MessageTestView.cpp中,要不會(huì)出現(xiàn): redefinition
    在其MessageTestView.h中只要定義消息映射函數(shù)
    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");
    接著注冊(cè)消息:
    BEGIN_MESSAGE_MAP(CMessageTestView, CEditView)
    //{{AFX_MSG_MAP(CMessageTestView)
    ON_REGISTERED_MESSAGE(WM_MY_MESSAGE,OnMyMessage)
    //}}AFX_MSG_MAP
    最后添加消息響應(yīng)的函數(shù)實(shí)現(xiàn):
    LRESULT CMessageTestView::OnMyMessage(WPARAM wParam, LPARAM lParam)
    {
    MessageBox("OnMyMessage!");
    return 0;
    }
    ----------------------------------------------------------------
    比較兩種方法,只是略有不同。但也要小心謹(jǐn)慎,以免出現(xiàn)接收不到消息的情況。
    -------------------------------------------------------------------
    其他注意事項(xiàng):
    發(fā)送消息的-MyMessageDlg.cpp前也要定義
    static UINT WM_MY_MESSAGE=RegisterWindowMessage("Message");
    接受消息的-MessageTestView.cpp前也要定義
    static UINT WM_MY_MESSAGE=RegisterWindowMessage("Message");
    RegisterWindowMessage("Message")中""的內(nèi)容是什么不重要,寫(xiě)什么都可以,但是
    發(fā)送者與接受者必須是一樣的內(nèi)容,例如:"Message"
    CMainFrame* pMF=(CMainFrame*)AfxGetApp()->m_pMainWnd; //先通過(guò)獲取當(dāng)前框架指針
    CView * active = pMF->GetActiveView();//才能獲取當(dāng)前視類(lèi)指針
    運(yùn)行此出錯(cuò)的,需要改成CMainFrame * hwd = (CMainFrame *)AfxGetMainWnd();