Method 1: Use ON_MESSAGE
When ON_MESSAGE is used to respond to a message, the message must be defined together with # define WM_MY_MESSAGE (WM_USER + 100)
For the sender-MyMessageDlg,
In its MyMessageDlg. h, define # define WM_MY_MESSAGE (WM_USER + 100)
In its MyMessageDlg. cpp, add: # I nclude "MainFrm. h"
Because CMainFrame * is used to define objects.
Function for testing messages:
Void MyMessageDlg: OnButtonMsg ()
{
// TODO: Add your control notification handler code here
CMainFrame * pMF = (CMainFrame *) AfxGetApp ()-> m_pMainWnd; // first obtain the current frame pointer
CView * active = pMF-> GetActiveView (); // you can obtain the current video class pointer.
If (active! = NULL) // get the current class pointer to send messages
Active-> PostMessage (WM_MY_MESSAGE,); // use PostMessage to send messages
}
For the message recipient-MessageTestView,
In its MessageTestView. h, you must also define # define WM_MY_MESSAGE (WM_USER + 100)
And define the message ing function-OnMyMessage ()
Protected:
// {AFX_MSG (CMessageTestView)
Afx_msg LRESULT OnMyMessage (WPARAM wParam, LPARAM lParam );
//} AFX_MSG
DECLARE_MESSAGE_MAP ()
In its MessageTestView. cpp,
First, declare the Response Message:
BEGIN_MESSAGE_MAP (CMessageTestView, CEditView)
// {AFX_MSG_MAP (CMessageTestView)
ON_MESSAGE (WM_MY_MESSAGE, OnMyMessage)
//} AFX_MSG_MAP
Then add the message response function implementation:
LRESULT CMessageTestView: OnMyMessage (WPARAM wParam, LPARAM lParam)
{
MessageBox ("OnMyMessage! ");
Return 0;
}
Method 2: Use ON_REGISTERED_MESSAGE
Use ON_REGISTERED_MESSAGE to register a message.
Static UINT WM_MY_MESSAGE = RegisterWindowMessage ("Message ");
For the message sender-MyMessageDlg,
In its MyMessageDlg. h, as long
Define static UINT WM_MY_MESSAGE = RegisterWindowMessage ("Message ");
You can.
In its MyMessageDlg. cpp, add: # I nclude "MainFrm. h"
Because CMainFrame * is used to define objects.
Function for testing messages:
Void MyMessageDlg: OnButtonMsg ()
{
// TODO: Add your control notification handler code here
CMainFrame * pMF = (CMainFrame *) AfxGetApp ()-> m_pMainWnd; // first obtain the current frame pointer
CView * active = pMF-> GetActiveView (); // you can obtain the current video class pointer.
If (active! = NULL) // get the current class pointer to send messages
Active-> PostMessage (WM_MY_MESSAGE,); // use PostMessage to send messages
}
For the Message Receiver-MessageTestView,
Do not define in its messagetestview. h
Static uint wm_my_message = registerwindowmessage ("message ");
Put this definition in messagetestview. cpp. Do not see: redefinition
In its messagetestview. H, you only need to define the message ing function.
Protected:
// {Afx_msg (cmessagetestview)
Afx_msg lresult onmymessage (wparam, lparam );
//} Afx_msg
Declare_message_map ()
In its messagetestview. cpp, define
Static uint wm_my_message = registerwindowmessage ("message ");
Then register the message:
Begin_message_map (cmessagetestview, ceditview)
// {Afx_msg_map (cmessagetestview)
On_registered_message (wm_my_message, onmymessage)
//} Afx_msg_map
Finally, add the message response function implementation:
Lresult cmessagetestview: onmymessage (wparam, lparam)
{
MessageBox ("onmymessage! ");
Return 0;
}
----------------------------------------------------------------
The two methods are compared, but they are slightly different. However, be cautious to avoid the possibility that messages cannot be received.
-------------------------------------------------------------------
Other considerations:
Before sending the message-mymessagedlg. cpp, define
Static uint wm_my_message = registerwindowmessage ("message ");
The message-messagetestview. cpp must be defined before receiving the message.
Static uint wm_my_message = registerwindowmessage ("message ");
The content of "" In registerwindowmessage ("message") is not important. You can write anything,
The sender and receiver must be the same content, for example: "message"