Recently, vc2008 was used for development. Later, due to the need to send messages and customize messages, I found many examples on the Internet. According to what they said, although they are generally similar, but basically none of them can be completely done. You need to know that there is a small error in VC programming, which may make you dizzy. I will not only explore it later, but I will not make any mistakes after improvement. I will post it for your reference.
1. Let's first introduce the basic knowledge.
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;
2. Example of sendmessage usage
For example, you can use the following statement:
Void ctscrollwinview: onlbuttondblclk (uint nflags, cpoint point)
{
// Todo: add the message processing program code and/or call the default value here
: Sendmessage (afxgetmainwnd ()-> m_hwnd, wm_childframedbclk, 0, 0 );
Cscrollview: onlbuttondblclk (nflags, point );
}
This is a piece of code in a multi-document option card project created under vc2008, it is to send a message without parameters (its ID is wm_user + 1) to the main window after double-clicking in the subwindow. The above is part of the Code, where wm_childframedbclk is the custom message id, afxgetmainwnd ()-> m_hwnd is used to obtain the main window (getparent ()-> m_hwnd or getparentframe ()-> m_hwnd, because this is to obtain the parent window, however, the parent window is not necessarily the main window, so be sure to pay attention to it. Otherwise, an error will be sent and the message cannot be received. I am stuck here for a long time. Then a man in the QQ group told me that, thank you again );
3. Define in the header file of the message receiving form and thread:
# Define wm_childframedbclk wm_user + 1 // do something
4. Then define a function for message ing, as shown below:
Afx_msg lresult onchreceivframedbclick (wparam, lparam );
Note that the format must be: two parameters are required, and the return type must be lresult. Many articles on the Internet ignore these two parameters, which is also a common error in online articles.
5. Add message function ing
On_message (wm_childframedbclk, onch?framedbclick)
Note that the message must be on_message and on_command cannot be used. The former is mainly for user-defined messages, and the latter is
Wm_command command, such as menus and toolbar.
6. Message functions:
We define a process in the receiving form ),
Lresult cmainframe: onchreceivframedbclick (wparam, lparam)
{
Cancelfullscreenwin ();
Return 0;
}
7. The following is part of the code. It is enough to check the code for custom message sending.
In the mainfrm. h header file, the message is defined as follows:
# Define wm_childframedbclk wm_user + 1
Then declare the message function to be mapped in mainfrm. cpp as follows:
Afx_msg lresult onchreceivframedbclick (wparam, lparam );
Add the ing in mainfrm. cpp as follows:
Begin_message_map (cmainframe, cmdiframewndex)
On_wm_create ()
On_command (id_fullscreen, onviewfullscreen)
On_message (wm_childframedbclk, onchreceivframedbclick) // Add the ing here.
End_message_map ()
Then the onchreceivframedbclick () method is implemented in mainfrm. cpp, as follows:
Lresult cmainframe: onchreceivframedbclick (wparam, lparam)
{
Cancelfullscreenwin (); // The function defined by yourself. You can complete the services you need here.
Return true;
}
Finally, you can send the message to the main window, as shown below:
Void ctscrollwinview: onlbuttondblclk (uint nflags, cpoint point)
{
// Todo: add the message processing program code and/or call the default value here
: Sendmessage (afxgetmainwnd ()-> m_hwnd, wm_user + 1, 0, 0 );
Cscrollview: onlbuttondblclk (nflags, point );
}
To sum up, ① note that the ID of the defined message cannot be repeated. You must use wm_user + N;
② Note that the returned value of the defined message function must be lresult, and the two parameters must be wparam and lparam respectively;
③ Use on_message instead of on_command for ing;
④ Note that the messages sent to the main window must be obtained using afxgetmainwnd ()-> m_hwnd in the cxxxxview class.
This is the end of the lecture. I hope it will help you find resources that are not correct.