"Go" MFC message map in detail (collation reprint)

Source: Internet
Author: User
Tags message queue

message : Mainly refers to the information that is sent to the application by the user's actions, and also includes messages generated inside the operating system. For example, by clicking the left mouse button, Windows will produce a WM_LBUTTONDOWN message, and releasing the left mouse button will produce a WM_LBUTTONUP message, and a WM_CHAR message will be generated by pressing the letter key on the keyboard.

Message processing : In MFC, each specialized handler functions individually for each message. Message handlers are usually member functions of a class, and writing a message handler is the main task of writing a framework application. You can use ClassWizard to create a message function and then jump directly from ClassWizard to the source file message handler function to write the processing code.

message map : All framework classes that can receive messages and commands have their own message mappings. The framework uses message maps to link messages, commands, and their processing functions. Any class derived from the CCmdTarget class can have a message map. Although called a message map, a message map can handle both messages and commands.

Unlike traditional DOS, Windows uses a message-based time-driven form. When the application starts executing, Windows creates a message queue for the program that holds the various unused Windows messages that the program may create, with the message structure:

typedef struct TAGMSG {//msg

HWND hwnd;//The window handle used to identify the window that accepts the message

UINT message;//Message Identification number

WPARAM wparam;//Message Parameters

LPARAM lparam;//Message Parameters

DWORD time;//time to mail messages

Point pt;//coordinates in screen coordinates

} MSG;

Windows provides functions such as GetMessage, PostMessage, seekmessge to get messages, send messages, and find messages to handle different messages to achieve the program's goals. A message is cumbersome to handle, and in order to improve efficiency, MFC encapsulates these messages and functions, which is the concept of message map and message map macros.

message map Table :

For example, when the user presses the left mouse button, Windows issues a WM_LBUTTONDOWN message. MFC provides a message-mapping mechanism that links the WM_LBUTTONDOWN message issued by the user's left-click with a function. The specific code is:

afx_msg void OnLButtonDown (UINT nflags,cpoint point); The location of the statement is within the header file Message declaration macro (DECLARE_MESSAGE_MAP)

On_wm_lbuttondown (); The position of this statement is within the message macro that implements the file (between the BEGIN_MESSAGE_MAP macro and the End_message_map)

Through the above two lines of code has established a message mapping, the user only need to implement the function of the code to write the following function can be:

void Cdlg::onluttondown (UINT nflags,cpoint point) {

Add code

Cdlg::onluttondown (Nflags,point);

}

Each message in MFC corresponds to a function, so that a one-to-two relationship is listed as a table, if the developer needs to process a message, just write the code in the corresponding handler for the message.

The message map table is mainly implemented by three macros in MFC. These three macros are:

Declare_message_map the Declaration macro of the message,

BEGIN_MESSAGE_MAP macros,

END_MESSAGE_MAP macros.

message map Macros :

Message map macros mainly have command message map macros, control form message map macros, normal window message map macros, and user-defined message map macros.

The message-map macros have command words that are used to process the message. The command message is generated primarily by clicking the menu and clicking the button. command messages in the CCmdTarget class are not handler functions, that is, they are not in the message map table. menu items and buttons There are other controls that have their ID values, and the command message map macro is primarily a map of the IDs and handlers for menus, buttons, and other controls. The addition of the command message is also done through MFC's ClassWizard.

Several of the mapping macros are with the reflect typeface, which are the mapping macros used to process the reflected message. A reflection message is a child control that sends a message to the parent control, and the parent control does not process the message, but instead returns the message to the child control, allowing the child control to process the message, which forms a one-time reflection process. The reflection message is mainly in the Claddwizard thought of the Messagemap tab, in the Mesage bar some front with "=" message.

Windows message Composition structure

Windows messages consist of message numbers and parameters:

Message number: The Windows operating system identifies a Windows message, called a message number, by a 32 integer. The Windows message number has three features:

(1) Uniqueness: Uniqueness means that there is one and only one message number corresponding to each message in the Windows operating system. Because the Windows operating system activates the process through messages, the identity of the message must be unique.

(2) Intuitive: message number in the form of the message name, the message name to live visually illustrates the way the message is produced and the type, such as the message name Wm_lbuttondown visually represents the message that the click produces.

(3) Customizable: The Windows system defines a number of message numbers and message names that programmers can use to work with messages from defined windows. The form is as follows: #define Message name message number

