Implementation of custom messages in VC windows

Source: Internet
Author: User

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

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.