Two Methods for sending and receiving custom messages

Source: Internet
Author: User
Two Methods for sending and receiving custom messages
Programmer's house Author: Unknown Date: 18:51:00

Note:
The following uses a self-created dialog box class (mymessagedlg) to view (messagetestview)
Sending a custom message is used as an example to describe


Summary:
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. in H, define # define wm_my_message (wm_user + 100)
In its mymessagedlg. in CPP, add: # include "mainfrm. H "
because cmainframe * is used to define an object.
function with Test message:
void mymessagedlg: onbuttonmsg ()
{< BR class = ""> // todo: add your control notification handler code here
cmainframe * PMF = (cmainframe *) afxgetapp ()-> m_pmainwnd; // obtain the current framework 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 a message
}

for the message receiver-messagetestview,
In its messagetestview. in H, define # define wm_my_message (wm_user + 100)
and define the message ing function-onmymessage ()
protected:
// {afx_msg (cmessagetestview)
afx_msg lresult onmymessage (wparam, lparam );
//}} afx_msg
declare_message_map ()
In its messagetestview. in CPP,
declare the response Information:
begin_message_map (cmessagetestview, ceditview)
// {afx_msg_map (cmessagetestview)
on_message (wm_my_message, onmymessage)
///} afx_msg_map
Add a message response function implementation:
lresult cmessagetestview: onmymessage (wparam, lparam)
{< BR class = "> MessageBox (" onmymessage! ");
return 0;
}


Method 2: Use on_registered_message
on_registered_message to register a message, must be used with
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 mymessagedlg. cpp, add: # include "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. It is mandatory.
The sender and receiver are the same content, for example: "message"

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.