Note that in order to not conflict with existing Windows system messages, the definition of the message number is usually as follows:

Wm_user+n Wm_user is the user message start value defined by the Windows operating system, and the programmer must pick the message number from that starting value, such as the custom message named "Message_1" and "message_2", which can be in the following form:

#define Message_1 wm_user+1

#DEFINE message_2 wm_user+2

Parameters: Windows messages have the following two parameters: Word parameter (wParam); long parameter (LParam). Both the word parameter and the long parameter are 32-bit integers that provide the accompanying message for the message, which is the carrier of the parameter during the message passing. The message number of the additional information depends on the message number.

Type of message

There are three main types of messages, namely Windows messages, command messages, and control notifications. Message generation can be divided into four categories:

* (1) messages generated by external input events; * (2) messages sent to the application by the Windows system

* (3) messages sent between applications; * (4) Other

Depending on the object that generated the message, you can further divide the message into the following categories:

* (1) window management messages include messages that activate basic window operations, such as close, maximize, and so on.

* (2) Initializes messages for message applications, Windows, controls, and other objects.

* (3) input message includes input message of keyboard, mouse, plotter and other input tools.

* (4) System messages inside Windows system messages.

* (5) message generated when Clipboard message operations Clipboard.

* (6) System Information messages are used to send and process messages for system information.

* (7) The message generated when the control handles the message manipulation control.

* (8) The control notification message system uses an empty notification message to send a message to the control.

* (9) message generated when scroll bar message operation scroll bar

* (10) messages generated by non-user area messages

* (one) MDI messages for multi-document operation messages

* (a) DDE messages are used for dynamic data exchange messages.

* (13) application-customized messages and so on.

Windows messages: All messages beginning with wm_ except WM_COMMAND are Windows messages. Windows messages are processed by Windows and views. Such messages often contain parameters that determine how messages are processed.

Control messages: Control notifications contain WM_COMMAND notification messages that are passed from controls and other child windows to the parent window. For example, when the user changes the text in the edit control, the edit control is sent to the parent window (such as a dialog box) with a WM_COMMAND message that contains the En_exchange control notification code. The message handler function of the window responds to the notification message in the appropriate way, such as getting the text in the edit box, and so on. Like other standard Windows messages, control notification messages are handled by Windows and views. However, the BN_CLICKED control notification message that is emitted when the user clicks the control button is handled as a command message.

Command message: The command message includes a WM_COMMAND notification message from the user interface object. menu items, toolbar buttons, and accelerator keys are user-interface objects that can produce commands. Each of these objects has an ID. User interface objects and commands can be linked by assigning objects and commands to the same ID. The command is handled as a special message.

Typically, the command ID is named with the functionality of the user-interface object it represents. For example, the Copy command in the EDIT menu can be represented by id_edit_copy. The MFC Class Library pre-defines some command IDs (such as Id_edit_paste and Id_file_open, and so on). Other command IDs are programmed to be self-defined, with a list of all the predefined command IDs, see Afxres. H file.

The processing of command messages differs from other messages. command messages can be processed by a wider range of objects, such as documents, document templates, application objects, Windows, and views. Windows sends commands to multiple candidates, called command targets. Typically, one of the objects has a handler function for the command. The processing function handles commands in the same way as Windows messages, but with different invocation mechanisms.

Common messages

1, Wm_lbuttondown

Generate: Click.

Parameter: WParam: Contains an integer value to identify the pressed state of the mouse button. Mouse down the logo and description:

Mk_lbutton identification Press the left mouse button

Mk_mbutton logo Press the middle mouse button

Mk_rbutton the right mouse button

LParam: The low character of the long parameter LParam contains the x-coordinate of the current coordinate, and the y-coordinate of the current coordinate in the high byte.

Similar messages are: Wm_lbuttonup, Wm_rbuttondown, Wm_rbuttonup, WM_LBUTTONDBLCLK, WM_RBUTTONDBLCLK

2, Wm_keydown

Generation: This event occurs when a non-system key is pressed, and the system key is a combination of keys that implement the system operation, such as a combination of ALT and a function key to implement menu operations.

Parameter: WParam: Press the Key's virtual keyboard code, the virtual key code is used to identify which key is pressed or released, such as the function key F1 virtual key code in Windows.h file defined as vk_f1.

LParam: Long parameters record the number of repetitions of the key, scan code, transfer code, the status of the previous key and other information.

