Vc custom message sending and receiving Methods

Source: Internet
Author: User

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 ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.