MFC child windows send messages to the parent window

Source: Internet
Author: User

 MFC child window sends a message to the parent window (test succeeded 20110117)Category: VC + + Learning 2012-08-17 17:22 4327 People read Comments (4) favorite reports Test MFCUSERC

In MFC, using a user-defined message, a child window sends a message procedure, code to the parent window:

1) Add the definition in resourse.h:

#define WM_ADD_EVENT_OK (wm_user + 100)//This definition method is not tested, do not know the line is not good;
static UINT Wm_add_event_ok = RegisterWindowMessage (_t ("User"));//tested, feasible.

2) in the parent window, in the. h file, add the definition:

Protected

afx_msg LRESULT Onadd_event_ok (WPARAM WPARAM, LPARAM LPARAM);

Two parameters: WPARAM WPARAM, LPARAM LPARAM type cannot be changed.

3) In the parent window, add the message map section to the. cpp file:

On_message (WM_ADD_EVENT_OK, ONADD_EVENT_OK)//have not been tested, do not know whether it is feasible;
On_registered_message (WM_ADD_EVENT_OK,ONADD_EVENT_OK)//test pass, feasible.

4) Define the implementation of the message handler function in the parent window's. CPP:

LRESULT class Name:: Onadd_event_ok (WPARAM WPARAM, LPARAM LPARAM)
{
int c=10;
return 0;
}

5) Use the code at the point where you want to send the message:

WPARAM a=8;
LPARAM b=9;

GetParent ()->sendmessage (WM_ADD_EVENT_OK,A,B);

Or send a message using the following code:

HWND hwnd =:: GetParent (M_hwnd);

:: SendMessage (HWND,WM_ADD_EVENT_OK,A,B);

SendMessage (wm_add_event_ok,a,b);//In the test, when you do not use GetParent (), the message cannot be passed to the parent window's message handler, only if you add GetParent (), Message to be sent successfully.

MFC child windows send messages to the parent window

Related Article

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.