Method of sending and receiving VC custom messages

Source: Internet
Author: User

The following is a self-created dialog class (MYMESSAGEDLG) to the View class (Messagetestview)
Send a custom message as an example of the two different methods of customizing the message

Method of message Delivery one: using On_message
Using on_message response messages, you must match the definition message #define WM_MY_MESSAGE (wm_user+100)

For those who send the message,-MYMESSAGEDLG,
In its MyMessageDlg.h, define #define WM_MY_MESSAGE (WM_USER+100)
In its MyMessageDlg.cpp to add first: #i nclude "MainFrm.h"
Because the cmainframe* definition object is used.
and a function to test the message:
void Mymessagedlg::onbuttonmsg ()
{
Todo:add your control notification handler code here
cmainframe* pmf= (cmainframe*) AfxGetApp ()->m_pmainwnd; First by getting the current frame pointer
CView * active = Pmf->getactiveview ()//To get current view class pointer
if (active!= NULL)//Gets the current view class pointer to send a message
Active->postmessage (wm_my_message,0,0); Send a message using PostMessage
}

For the recipient of the message,-messagetestview,
In its MessageTestView.h, you also define #define WM_MY_MESSAGE (WM_USER+100)
and define the message mapping function-onmymessage ()
Protected
{{afx_msg (Cmessagetestview)
afx_msg lresult OnMyMessage (WPARAM WPARAM, LPARAM LPARAM);
}}afx_msg
Declare_message_map ()
In its MessageTestView.cpp,
To declare a response message first:
Begin_message_map (Cmessagetestview, CEditView)
{{Afx_msg_map (Cmessagetestview)
On_message (Wm_my_message, OnMyMessage)
}}afx_msg_map
Add a function implementation of the message response:
Lresult Cmessagetestview::onmymessage (WPARAM WPARAM, LPARAM LPARAM)
{
MessageBox ("onmymessage!");
return 0;
}


Method of message Delivery two: using On_registered_message
Use on_registered_message Registration message, must cooperate with
Static UINT wm_my_message=registerwindowmessage ("message");

For the sender of the message,
in its MyMessageDlg.h, as long as
defines static UINT wm_my_message=registerwindowmessage ("-mymessagedlg" ); The
is OK. The
is added first in its MyMessageDlg.cpp: #i nclude "MainFrm.h"
because the cmainframe* definition object is used.
and a function to test the message:
void mymessagedlg::onbuttonmsg ()
{
//Todo:add your control notification handler code here< br> cmainframe* pmf= (cmainframe*) AfxGetApp ()->m_pmainwnd; First by getting the current frame pointer
CView * active = Pmf->getactiveview ();//To get the current view class pointer
if (active!= NULL)//Get the current view class pointer to send the message
Active->postmessage (wm_my_message,0,0); Send a message using PostMessage
}

For the recipient of the message-messagetestview,
Do not define in its MessageTestView.h
Static UINT wm_my_message=registerwindowmessage ("message");
This definition should be placed in the MessageTestView.cpp and not appear: redefinition
Just define the message mapping function in its MessageTestView.h
Protected
{{afx_msg (Cmessagetestview)
afx_msg lresult OnMyMessage (WPARAM WPARAM, LPARAM LPARAM);
}}afx_msg
Declare_message_map ()
In its MessageTestView.cpp, first 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
The last function implementation to add a message response:
Lresult Cmessagetestview::onmymessage (WPARAM WPARAM, LPARAM LPARAM)
{
MessageBox ("onmymessage!");
return 0;
}
----------------------------------------------------------------
Compare two methods, just slightly different. But be careful not to receive a message.

-------------------------------------------------------------------

Additional considerations:

Before you send a message, you also define the-mymessagedlg.cpp
Static UINT wm_my_message=registerwindowmessage ("message");

Before you accept a message, you also define the-messagetestview.cpp
Static UINT wm_my_message=registerwindowmessage ("message");

RegisterWindowMessage ("message") in the content of what is not important, write anything can, but
The sender and the recipient must be the same content, such as: "Message"


cmainframe* pmf= (cmainframe*) AfxGetApp ()->m_pmainwnd; First by getting the current frame pointer
CView * active = Pmf->getactiveview ()//To get current view class pointer
To run this error, you need to change 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.