Sometimes the window needs to receive and send messages to achieve a certain effect or purpose. In addition, you can customize messages and include parameters.
This article introduces how to set custom messages and parameters.
//////////////////////////////////////// /////////////////
Create a dialog box project named Dialog
The dialogdlg. h Class header file defines the customer message macro, as shown below:
# Define wm_myownmessage wm_user+ 100
The following message processing functions are declared in the dialogdlg. H class:
Afx_msg void onownmessage (wparam WP, lparam LP );
In dialogdlg. cpp message ing, the function for associating custom messages with message processing is as follows:
Begin_message_map (cdialogdlg, cdialog)
// {Afx_msg_map (cdialogdlg)
On_message (wm_myownmessage, onownmessage)/* Associate message ing */
//} Afx_msg_map
End_message_map ()
Dialogdlg. cpp function implementation
Void cdialogdlg: onownmessage (wparam WP, lparam LP)
{
// Add processing here
}
Ready...
The rest is to send the message to cdialogdlg at the call. Request cdialogdlg to respond.
//////////////////////////////////////// /////////////////
String parameters ------
Send:
Char * szsend = "this is text ";
Sendmessage (hwnddlg, wm_myownmessage, (wparam) (char *) sztext, 0 );
Accept:
Char szreceive [128];
Memcpy (& szreceive, (char *) WP, 128); // handle overflow, which is not described here
MessageBox (szreceive );
Struct parameters ------
Typedef struct _ taginfo {
Char name [128];
Int age;
} Info, * lpinfo;
Send:
Info;
Memset (& info, 0, sizeof (Info ));
Info. Age = 12;
Strcpy (info. Name, "Jack ");
: Sendmessage (hwnddlg, wm_myownmessage, (wparam) (lpinfo) & info, 0 );
Receive:
Info rinfo;
Memcpy (& rinfo, (lpinfo) WP, sizeof (Info ));
MessageBox (rinfo. Name );
Note that
1. Message Parameter Life Cycle Problem. When the function returns, the space allocated in the stack is recycled, and the parameter is invalid.
2. Differences between sendmessage () and postmessage.