Various custom messages (below)

Source: Internet
Author: User

In the previous sections, we discussed how to transmit messages in our own classes and how to transmit messages to groups. The next question is how to transmit messages to different classes. In fact, in parsing pointers in VC ++ 6, we discussed how to obtain pointers of other classes in different classes ,, by using this method, we can easily transmit messages to various classes as needed.
First, the method for defining messages is the same as what we mentioned above (for example, sending messages to the View class in the framework class ):
(1) define the message value in view class. cpp: # define wm_msg (wm_user + 101)
(2) First add the message declaration to the afx_msg block: In cmyview. H, locate the following section and add the message declaration:
Protected:
// {Afx_msg (cmyview)
......
Afx_msg lresult onmymsg (wparam, lparam );
File: //} afx_msg
(3) Add the on_message macro command to the message_map block:
Begin_message_map (cmyview, cview)
File: // {afx_msg_map (cmyview)
.....
On_message (wm_msg, onmymsg)
File: //} afx_msg_map
End_message_map ()
(4) Add the message function body:
Lpesult cmyview: onmymsg (wparam, lparam)
{
Afxmessagebox ("Message received! ");
Return 0;
}
(5) add test functions to the main framework
Void cmainframe: ontestmsg ()
{
Cview * pview = getactiveview (); // obtain the current video class pointer
If (pview! = NULL) pview-> postmessage (wm_msg, 0, 0 );
}
Here, we can see that as long as we have a way to get a pointer to the target class for sending messages, we can send any message, let's go!

Communicate with other applications
The message passing mentioned above is based on the same application, but in some cases, we may need to send messages to other applications. In this case, we can use sendmessage () the function sends a message to the handle of a window of the target application. The trick is to obtain the handle of the window. At the same time, use the registerwindowmessage () function to create a unique message, and both applications understand the meaning of the message. The brodcastsystemmessage () function is also used to send messages to the main window of each application in the system. In this way, you can avoid the problem of obtaining another application window handle. The broadcastsystemmessage () function provides the bsf_lparampointer flag that can be used to convert the pointer written to the lparam parameter to a pointer that can be used by the target program to access the program space. However, this flag may not have been standardized.
The method is as follows:
Register your own window message first. However, we do not need the wm_user + 1 technology this time. The advantage of registering window messages is that we do not have to worry about whether the message identifier indicated by wm_user is beyond the permitted scope of the project after a certain number is added. In this example, messages are registered using text strings in both projects. Since this text string should be unique throughout the system, a COM technology called guid will be used to name messages. The GUID name Generator program can be found in the/bin directory of MFC and Its executable file name is guidgen. EXE. This program will generate a text string that is considered to be unique within the scope known to the application, which is of course the best for the application.
1) register a unique window message
Use guidgen. EXE to generate a guid.
In the application, define the guid as the text string of the window message: # define hello_msg "{6047ccb1-e4e7-11d1-9b7e-00aa003d8695 }"
Use: registerwindowsmessage () to register the text string of the window message: idhellomsg =: registerwindowmessage (hello_msg );
Save the message identifier idhellomsg for future use.
2) send messages to other applications
Use the following code to send a message with the message identifier returned by registerwindowsmessage:
: Sendmessage (hwnd, idhellomsg, wparam, lparam );
The code above assumes that you can obtain the handle of a window of the target application in some way in advance. A pointer pointing to the cwnd class cannot play a role outside the scope of the program. However, you can encapsulate the obtained window handle in the cwnd class and send messages as follows:
Cwnd WND;
WND. Attach (hwnd );
WND. sendmessage (idhellomsg, wparam, lparam );
3) receive registered window messages
To receive registered window messages, you must add the on_registered_message macro to message ing in the receiving window class:
Begin_message_map (cmainframe, cmdiframewnd)
// {Afx_msg_map (cmainframe)
//} Afx_msg_map
On_registered_message (idhellomsg, onhellomsg)
End_message_map ()
The code for the message processing function for registered messages is as follows:
Lresult cmainframe: onhellomsg (wparam, lparam)
{
// Process message
Return 0;
}
So far, this instance has been assumed that a window handle of the target application can be obtained in some way in advance. However, this is a difficult task. A simple way is to broadcast a message to each application and expect the target program to be listening. Because a unique message is registered in the system, only the target program will respond to the message. The message broadcast by the application may be its own window handle, so the receiver can use: sendmessage () to send a response, or use a window handle to end the loop.
4) broadcast window message
Use the following code to broadcast window messages:
Wparam = xxx;
Lparam = xxx;
DWORD dwrecipients = bsm_applications;
: Broadcastsystemmessage (bsf_ignorecurrenttask, & dwrecipients, idhellomsg, wparam, lparam );

 

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.