This article describes the VC SendMessage custom message function usage, share for everyone to reference. Specifically as follows:
The basic structure of the SendMessage is as follows:
Copy Code code 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:
Copy Code code as follows:
:: 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:
Copy Code code as follows:
#define Wm_my_dosome wm_user+1//do something
Of course you can also define more such as:
Copy Code code as follows:
#define Wm_doother wm_user+2//Do other
Say something to do.
Here, you may still have a vague message category, do not worry, the following soon.
We send out a message, and the receiver is able to identify what the message is, to differentiate it by category of messages, and to start doing what the message corresponds to. As follows:
One, write a thing:
We define one such thing (process) in the receiving form,
Copy Code code as follows:
afx_msg lresult dosomething (WPARAM iparam1,lparam iParam2)
{
MessageBox ("I got 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. Use of afx_msg 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 nothing is passed in to write, or will suffer, 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 receiving party know when to do this thing:
we are
Copy Code code as follows:
{{Afx_msg_map
。。。 Here, write it.
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 over, the receiver will do the dosomething (WPARAM iparam1,lparam iParam2)
When a wm_doother type of message comes up, 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, there is no detailed argument, IPARAM1, because it has not been used in a very complicated situation,
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:
Copy Code code as follows:
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
{
CString *str;
Str= (CString *) LParam;
Setdlgitemtext (IDC_EDIT,*STR);
}
Press the button to send a message
Copy Code code as follows:
void Cmodelessdlg::onmsgbtn ()
{
CString str= "Custom message was triggered! ";
SendMessage (wm_mymsg, 0, (LPARAM) &str);
Send a custom message to Modelessdlg
}
I hope this article on the VC program for everyone to help.