MFC child windows send messages to the parent window (test succeeded 20110117)

Source: Internet
Author: User

In MFC, using user-defined messages, child windows Send message procedures, 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 how to do;
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 Add the Message Mapping section in the parent window's. cpp file:

On_message (WM_ADD_EVENT_OK, ONADD_EVENT_OK)//No test, do not know whether feasible;
On_registered_message (WM_ADD_EVENT_OK,ONADD_EVENT_OK)//test pass, feasible.

4 defines 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 code when you need to send a 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 the GetParent ()-> is not used, the message cannot be delivered to the message handler function of the parent window, and only after the GetParent ()-> is added, The message can be sent successfully.

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.