Comprehensive interpretation of WM

zhaozj2021-02-16  56

Summary: There are many kinds of control notification messages, but there is a very common, but it is not easy to master, that is, WM_NOTIFY, I try to make a more comprehensive discussion, there is a place, I also hope that all roads Shrimp criticism finger.

Control Notification Messages In "Deep Resolution VC" Message (on) ", we mentioned three categories of the message: window messages, command messages, and control notification messages, we have to talk about the last type: control notification message . The control notification message refers to such a message. There are some things in one window that need to notify the parent window. The notification message is only available for standard window controls such as buttons, list boxes, combo boxs, edit boxes, and Windows public controls such as tree-like views, list views, etc. For example, click or double-click a control, select some text in the control, and the scroll bar of the operating control will generate a notification message. She is similar to the command message, when the user interacts with the control window, the control notification message will be sent from the control window to its main window. However, the existence of such messages is not to handle user commands, but to make the main window to change controls, such as loading, display data. For example, pressing a button, the message sent to the parent window can also be seen as a control notification message; click the message generated by the mouse to process directly by the main window, and then hand it over to the control window processing. The control notification message is mainly derived by the CWND class directly or indirectly by the window class.

The control notification format control notifies an evolutionary process, so the variables of sendMessage () Message, WPARAM, and LPARAM have three formats. The first control notification format first control notification format is only a subset of the window message. Its feature format is as follows: WM_XXXX. It mainly comes from the following three types of message: (1) Represents a control window either has been created or destroyed, or it has been click on the message: WM_ParentNotify; (2) Send to the parent window, used to draw a message of your own window , for example: WM_CTLCOLOR, WM_DRAWITEM, WM_MEASUREITEM, WM_DELETEITEM, WM_CHARTOITEM, WM_VKTOITEM, WM_COMMAND and WM_COMPAREITEM (3) regulation of rolling transmission member, notifies the parent window scroll window message: WM_VSCROLL WM_HSCROLL second control notification format and a second format control notification command Message sharing, its feature format is as follows: WM_COMMAND. In WM_COMMAND, LPARAM is used to distinguish whether a command message or a control notification message: If lparam is null, this is a command message, otherwise the LParam is inevitable is the handle of the control, is a control notification message. For WPARAM, it is a low position that is the control ID, and the high position is put on the corresponding message event. The third control notification format This really involves the content we have to talk, and he is also the most flexible format. Its feature format is as follows: WM_NOTIFY. In WM_NOTIFY, LPARAM is placed in a pointer called NMHDR structure. It is the ID of the control in WPARAM.

The NMHDR structure of the NMHDR structure is worth mentioning, which includes any content of the control to make the notification, not limited to space and type limits, his origin is also very interesting. In the initial Windows3.x, there is no WM_NOTIFY, and the control notifies them parent windows, such as mouse click, control background drawing events, by sending a message to the parent window. Simple notifications only send a WM_COMMAND message, containing a notification code and a control ID in WPARAM and a control handle in LPraram. In this way, WPARAM and LPARAM are filled with no additional space to deliver some other messages, such as the position and time of the mouse. In order to overcome this difficulty, Windows3.x proposes a relatively low-level solution, which is to add some additional messages to some messages. The most obvious is the DrawItemStruct for the control. I don't know if you are familiar with this structure, but if you are older, you should be very clear, this structure contains 9 content, almost you need to control information to provide you. Why do it say that it is relatively low? Because the content of different messages is different, the result is a plate of shackles, very confusing. In Win32, MS proposes a better solution: introducing NMHDR structures. The introduction of this structure is the unity of the message, and it can deliver complex information. The layout of this structure is as follows: nmHDR {hwnd hwndfrom; LPARAM uint idfrom equivalent to the original WM_COMMAND; is equivalent to WParam (low-order) uint code equivalent to the original WM_COMMAND; is equivalent to the original WM_COMMAND passing Notify Code (WPARAM " SHIGH-Order)}; For this structure, the WM_Notify information structure is applied. The result is that WM_NOTIFY turns: A, no additional information. The structure is very simple, it is an NMHDR structure. B, there is additional information. Define a big The structure, its first element is the NMHDR structure, which places additional information. The benefits of the WM_Notify structure are in addition to what we said above, WN_NOTIFY has its own unique benefits: due to the big structure, the first Members are NMHDR, so we can use pointers to NMHDR to deliver structural addresses, depending on the characteristics of the pointer, regardless of the additional information, this pointer is applicable, and it can be easily enforced.

Analysis The ON_NOTIFY class wizard can create an ON_NOTIFY message mapping entry and provides a framework for handling a function to process the WM_Notify type message. The ON_NOTIFY message mapping macro has the following syntax. ON_NOTIFY (WNOTIFYCODE, ID, MEMBERFXN) where: WNOTIFYCODE: The notification message notification code to be processed. For example, the LVN_KEYDOWN we mentioned above; ID: control identifier ID; MEMBERFXN: Handling the member function of this message. This member function has the following prototype declaration: AFX_MSG Void Memberfxn (NMHDR * PNOTIFYSTRUCT, LRESULT * Result) mapping message: ON_NOTIFY (LVN_KEYDOWN, IDC_LIST1, OnKeydownList1) in the above example, the wizard provides the following functions class: void CMessageReflectionDlg :: OnKeydownList1 (NMHDR * pNMHDR, LRESULT * pResult) {LV_KEYDOWN * pLVKey = (LV_KEYDOWN *) pNMHDR; * pResult = 0;} At this time, the class wizard provides an appropriate type of pointer, you can access this notification structure through PNMHDR or through PLVKey. ON_NOTIFY_RANGE Sometimes we may need to process the same WM_NOTIFY message for a set of controls. This will need to use ON_NOTIFY_RANGE instead of ON_NOTIFY. However, very unfortunately, VC6's classwizard does not support this message, so we must be added manually. Methods and general manual adding messages, but you need to pay attention to: (1) When you use ON_NOTIFY_RANGE, you need to specify the ID range of the control. The message mapping entry and function prototype are as follows: ON_NOTIFY_RANGE (wnotifycode, id, idlast, Memberfxn) where: WNOTIFYCODE: Message Notification Code. For example: lvn_keydown. ID: Id identification ID of the first control. IDLAST: The identifier ID of the last control. (Logo value must be continuous) MEMBERFXN: Message handler. (2) The member function must have the following prototype declaration: AFX_MSG Void MemberfxN (UINT ID, NMHDR * PNOTIFYSTRUCT, LRESULT * RESULT);

Conclusion: WM_Notify is a very worthwhile structure, and he is very close to the Reflect Message, and we will discuss the reflection message next.

转载请注明原文地址:https://www.9cbs.com/read-23883.html

New Post(0)