What is message, message processing function, message mapping?
Message is simple to say that an operation is to be executed by entering the device to execute an instruction. The specific operation is your series of code. Message processing function. In the SDK, the message is actually very easy to understand. When the window is established, there will be a function (window handler) to start executing a message loop, we can clearly see the thread of the message processing. A Switch Case statement can get it, the message is loop until the WM_QUIT message will end, and the remaining messages are intercepted after calling the corresponding processing function. But in the MFC encapsulated the API, the news seems to be a bit complex. We can't see the familiar Switch Case statement, and it is a thing that is called a message map. Why is the MFC to introduce a message mapping mechanism, you can imagine, in the current program development activity, your program has multiple forms, the main window is only one, the menu, toolbar, controls These are sub-windows , Then how many Switch Case we need to write, and also assign a message handler for each message, how complicated this is. Therefore, MFC uses a new mechanism. With an array, map the window message and the corresponding message processing function, you can understand this is a table. This mechanism is the message mapping. This table is defined in the window base class CWnd, and the message mapping table of derived class is empty, that is, if you don't manually add message processing functions, the parent class will be executed when the derived window accepts a message. Message handler. This is obviously efficient.
Message structure provided by MFC
At the same time, MFC defines the following two main structures:
AFX_MSGMAP_ENTRY
Struct AFX_MSGMAP_ENTRY {
UINT NMESSAGE; / / WINDOWS message ID number
UINT NCODE; / / Notification of the message
UINT NID; // Windows Control Message ID
UINT NLASTID; / / The message is a message that is mapped by a specified range
Uint nsig; / / indicate the action identity of the message
AFX_PMSG PFN; / / Pointer to the message processing function
}
AFX_MSGMAP
Struct AFX_MSGMAP {
#ifdef _AFXDLL
Const AFX_MSGMAP * (Pascal * PfNgetBaseMap) ();
#ELSE
Const AFX_MSGMAP * PBASEMAP;
#ENDIF
Const AFX_MSGMAP_ENTRY * LPENTRIES;
}
/// AFX_MSGMAP can get the message map of the base class map the entry address and the message map of the itself.
The process of processing the next message in the MFC is usually the case.
1, _AFXCBTFILTERHOOK intercept message (this is a hook function)
2, _afxcbtfilterhook Set the window process to AfxWndProc.
3, function AFXWndProc Receives the message sent by the Windows operating system.
4. Function AfxWndProc call function AFXCallWndProc for message processing.
5. Function AFXCallWndProc calls the CWND class method WINDOWPROC for message processing.
How to add your own message?
We have already learned the WINDOW message mechanism, how to join our own news? Well to see
A standard message handler is this
The default handler for pre-defining standard Windows messages (WM_XXXX WM is the abbreviation of Window Message) in the CWND class. The class library is named after the message name. For example, the handler of the WM_Paint message is declared in CWND:
AFX_MSG void onpaint ();
The AFX_MSG keyword indicates the role of the C Virtual keyword by distinguishing these handlers to other CWND member functions. However, please note that these functions are actually not virtual, but through message mapping. We have explained this in the beginning of this article. All classes that can perform message processing are based on CCMDTARGET classes, that is, the CCMDTarget class is all parent classes that can be handled. The CCMDTARGET class is the basis and core of the MFC handling command message.
To rewrite the processing programs defined in the base class, just define a function with the same prototype in the derived class, and create the message mapping item for this handler. We can create most window messages or custom messages via ClassWizard. You can automatically establish message mapping through ClassWizard, and the framework of message processing function, we only need to fill out what we have to do, add what you have to do to handle functions . This is very simple, it will not say it. But perhaps we need to add some ClassWizard that does not support window messages or custom messages, then we need to create a framework for handset to create message mapping and message processing, usually the steps are as follows:
Step 1: Define the message. Microsoft recommends user-defined messages at least WM_USER 100 because many new controls also use WM_USER messages.
#define WM_MYMESSAGE (WM_USER 100)
Step 2: Implement the message processing function. This function uses WPRAM and LPARAM parameters and returns lpeSult.
LpeSult CMAINFRAME :: ONMYMESSAGE (WPARAM WPARAM, LPARAM LPARAM)
{
// Todo: Handle user custom message, fill in the blank is to fill here.
Return 0;
}
Step 3: Describe the message processing function in the AFX_MSG block of the class header:
// {{AFX_MSG (CMAINFRAME)
AFX_MSG LRESULT ONMYMESSAGE (WPARAM WPARAM, LPARAM LPARAM);
//}} AFX_MSG
Declare_message_map ()
Step 4: In the user class message block, use the ON_MESSAGE macro to map the message to the message processing function.
ON_MESSAGE (WM_MYMESSAGE, ONMYMESSAGE)
It can be seen that the user-defined message is the same as the message added to the ClassWizard, which uses the ON_MESSAGE macro to establish a message mapping.
In fact, the message category can be divided into a variety, and it is just one of them. There are three main message categories: (The following is taken from MSDN)
1, Windows Message
Such messages mainly include messages starting with WM_, except for WM_COMMAND. Windows messages are processed by windows and views. Such messages tend to have parameters for determining how to handle messages.
2, control notification
Such messages include WM_COMMAND notification messages sent from controls and other sub-windows to their parent windows. For example, when the user performs operations that may change the text in Edit Control, the Edit Control will send a WM_CHANGMAND message containing the En_Change Control Notification Code to its parent. The window handler of this message responds to this notification message in a certain manner, such as retrieving the text in the control.
The framework is transmitted to other WM_ messages, just like the control notification message. However, there is an exception, i.e., when the user clicks the button, the BN_Clicked control message is sent by the button. This message is specifically handled as a command message and transmits like other commands.
3, command message
Such messages include WM_COMMAND notification messages issued by the user interface object (menu, toolbar buttons, and shortcut). The frame processing command is different from the processing other messages, and more types of object processing commands can be used. Windows Messages and Control Notification Messages are processed by the window (the window is an object that is derived from the CWND class). Including CFrameWnd, CmdiframeWnd, CmdichildWnd, CView, CDIALOG, and your own class derived from these base classes. These objects encapsulate the handle of the HWnd - Windows window.
Command messages can be processed by a broader object (document, document template, and application object itself), not just windows and views. When a certain command directly affects a particular object, the object should be processed by this command. For example, the "Open" command in the File menu is logically associated with the application: the specified document is opened when the application receives this command. Therefore, the handler of the "Open" command is a member function of the application class.
Command news We are more common with menu items and toolbars. You can see that his message mapping macro and window messages are not the same. The general form is like this.
ON_COMMAND (ID, Memberfxn)
The first parameter is the command ID, one ID number corresponds to a message processing, of course, you can make multiple ids share a handler. Common applications, for example: menu items Open the ID and toolbar buttons of the document Open the ID of the document simultaneously using a process function, or directly set them directly into the same.
There is also a message called notification message. For example, some of the complex controls such as tree controls are available after clicking, such as a structure of the cursor, the location of the cursor, so the MFC is a macro for each notification message for the control, which is long. This looks like this:
ON_CONTROL (en_change, id, memberfxn)
There are still many messages exist in the MFC, the macro definition is different, you can touch bypass.
The window message has hundreds. You can check the WM_ from MSDN, or view the CWnd member function, you will list a lot, don't forget that there are still many non-window messages. Thunder is unable to list one by one, it is not necessary. Everyone check it. However, for some common, new control messages and special notice messages I still list several tables, everyone is a reference.
Table 1: Common message mapping macro
Table 2: Common and universal window messages
Table 3: Win32 new control shared notification message
Table 4: Special notification message
It is actually a beginning that there is still a lot of content about the news, you can experience it in practical applications.
---------------------------------------
[C · K] Welcome to http://blog.9cbs.net/welcome_cknew computer, new knowledge, new human being!