In the development of MFC project, it involves the transmission of data information between different modules, such as user name and password to the System Management module after the login interface is successfully logged in.
First, the following two points need to be clarified:
- Who wants to send this message--the message sender
- Who will accept the message-the recipient of the message
In this paper, the object A to object B to send a message as an example to explain, as follows:
1. Message sender: Send Message
This message is first customized in a header file (*.h):
1 #define Wm_user+100
or:
1 Const UINT Wm_user + N
the following points are to be noted:
- All custom messages are represented by an arbitrary natural number N, based on the Wm_user message.
n is generally at least greater than 100
This message is generally added to stdafx.h or resource.h or *.h, and generally are all global variable header files
- A is an object that sends messages outward, so in a method (function) of a, the function b::sendmessage ()/b::P ostmessage () is called to send the message, because B accepts the message.
2. Message Receiver: Accept Message
The message Receiver object accepts a message that requires the following three parts:
- A prototype of the handler function for the message in the header file (*.h);
- There are macros in the implementation file (*.cpp) that accept the message map;
- There is a concrete implementation of the handler function for the message in the implementation file (*.cpp).
2.1The processing function prototype of the custom message is added to the header file
Before the Declare_message_map () statement, add the following form of the function prototype:
1 afx_msg LRESULT OnMyMessage (WPARAM WPARAM, LPARAM LPARAM);
For Win32, WParam, lparam is the most commonly used means of delivering messages.
2.2 Add a macro that accepts a message map in the implementation file
In the CPP file, after thebegin_message_map statement, add the following form of code:
1 On_message (Wm_mymessage, OnMyMessage)
This means that the wm_mymessage message corresponds to the OnMyMessage function. Note that the above is not terminated with a semicolon.
2.3 Implementation of the message handler function in the implementation file
1 LRESULT cbdlg::onmymessageWPARAMLPARAM LPARAM) 2 {3 // handling user-defined messages 4 ...... 5 return 0 ; 6 }
How to customize message passing between MFC dialog boxes