Custom messages in VC

Source: Internet
Author: User
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:Implements 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:In the Class header fileAFX_MSGBlock to describe the Message Processing Function:

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:In the message block of the user class, useON_MESSAGEMacro commands map messages to message processing functions.

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, 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 custom messages to the video class:
Add message ing in the header file messageview. h
Protected:
// {Afx_msg (cmessageview)
//} Afx_msg
Afx_msg lresult onmymessage (wparam, 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 ();
}

Second:

/*
To create a custom message program, follow these steps:
1: Use classward to create a new project named messagetest
2: This project menu document cancels the Active X support and the printing support. Other Default documents are supported. Finish.
3: Add a "test" sub-menu to the menu and map its execution method to ontest.
4: create a message sender: Add a dialog box in the resource and use classward to create a class named
Testdialog. Then add a button in the dialog box and map it with classward to execute the function.
Onbutton1 ().
5: add a line in the testdialog. h file # define wm_my_message (wm_user + 100)
Used to define your own messages
6: add a line in the testdialog. cpp file: # include "mainfrm. H"
7. Add the message sending code to the button in the dialog box as follows:
Void testdialog: onbutton1 ()
{
// Todo: add your control notification handler code here
// Obtain the current framework pointer
Cmainframe * pmainframe = (cmainframe *) afxgetapp ()-> m_pmainwnd;
// Obtain the current view pointer
Cview * pview = pmainframe-> getactiveview ();
If (pview! = NULL)
{
Pview-> postmessage (wm_my_message, 0, 0 );
}
}
The above is the message sender's work has been completed
8: For message recipients
Define # define wm_my_message (wm_user + 100) in messagetestview. h)
9: The message ing function is defined in messagetestview. h as follows:
Protected:
// {Afx_msg (cmessagetestview)
Afx_msg void ontest ();
Afx_msg lresult onmymessage (wparam, lparam); // The custom message ing function is used here.
//} Afx_msg
Declare_message_map ()
10: In the messagetestview. cpp file, the message response function is declared as follows:
Begin_message_map (cmessagetestview, cview)
// {Afx_msg_map (cmessagetestview)
On_message (wm_my_message, onmymessage) // custom message Response Function
On_command (id_test, ontest)
//} AFX_MSG_MAP
END_MESSAGE_MAP ()
11: The message response function is implemented in the MessageTestView. cpp file as follows:
LRESULT CMessageTestView: OnMyMessage (WPARAM wParam, LPARAM lParam)
{
MessageBox ("OnMyMessage! Aggreger ");
Return 0;
}
12: Associate the message sender with the message responder. That is, associate the dialog box with the menu.
The OnTest method for implementing CMessageTestView is as follows:
Void CMessageTestView: OnTest ()
{
// TODO: Add your command handler code here
TestDialog dlg;
Dlg. DoModal ();
}

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.