How to Use sendmessage to customize the message function (VC instance)

Source: Internet
Author: User
How to Use the sendmessage custom message function in VC:

The basic structure of sendmessage is as follows:
Sendmessage (
Hwnd, // handle of the target window or thread for message transmission.
Uint MSG, // message category (this can be system messages or custom messages, which will be described below ,)
Wparam, // parameter 1 (wparam is actually of the same type as uint,
// Right-click the VC compiler and you can view the option "go to wparam definition.
Lparam); // parameter 2
The origin of some parameters is as follows:
// Typedef unsigned int uint;
// Typedef uint wparam;
// Typedef long lparam;
// Typedef long lresult;
For example, you can use the following statement:
: Sendmessage (this-> m_hwnd, wm_my_dosome, (wparam) 0, (lparam) 0 );
Here, the message I sent is received in this form, so the handle is: This-> m_hwnd
Here, the message category wm_my_dosome is my custom,
In the header file of the form or thread that receives the message:
# Define wm_my_dosome wm_user + 1 // do something
Of course, you can also define more such:
# Define wm_doother wm_user + 2 // do other

Indicates to do something.
At this point, you may still be confused about the message category. Don't worry, I will talk about it soon.
When we send a message, the receiver must be able to identify what the message is, that is, to distinguish it by message category,
And start to process the message. As follows:
I. Write one thing:
We define a process in the receiving form ),
Afx_msg lresult dosomething (wparam iparam1, lparam iparam2)
{
MessageBox ("after receiving the message, I want to do something. "," Received ", mb_ OK );
// You can use iparam1 and iparam2 to do something.
Return 0;
}
Note the following three important points:
1: afx_msg is used, and afx_msg lresult dosomething (wparam iparam1, lparam iparam2)
Rewrite to the header file
// {Afx_msg
//... Here, the color turns gray. This is very important.
//} Afx_msg
2: There are two parameters: wparam iparam1 and lparam iparam2. If nothing is passed in, it will be written. Otherwise, the system will not remind you to write less than one parameter in the VC,
But some strange things will happen.
3: type with lresult, return 0 after completion;
2. Let the recipient know when to do this:
In
// {Afx_msg_map
//... Write it here
On_message (wm_my_dosome, dosomething)
// If there are other messages, write another one.
On_message (wm_doother, doother)
//} Afx_msg_map
Here, when you use sendmessage to send a message of the wm_my_dosome type,
The Receiver performs dosomething (wparam iparam1, lparam iparam2)
When a wm_doother type message is sent,
The receiver will do doother (wparam iparam1, lparam iparam2). Of course, I have not defined doother here,
This is a complete message sending and receiving process. I didn't elaborate on the parameter iparam1, because it hasn't been very complicated yet,
If you have some experiences, we can improve them together.

In the header file
# Define wm_mymsg wm_user + 5 // customize a message


Afx_msg void onmymessage (wparam, lparam); // process function declaration for custom messages



In the. cpp File
On_message (wm_mymsg, onmymessage)
// Use the on_message () macro to establish a ing between a custom message and its processing function

Void cmodelessdlg: onmymessage (wparam, lparam)
// Retrieve the pointer of the cstring object from lparam and display the string content in idc_msgedit
{

Cstring * STR;
STR = (cstring *) lparam;

Setdlgitemtext (idc_edit, * Str );
}


Press the button to send a message
Void cmodelessdlg: onmsgbtn ()
{

Cstring STR = "the custom message is triggered! ";

Sendmessage (wm_mymsg, 0, (lparam) & Str );
// Send a custom message to modelessdlg.

}

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.