Custom messages and parameter settings for message transmission

Source: Internet
Author: User

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.

 

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.