MFC Custom Messages

Source: Internet
Author: User

Message mechanism is a typical operating mechanism of windows, there are many messages such as wm_btn** in MFC. But in some cases we need to customize some of the messages to do what we need,

MFC's wizards do not help us do this, we can do this by adding the appropriate code.

Add a custom message action as follows:
1. Create an MFC project, such as a dialog-based application, Test.
2. Add the value of the message to be processed in the resource, i.e. add the following code in the CTestDlg.h. (Because a lot of MFC's messages are inside the Wm_user, so here's a bigger message than Wm_user)

#define Wm_mymessage (WM_USER+100)

3. Declaring message handlers, adding code in CTestDlg.h

Class Ctestdlg:public CDialog
{
Protected
......
Generated message-map functions
......
afx_msg LRESULT onmymessage (WPARAM WPARAM, LPARAM LPARAM); Add Lyw
Declare_message_map ()

......

}

4. Add the message map processing, in the CTestDlg.cpp people find the following section to add code

Begin_message_map (Ctestdlg, CDialog)
......
On_message (Wm_mymessage, OnMyMessage)
End_message_map ()

5. Implement your own custom message processing

LRESULT ctestdlg::onmymessage (WPARAM WPARAM, LPARAM LPARAM)
{
MessageBox ("recv msg Success");
Add your own message handling
......
return 0;
}

6. If you want to send a customized message, use the code

SendMessage (wm_mymessage, 0, 0);

or PostMessage (wm_mymessage, 0, 0);

If you want to define a system-unique message for multiple applications to process, the difference is as follows

1. Replace the macro #define WM_MYMESSAGE (wm_user+100) in the above 2 steps with the following

static UINT Wm_mymessage = RegisterWindowMessage ("Mymessage");

2. The code in the above 4 steps is replaced by the following

Begin_message_map (Ctestdlg, CDialog)
......
On_registered_message (Wm_mymessage, OnMyMessage)
End_message_map ()

3. When testing messages, if you want to have multiple applications receive this message, use the

:: SendMessage (Hwnd_broadcast, wm_mymessage, 0, 0);

Reference article: http://blog.csdn.net/kenkao/archive/2009/03/05/3960837.aspx

http://blog.163.com/strive_only/blog/static/893801682010101911467765/

Http://www.cnblogs.com/smartvessel/archive/2011/07/18/2109472.html

Summary: Implementing messages is important for sending functions of messages, but XTP uses no more than displaying send messages, which seem to send themselves internally.

MFC Custom Messages

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.