In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "MFC simulation to achieve custom message sending method", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "MFC simulation to achieve custom message sending method" bar!
Under the MFC framework, there are many messages that have been defined by the system, such as ON_WM_LBUTTONDOWN (), ON_WM_MBUTTONDOWN (), and so on. When we use it, we just need to declare and call it, the simplest usage.
The use of a little more difficulty is to set up custom messages, and a little more difficulty is how to simulate system messages. In general, it is not necessary to do ordinary project development functions.
Custom messa
Sometimes the system-defined message can not fully meet the application of the program, so it is necessary to customize the message, so how to implement the custom message?
Step 1: define the custom message name and ID
Under the MFC framework, the messages assigned with WM_USER are all message ID that developers can customize.
Suppose, define a selected trigger message:
# define WM_USER_CLICKED WM_USER+1
This way of writing represents the definition of a custom message, the message name is: WM_USER_CLICKED, and the message ID number is: WM_USER+1
Here, a warm reminder: in order to facilitate the management of custom messages in large-scale drawing development programs, WM_USER is generally not directly used for numbering self-increment, but custom macros are used instead of WM_USER. For easy management, the format is as follows:
# define UIXML_WM_BASE WM_USER + 20000#define WM_USER_CLICKED1 UIXML_WM_BASE+ 0#define WM_USER_CLICKED2 UIXML_WM_BASE+1#define WM_USER_CLICKED3 UIXML_WM_BASE+2#define WM_USER_CLICKED4 UIXML_WM_BASE+3
If there are only one or two custom messages in your program, there is no need to write them in the above format.
Benefits of this format:
Custom message areas can be divided according to their functions. Suppose you are currently developing a basic framework class for internal use only. When customizing messages, you need to distinguish which are used by your own development and which are invoked by external developers. With a clear division of labor, it is not easy to get confused.
Step 2: send custom messages
Where you want to trigger a custom message, you need to send the message.
In the MFC framework, there are two mechanisms for sending messages: SendMessage and PostMessage.
:: SendMessage (GetParent ()-> GetSafeHwnd (), WM_USER_CLICKED, parameter 2, parameter 1)
It's just an example to show that it is determined according to the actual function.
Step 3: customize the message mapping ON_MESSAGE (WM_USER_CLICKED1, OnCustomClicked1)
Parameter 1: defined custom message
Parameter 2: mapping message of the response in the actual window
Step 4: customize the message implementation afx_msg LRESULT OnCustomClicked1 (WPARAM wParam, LPARAM lParam)
Specific operational applications can be written in OnCustomClicked1, which should be applied according to the actual requirements. We will not explain too much here. At this point, we should all know how to apply it.
Analog system message
The simple custom message method has been described in the custom message, so let's take a deeper improvement. How to simulate the system message, for an easy-to-understand explanation, here we simulate the ComboBox control CBN_SELCHANGE message.
People who have used the control development under the MFC framework all know that the message is sent by the system, usually we just apply it, if we let ourselves write such a trigger message, how to achieve it?
Prerequisite: inherit the CWnd class to simulate the ComboBox control, at this time, all the control messages need to be imitated internally, so how do you want the external developer to call it consistent with the messages in the system's ComboBox?
Suppose we use SendMessage to send messages. At this time, we also need to send messages, but we need to send command messages: WM_COMMAND
When is WM_COMMAND triggered?
1: when the user selects a command item from the menu
2: when a control sends a message to its parent window
3: when acceleration keystrokes are translated
In fact, WM_NOTIFY is sent above WINDOW9X, but in order to make it compatible with the base class, WM_COMMAND messages are still used.
Once WM_COMMAND is specified for message delivery, what data should wParam and lParam send?
WParam parameter
In sending custom messages, the wParam parameter is divided into high bytes and low bytes, which needs to be sent using MAKEWPARAM macros.
The macro creates a 32-bit unsigned value, which is used as a wParam parameter in the message, and connects two specified 16-bit values.
When passing parameters, the high bit is the ID number sent by the current simulation, and each window or even each control has its own number, which is not difficult to obtain.
The low order is the system message that needs to be triggered. As mentioned earlier, the simulated CBN_SELCHANGE message is used, so the wParam parameter is written as follows:
MAKEWPARAM (GetDlgCtrlID (), CBN_SELCHANGE) LPARAM parameter
For this parameter, consider it simple for you, just pass the HWND of the current window and convert it to the LPARAM parameter.
The overall transmission format of the simulation system:
:: SendMessage (pwnd- > GetSafeHwnd (), WM_COMMAND, MAKEWPARAM (GetDlgCtrlID (), CBN_SELCHANGE), (LPARAM) GetSafeHwnd ()
For the simulation system to send messages, the most important thing is the second parameter and the third parameter of SendMessage. By the same token, you can also send it using PostMessage.
After the message is sent, how is it used in the outside world?
Since we are simulating the system message, the call in the external window is consistent with the control message method that comes with MFC
Message mapping:
ON_CBN_SELCHANGE (ID_CUSTOMCOMBOBOXID, & CTestComboboxDlg::OnCbnSelchangeComboSd) Thank you for your reading, the above is the content of "MFC simulation to achieve custom message sending method". After the study of this article, I believe you have a deeper understanding of the problem of MFC simulation to achieve custom message sending method, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.