Comprehensive Explanation of wm_notify

Source: Internet
Author: User

 

Abstract: There are many types of control notification messages, but one of them is very common, but not very easy to grasp, that is, wm_notify. I try to give a more comprehensive discussion about this, if something is wrong, I also hope that all kinds of prawns will criticize and correct them.

Control notification message
In deep resolution messages in VC (I), we mentioned three types of messages: Window messages, command messages, and control notification messages, here we will talk about the last one: Control notification message.
A control notification message refers to a message in which a child control in a window has something to notify the parent window. The notification message is only applicable to standard window controls such as buttons, list boxes, Combo boxes, and edit boxes, as well as windows public controls such as tree views and list views. For example, clicking or double-clicking a control, selecting some text in the control, and the scroll bar of the operation control will generate a notification message. It is similar to a command message. When the user interacts with the control window, the control notification message is sent from the control window to its main window. However, this message does not exist to process user commands, but to enable the main window to change controls, such as loading and displaying data. For example, if you press a button, the message sent to the parent window can also be considered as a control notification message. The message generated by clicking the mouse can be processed directly by the main window and then handed over to the control window for processing.
The control notification message is processed directly or indirectly by the cwnd class derived class.

Control notification format
The control notification goes through an evolutionary process, so the sendmessage () Variable message, wparam, and lparam have three formats.
Control 1 notification format
The notification format of the first control is only a subset of window messages. The feature format is as follows: wm_xxxx. It mainly comes from the following three message types:
(1) indicates that a control window is either created or destroyed or clicked by the mouse: wm_parentnotify;
(2) messages sent to the parent window are used to draw messages of the window, such as wm_ctlcolor, wm_drawitem, wm_measureitem, wm_deleteitem, wm_chartoitem, wm_vktoitem, wm_command, and wm_compareitem.
(3) A scroll control component is sent to notify the parent window of the rolling window messages: wm_vscroll and wm_hscroll
Second control notification format
The notification format of the second control is shared with the command message. Its feature format is as follows: wm_command.
In wm_command, lparam is used to distinguish between command messages and control notification messages. If lparam is null, This is a command message. Otherwise, the control handle must be placed in lparam, is a control notification message. For wparam, the Control ID is used for low position, and the message event is used for high position.
Third control notification format
This is what we want to talk about, and it is also the most flexible format. Its feature format is as follows: wm_notify.
In wm_notify, lparam puts a pointer called nmhdr structure. The Control ID is put in wparam.

The origin of nmhdr Structure
The nmhdr structure is worth mentioning. This structure includes any content about the control for making the notification, without the limitations of space and type. His origins are also very interesting.
In the original Windows 3.x, wm_notify does not exist at all. The control notifies them of the parent window, such as mouse clicking and Widget background rendering events, and sends a message to the parent window. A simple notification sends only one wm_command message, containing a notification code, a control ID in wparam, and a control handle in lpraram. In this way, both wparam and lparam are filled, and there is no extra space to transmit some other messages, such as the location and time when the mouse is pressed.
To overcome this difficulty, windows3.x proposed a relatively low-level solution, that is, to add some additional messages to some messages, the most obvious is the drawitemstruct used by the Control Self-painting. I don't know if you are familiar with this structure. However, if you are a veteran, you should be very clear about this structure, which contains nine contents, almost all the information you need to control is provided to you. Why is it relatively low? Because the content appended to different messages is different, the results are scattered and messy.
In Win32, Ms proposed a better solution: Introducing the nmhdr structure. The introduction of this structure is to unify messages and use it to transmit complex information. The layout of this structure is as follows:
Nmhdr
{
Hwnd hwndfrom; equivalent to lparam in the original wm_command Transmission Mode
Uint idfrom; equivalent to the wparam (low-order) of the original wm_command Transmission Method)
Uint code; equivalent to running y code (wparam "s high-order) of the original wm_command Transmission Method)
};
If this structure is applied to the wm_notify information structure, the result of wm_notify is changed:
A. No additional information. The structure becomes simple, that is, a nmhdr structure.
B. Additional information is available. Defines a large structure. Its first element is the nmhdr structure, which is followed by additional information.

Benefits of wm_notify Structure
In addition to the advantages we mentioned above, wn_policy has its own unique advantages:
In a large structure, the first member is nmhdr. In this way, we can use a pointer to nmhdr to transmit the structure address. According to the pointer feature, no matter whether the message has additional information, this pointer is applicable and can be easily forced conversion.

Analyze on_policy
The Class Wizard can create the on_notify message ing entry and provide a framework for processing functions to process messages of the wm_notify type. The on_notify message ing macro has the following syntax.
On_policy (wnotifycode, ID, memberfxn)
Wyycode: indicates the notification message and notification code to be processed. For example, lvn_keydown; ID: Control ID; memberfxn: The member function that processes the message.
This member function has the following prototype declaration:
   

Afx_msg void memberfxn (nmhdr * pnotifystruct, lresult * result );

For example, if you want the onkeydownlist1 function to process the lvn_keydown message of clistctrl (ID = idc_list1), you can use the Class Wizard to add the following message ing:
On_policy (lvn_keydown, idc_list1, onkeydownlist1)
In the preceding example, the Class Wizard provides the following functions:

Void cmessagereflectiondlg: onkeydownlist1 (nmhdr * pnmhdr, lresult * presult)
...{
Lv_keydown * plvkey = (lv_keydown *) pnmhdr;
* Presult = 0;
}

The Class Wizard provides a pointer of the appropriate type. You can access the notification structure either through pnmhdr or plvkey.

On_policy_range
Sometimes we may need to process the same wm_notify message for a group of controls. In this case, you need to use on_policy_range instead of on_policy. However, unfortunately, the classwizard of vc6 does not support this message, so we must add it manually. The method is the same as that of manually Added messages, but note that:
(1) When on_policy_range is used, you need to specify the ID range of the control. The message ing entry and function prototype are as follows:
On_policy_range (wnotifycode, ID, idlast, memberfxn)
Where: wyycode: Message notification Code. For example: lvn_keydown. ID: ID of the first control.
Idlast: ID of the last control. (The id value must be continuous) memberfxn: message processing function.
(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 well-worth-thinking structure. In addition, wm_notify is closely related to the reflect message. Next we will discuss the reflection message.

 

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.