The basic structure of the SendMessage is as follows:
SendMessage (
hwnd hwnd,//Message delivery target window or thread handle.)
UINT MSG,//message category (This can be some system messages, or it can be your own definition, described below,)
WPARAM WPARAM,//Parameter 1 (WPARAM is actually the same type as uint,
//The right key in the VC compiler has a "go to WPARAM definition" option 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: This->m_hwnd
The message category Wm_my_dosome here is my custom,
In the header file of the form or thread where the message is received:
#define Wm_my_dosome wm_user+1//do something
Of course you can also define more such as:
#define Wm_doother wm_user+2//Do other
Say something to do.
Here, maybe everyone is still a little vague about the message category, don't worry, I'll talk about it soon.
We sent out a message, so the receiver had to be able to identify what the message was for, or to differentiate it by the message category,
and start to do the information that corresponds to the things to be dealt with. As follows:
One: Write one thing:
We define one such thing (process) in the receiving form,
afx_msg lresult dosomething (WPARAM iparam1,lparam iParam2)
{
MessageBox ("received the message, I'm going to start doing something." "," received ", MB_OK);
You can use IPARAM1,IPARAM2 to do something. 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)
Overwrite the header file
{{afx_msg
。。。 Rewrite it here, the color will turn gray. This is very important.
}}afx_msg
2: There are 2 parameters, WPARAM Iparam1,lparam iParam2, even if there is no thing to write, or will suffer, the VC will not remind you to write a less,
But some inexplicable and wonderful things will happen.
3: Type with LRESULT, finished to return 0;
Second: Let the receiver know when to do this thing:
We are
{{Afx_msg_map
//... Write
a on_message (wm_my_dosome,dosomething)
//
////////////////////////
}}afx_msg_map
Here, when you use SendMessage to send out a wm_my_dosome type of message over,
The receiver is going to do dosomething (WPARAM iparam1,lparam iParam2)
Sent a wm_doother type of news over the time,
The receiver will do the DoOther (WPARAM iparam1,lparam iParam2) Of course, here doother I have not defined,
This is a complete message send and receive process, I did not detail the parameters, iParam1, because it has not been used in a very complicated situation,
We can improve together if we have a heart.
In the header file.
Copy Code code as follows:
#define WM_MYMSG wm_user+5//Customize a message
afx_msg void OnMyMessage (WPARAM WPARAM, LPARAM LPARAM); Custom message processing function declaration
In the. cpp file
On_message (wm_mymsg, OnMyMessage)
Using the On_message () macro to establish a mapping relationship between a custom message and its processing function
void Cmodelessdlg::onmymessage (WPARAM WPARAM, LPARAM LPARAM)
Remove the CString object's pointer from the lparam and display the string contents in the Idc_msgedit
The above mentioned is the entire content of this article, I hope you can enjoy.