There are several methods to implement VC custom messages:
I. Reload the pretranslatemessage Function
For example:
Bool ctunnelsdlg: pretranslatemessage (MSG * PMSG)
{
If (PMSG-> message = bn_clicked)
{
M_stredit = "response successful! ";
This-> updatedata (false );
}
Return cdialog: pretranslatemessage (PMSG );
}
From window
Add in. h file
Protected:
Afx_msg void onbutton (uint NID );
Add in the. cpp File
Begin_message_map (cvnslview, cview)
// {Afx_msg_map (cvnslview)
On_wm_destroy ()
On_wm_create ()
//} Afx_msg_map
On_command (idc_button1, onbutton)
End_message_map ()
// Idc_button1 is the ID of the button you want to click in the window
Void cvnslview: onbutton (uint NID)
{
// Send a message to the parent window and click the message
: Postmessage (this-> getparent ()-> getsafehwnd (), bn_clicked, NID, 0 );
}
Ii. Overload virtual function defwindowproc
First, define a global handle.
Hwnd gwnd_mydlgwnd ;//
Second, in the window you need to respond (class ctestdlg: Public cdialog), declare it as an external reference.
Extern hwnd gwnd_mydlgwnd;
At the same time, the virtual function defwindowproc is overloaded under this class to receive messages.
Lresult ctestdlg: defwindowproc (uint message, wparam, lparam)
{
// Todo: add your specialized code here and/or call the base class
Switch (Message)
{
Case 0x401: MessageBox ("Test Program ");
Break;
}
Return cdialog: defwindowproc (message, wparam, lparam );
}
Again, in the window initialization function (bool ctestdlg: oninitdialog () where you want to obtain the message, add
// Todo: add extra initialization here
Gwnd_mydlgwnd = getsafehwnd ();
Ready
Then, send the message where you want to send the message.
: Postmessage (gwnd_mydlgwnd, 0x401, 0l, 0l); // send a message