Define a custom message number: const UINT WM_MYMESSAGE = WM_USER + n; // The custom message is generally larger than WM_USER, and then you can add a ing for the message.
Afx_msg LRESULT OnMyMessage (WPARAM wParam, LPARAM lParam );
ON_MESSAGE (WM_MYMESSAGE, OnMyMessage)
LRESULT cxx: OnMyMessage (WPARAM wParma, LPARAM lParam)
{
...
}
If the message does not require a return value or a parameter, you can use the macro ON_MESSAGE_VOID for ing.
Afx_msg void OnMyMessage ();
ON_MESSAGE_VOID (WM_MYMESSAGE, OnMyMessage)
Void cxx: OnMyMessage ()
{
...
}
Complex and comprehensive version:
Message ing and loop mechanisms are the basic methods for running Windows programs. There are many ready-made message handles in VC ++ MFC. When we need to complete other tasks and customize messages, we have encountered some difficulties. In MFC ClassWizard, user-defined messages cannot be added. Therefore, you must add the corresponding code to the program to process custom messages like other messages. The common practice is to take the following steps:
Step 1: Define a message.
We recommend that you customize the message at least WM_USER + 100, because many new controls also use WM_USER messages.
# Define WM_MY_MESSAGE (wm_user+ 100)
Step 2: Implement Message processing functions.This function uses WPRAM and LPARAM parameters and returns LPESULT.
LPESULT CMainFrame: OnMyMessage (WPARAM wParam, LPARAM lParam)
{
// TODO: Process custom messages
...
Return 0;
}
Step 3: Describe the message processing function in the AFX_MSG block of the Class header file:
Class CMainFrame: public CMDIFrameWnd
{
...
// General message ing Function
Protected:
// {AFX_MSG (CMainFrame)
Afx_msg int OnCreate (maid );
Afx_msg void OnTimer (UINT nIDEvent );
Afx_msg LRESULT OnMyMessage (WPARAM wParam, LPARAM lParam );
//} AFX_MSG
DECLARE_MESSAGE_MAP ()
}
Step 4: Use the ON_MESSAGE macro command in the message block of the user class to map the message to the message processing function.
BEGIN_MESSAGE_MAP (CMainFrame, CMDIFrameWnd)
// {AFX_MSG_MAP (CMainFrame)
ON_WM_CREATE ()
ON_WM_TIMER ()
ON_MESSAGE (WM_MY_MESSAGE, OnMyMessage)
//} AFX_MSG_MAP
END_MESSAGE_MAP ()
If you need a unique message that defines the entire system, you can call the SDK function RegisterWindowMessage to define the message:
Static UINT WM_MY_MESSAGE = RegisterWindowMessage (User );
Use the ON_REGISTERED_MESSAGE macro command to replace the ON_MESSAGE macro command. The other steps are the same as above.
When a custom message is required, you can call the PostMessage or SendMessage function in the function of the corresponding class to send the message PoseMessage (WM_MY_MESSAGE, O, O ); to send messages to other processes, you can send messages as follows:
DWORD result;
SendMessageTimeout (wnd-> m_hWnd, // target window
WM_MY_MESSAGE, // message
0, // WPARAM
0, // LPARAM
SMTO_ABORTIFHUNG |
SMTO_NORMAL,
TIMEOUT_INTERVAL,
& Result );
To prevent other processes from being blocked and causing system crashes.
However, if you want to send messages to other classes (such as the main framework, sub-window, View class, dialog box, status bar, toolbar, or other controls), the above method is powerless, in the programming process, we often need to obtain a recognition signal from other classes. The MFC framework imposes various restrictions on us, but we can send messages to this class by getting the pointer of a class, the various actions of custom messages are defined in this class, so that you can freely send messages to other classes.
The following example describes how to send messages to the View class and framework class:
Send messages to the video class in the main framework class:
Define messages in the class:
ON_REGISTERED_MESSAGE (WM_MY_MESSAGE, OnMyMessage) // defines message ing.
Define message processing functions based on class:
// Message Processing Function
LRESULT CMessageView: OnMyMessage (WPARAM wParam, LPARAM lParam)
{
// TODO: Process custom messages
...
Return 0;
}
// Test function for sending messages
Void CMainFrame: OnTest ()
{
CView * active = GetActiveView (); // get the current video class pointer
If (active! = NULL)
Active-> PostMessage (WM_MY_MESSAGE, 0, 0 );
}
Send messages to the video class in other classes:
// Test function for sending messages
Void CMainFrame: OnTest ()
{
CMDIFrameWnd * pFrame;
CMDIChildWnd * pChild;
CView * pView;
// Obtain the main window pointer
PFrame = (CMDIFrameWnd *) AfxGetApp ()-> m_pMainWnd;
// Obtain the sub-window pointer
PChild = (CMDIChildWnd *) pFrame-> GetActiveFrame ();
// Get the class pointer
PView = pChild-> GetActiveView ();
If (pView! = NULL)
PView-> PostMessage (WM_MY_MESSAGE,); // send a message
}
The remaining steps are the same as above.
Send messages to the main framework in the video class:
First, define the relevant message in the main framework. The method is the same as above. Then add the following code to the function for sending the message:
// Test function for sending messages
Void CMessageView: OnTest ()
{
CFrameWnd * active = GetActiveFrame (); // obtain the frame pointer of the current main window
If (active! = This)
Active-> PostMessage (WM_MY_MESSAGE, 0, 0 );
Return 0;
}
In other classes, messages can be sent to different classes in sequence and so on, so that our program can send messages to other classes and processes without restrictions, avoiding unexpected risks.
In the following example, a multi-document program sends a message to the video class in a dialog box, detailing the process of sending a custom message.
Steps:
Step 1: Create a project Message in VC ++. All the ClassWizard step options are default and complete.
Step 2: add the test menu as the call-up dialog box in the main menu and create the OnTest () function in the framework class ()
Step 3: In the create dialog box of the resource, use ClassWizard to add the new class TestDialog and add the test button,
Create the OnDialogTest () function in the dialog box class ()
// Function for sending messages through the button in the dialog box
Void TestDialog: OnDialogTest ()
{
CMDIFrameWnd * pFrame;
CMDIChildWnd * pChild;
CView * pView;
// Obtain the main window pointer
PFrame = (CMDIFrameWnd *) AfxGetApp ()-> m_pMainWnd;
// Obtain the sub-window pointer
PChild = (CMDIChildWnd *) pFrame-> GetActiveFrame ();
// Get the class pointer
PView = pChild-> GetActiveView ();
If (active! = NULL)
Active-> PostMessage (WM_MY_MESSAGE,); // send a message
}
Add the following statement to the Message. h header file:
Static UINT WM_MY_MESSAGE = RegisterWindowMessage (Message );
Step 4: Add a custom message to the video class:
Add message ing in the header file MessageView. h
Protected:
// {AFX_MSG (CMessageView)
//} AFX_MSG
Afx_msg LRESULT OnMyMessage (WPARAM wParam, LPARAM lParam); // Add code for this behavior
DECLARE_MESSAGE_MAP ()
Add custom message ing to message ing in the MessageView. cpp file.
BEGIN_MESSAGE_MAP (CMessageView, CView)
// {AFX_MSG_MAP (CMessageView)
//} AFX_MSG_MAP
// Standard printing commands
ON_REGISTERED_MESSAGE (WM_MY_MESSAGE, OnMyMessage) // Add code to define a unique message in this row
END_MESSAGE_MAP ()
Add corresponding 0 message processing functions
LRESULT CMessageView: OnMyMessage (WPARAM wParam, LPARAM lParam)
{
CRect rect;
GetClientRect (& rect );
InvalidateRect (& rect );
Test =! Test;
Return 0;
}
Add the public: BOOL test Boolean variable to MessageView. h;
Initialize the test variable in the class constructor: test = FALSE;
Modify CMessageView: OnDraw () function
Void CMessageView: OnDraw (CDC * pDC)
{
CMessageDoc * pDoc = GetDocument ();
ASSERT_VALID (pDoc );
// The following program displays the message Response Results
If (test)
PDC-> TextOut (0, 0, message response !);
}
Step 5: display the test dialog box
Include the dialog box header file in the MainFrame class:
# Include TestDialog. h;
Add code to the OnTest () function
Void CMainFrame: OnTest ()
{
TestDialog dialog;
Dialog. DoModal ();
}
Run the program, open the dialog box in the test menu, and click the test button to view the result.