vc++之自訂訊息案例研習

來源:互聯網
上載者:User

一 案例:

訊息映射、迴圈機制是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"

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.