The following uses a self-created dialog box class (MyMessageDlg) to view (MessageTestView)
Sending a custom message is used as an example to describe
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 wParam, LPARAM 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 wParam, LPARAM 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"
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 this error occurs, change it to CMainFrame * hwd = (CMainFrame *) AfxGetMainWnd ();