Similar messages have Wm_keyup: Events that occur when you release a non-system key that is pressed.

3, WM_CHAR

Generated: generated when a non-system key is pressed.

Parameter: WParam: The ASCII code of the key being pressed. LParam: Long parameters record the number of repetitions of the key, scan code, transfer code, the status of the previous key and other information.

4, Wm_create

Generation: Generated by the CreateWindow function, creating a window.

Parameter: WParam: no use. LParam: Contains a pointer to the CREATESTRUCT data structure that is a copy of the arguments passed to the CreateWindow function.

5, Wm_close

Generated: Generated when the current window is closed.

Parameter: WParam: Not used. LParam: Not used.

6, Wm_destroy

Generated: emitted by the DestroyWindow function when the window is to be destroyed.

Parameter: WParam: no use. LParam: It's useless.

7, Wm_quit

Generated: issued by the PostQuitMessage function when exiting an application.

Parameter: WParam: Contains an exit code that identifies information about the application exiting the runtime. LParam: Not used.

8, Wm_command

Generated: When a user interacts with a window or control, the control sends a WM_COMMAND message to its window. The low byte of the message's wparam parameter contains the ID of the child window (control) that contains the notification code of the child window to the parent window, and the lparam parameter contains the Control child window handle.

9, WM_PAINT

The user changes the window size or drags the window, the system sends the WM_PAINT message to the application, and WM_PAINT the message calls the BeginPaint function.

Message-handling functions

1. Processing functions for Windows message and control notifications

Windows message and control notifications are handled by the window class object that is derived from CWnd. They include CFrameWnd, CMDIFrameWnd, CMDIChildWnd, CView, CDialog, and classes that are customized from these derived users. Such a class object encapsulates the Windows window handle HWND.

Windows message and control notifications have default handler functions, which are predefined in the CWnd class, and the MFC class library forms the names of these handlers based on the message name, which begins with the prefix "on." Some processing functions have no parameters, some have several parameters, and some have return value types except void. The description of the message handler function in CWnd has a afx_msg prefix. The keyword afx_msg is used to differentiate processing functions from other CWnd member functions. For example, the handler function for message WM_PAINT is declared in CWnd as: afx_msg void OnPaint ();

Windows messages are common with mouse messages (such as WM_LBUTTONDOWN) messages, keyboard character messages (WM_CHAR messages), keyboard key messages (WM_KEYDOWN), window redraw messages WM_PAINT, horizontal and vertical bar scrolling messages Wm_ HScroll and Wm_vscroll), as well as system clock message wm_timer.

2. Processing function of command message

Because the user interface objects are user-defined, each application's user interface objects vary widely, so there is no default handler for command messages for user-interface objects. If a command directly affects an object, you should let the object handle the command. For example, the Open command on the File menu is of course relevant to the application: The application opens a specific document to respond to the command. Therefore, the handler function of the Open command is a member function of the application class.

When you map a command message to a processing function, ClassWizard names the handler with the command ID, which you can accept, modify, or replace with the recommended name. For example, the CUT command for an EDIT menu item, the corresponding ID is id_edit_cut, and the handler function is named: afx_msg void Oneditcut ();

In addition, for the BN_CLICKED notification message for a control button, its handler function can be named:

afx_msg void Onclickeduseasdefault ();

The handler function for the command message does not have a parameter value, nor does it return a value.

Message map

When you create an application framework with the MFC application Wizard, AppWizard writes a message map for each command target class that you create, including derived application objects, documents, views, and border windows, and so on. The message map for each command target class exists in the corresponding. cpp file. You can use ClassWizard to add some entries to the messages and commands that each class will process, based on the basic message map created by AppWizard. For example, for an application class, the basic message map created by MFC AppWizard is:

Begin_message_map (Ctexteditapp,cwinapp)

{{Afx_msg_map (Ctexteditapp)

On_command (Id_app_about,onappabout)

Note: ClassWizard will add and remove message map macros here

Do not edit these generated blocks of code

}}afx_msg_map

Document commands based on standard files (new and open)

On_command (id_file_new,cwinapp::onfilenew)

On_command (Id_file_open,cwinapp::onfileopen)

Standard Print Setup Commands

On_command (Id_file_print_setup,cwinapp::onfileprintsetup)

End_message_map ()

See: http://blog.csdn.net/seawaywjd/article/details/7628675

"Go" MFC message map in detail (collation reprint)

Related Article

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.