How to use SendMessage custom message function in C + + VC

Source: Internet
Author: User


How to use SendMessage custom message function in VC:

The basic structure of SendMessage is as follows:
SendMessage (
HWND hwnd,//Handle to the target window or thread of the message delivery.
UINT MSG,//message category (here can be some system messages, also can be their own definition, described below)
WPARAM WPARAM,//Parameter 1 (WPARAM is actually the same type as uint,
Right-click on the "Go to wparam definition" option in the VC compiler to view.
LPARAM LPARAM); Parameter 2
Some of these parameters are 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);
The message I sent here is received by this form, so the handle is used: This->m_hwnd
Here's the message category Wm_my_dosome I'm customizing,
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 as:
#define Wm_doother wm_user+2//Do other

means to do something.
Here, you may still be a bit vague about the message category, don't worry, I'll talk about it soon.
We send a message out, then the receiver should be able to identify what the message is, is to distinguish by the message category,
and start to do the message corresponding to the things to be handled. As follows:
One: Write a thing:
We define one such thing in the receiving form (the process),
afx_msg LRESULT dosomething (WPARAM iparam1,lparam iParam2)
{
MessageBox ("I'm getting a message, I'm going to start doing something.") "," received ", MB_OK);
You can use IPARAM1,IPARAM2 to do some things.
return 0;
}
This thing has 3 points to pay attention to, very important:
1: afx_msg is used, and afx_msg LRESULT dosomething (WPARAM iparam1,lparam iParam2)
Overwrite the header file
{{afx_msg
。。。 If you rewrite it here, the color will turn gray. This is very important.
}}afx_msg
2: Parameters have 2, WPARAM Iparam1,lparam iParam2, even if there is no thing to write, or will suffer, VC will not remind you to write one less,
But something strange is going to happen.
3: Type with LRESULT, finished to return 0;
Two: Let the receiver know when to do this thing:
We are
{{Afx_msg_map
。。。 Write it here.
On_message (wm_my_dosome,dosomething)
If there's another message, write another one.
On_message (Wm_doother,doother)
}}afx_msg_map
Here, when you use SendMessage, send a wm_my_dosome type of message come over,
The receiver will do dosomething (WPARAM iparam1,lparam iParam2)
Sent a wm_doother type of message over the time,
The receiver will do DoOther (WPARAM iparam1,lparam iParam2) Of course, here doother I have not defined,
This is a complete message sending and receiving process, I did not elaborate parameters, iParam1, because it has not been used in a very complex situation,
We can improve together if we have a heart.


In the header file.
#defineWm_mymsg wm_user+5//Customizing a message


afx_msgvoidOnMyMessage (WPARAM WPARAM, LPARAM LPARAM);//Declaration of a handler function for a custom message



In the. cpp file
On_message (wm_mymsg, OnMyMessage)
//Using On_message () macros to establish a mapping between custom messages and their processing functions

voidCmodelessdlg::onmymessage (WPARAM WPARAM, LPARAM LPARAM)
//Remove a pointer to the CString object from lparam and display the string contents in Idc_msgedit
{

CString*Str
Str=(CString*) LParam;

Setdlgitemtext (Idc_edit,*STR);
}


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

 cstring str< Span style= "color: #000000;" >=  " Custom messages are triggered! ;

SendMessage (Wm_mymsg, 0,  (LPARAM)  &str);
 // send a custom message to Modelessdlg yourself

}
/span>

How to use SendMessage custom message functions in C + + VC

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.