VC custom message transmission

Source: Internet
Author: User

Send custom messages

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. H, define # define wm_my_message (wm_user + 100)
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 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, 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, 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 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"

Be careful when using message transmission-an issue that requires attention when being transferred to multiple view classes

 

In my previous article, I talked about the implementation of two methods for sending and receiving custom messages.
However, when I use message transmission at one time, I also encounter a confusing problem,
Fortunately, it was finally solved.

-------------------------------------------------------------
I used to transmit a message.
Cmainframe * PMF = (cmainframe *) afxgetapp ()-> m_pmainwnd; // obtain the current frame pointer
Cview * active = PMF-> getactiveview (); // obtain the current video class pointer
If (active! = NULL)
Active-> postmessage (wm_my_message,); // use postmessage to send messages
To send my defined messages.
The message was successfully received in the View class to be received.

However, when a class defines two custom messages and sends them to two different view classes,
If the above Code is still used, some problems may occur.

Why is there a problem when I copy it?

Note the following:
In the above Code, how does one obtain the View class pointer.
The getactiveview () function is used to obtain the "current" view pointer.

This is why one of the view classes cannot receive messages due to a problem.

------------------------------------------------------------
The following describes in detail the error process and solution process.

Source: an SDI is split statically into two parts equal to the left and right, and the two parts point to the two new classes created by myself: cmyleftview and cmyrightview, their base classes are ceditview. Therefore, the two view classes that are preparing to receive messages have nothing except the names.

A dialog box (cmyfinddialog class) contains two radio button controls and a confirmation button,
When selecting the two radio button controls, they can only be selected. They correspond to the left and right sides of the preceding two controls. After selecting them, click "OK, the view to which the message is sent.

After sending and receiving are completed, only one message can be received,
Void cmyfinddialog: onbuttonfind ()
{Mainframe * PMF = (cmainframe *) afxgetapp ()-> m_pmainwnd;
Cview * active = PMF-> getactiveview (); // obtain the current video class pointer
// If the error occurs, the active object obtained in the previous sentence is an "uncertain" object.
If (m_selleft = true)
{If (active! = NULL)
Active-> postmessage (wm_my_message_left, 0, 0 );
}
Else if (m_selright = true)
{If (active! = NULL)
Active-> postmessage (wm_my_message_right, 0, 0 );
}
Else
MessageBox ("must choose one! ");
}

The current class pointer obtained through getactiveview (), then
If the focus (cursor) is on the left before you open the dialog box, the message is received on the left, and the message is not received on the right.
If the focus (cursor) is on the right before you open the dialog box, the right side can receive the message, and the left side cannot receive the message.
After the program runs, the focus (cursor) is on the left by default, which leads to a problem where I always receive messages on the right.
In fact, if you change the focus (cursor) to the right after running the program, and then open the dialog box to test sending and receiving, it becomes
Messages cannot be received on the left and messages can be received on the right. (The error was found only when someone else reminded me in the Forum. Otherwise, you may still be wondering if some problems have occurred .)

For such a message to two view classes, you must pay attention to who is executing the postmessage object!
The correct code is as follows:
Void cmyfinddialog: onbuttonfind ()
{Cmainframe * PMF = (cmainframe *) afxgetapp ()-> m_pmainwnd;
Cmyleftview * PLV = (cmyleftview *) PMF-> m_wndsplitter.getpane (0, 0 );
Cmyrightview * PRV = (cmyrightview *) PMF-> m_wndsplitter.getpane (0, 1 );
// For my question, it is obtained by splitting the object m_wndsplitter's getpane function and is determined
// View class pointer.
// If you are using multiple view classes, but not splitting, but other view classes, pay attention
// Obtain a pointer to the view object you are sure to view.
If (m_selleft = true)
{If (PLV! = NULL)
PLV-> postmessage (wm_my_message_left, 0, 0 );
}
Else if (m_selright = true)
{If (PRV! = NULL)
PRV-> postmessage (wm_my_message_right, 0, 0 );
}
Else
MessageBox ("must choose one! ");
}

Summary:
When you want to send messages to multiple view objects, consider the view objects you want to send messages.

If you send messages to the current view object of multiple view objects
The method is correct for you.

However, if you do not send a message to the current view object, but have a clear goal, pay attention
Some method must obtain a definite view pointer,
When you use (view *)> postmessage (,) to send a message,
The recipient can receive your messages.

How do I transmit a string in a custom message (I have a method )?
Sender:
Cstring mystr;
Char * Buf;
Buf = (char *) New char [100];
Mystr = "thank you for choosing wtzyb4446 and stonespace below :";
Strcpy (BUF, mystr );
: Sendmessage (pframe-> m_wndworkspace.getactiveview ()-> getsafehwnd (), wm_user_query, (wparam) BUF, (lparam) mystr. getlength ());

Receiver:
Char * dd;
Dd = (char *) wparam;
Int num = (INT) lparam;
For (INT I = 0; I <num; I ++)
{
STR + = dd [I];
}
Delete dd;

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.