Windows message (2): Message classification and analog sending control notification messages

Source: Internet
Author: User

From: http://blog.sina.com.cn/s/blog_4b3c1f950100nten.html

1. Standard Messages (queue messages)

Except wm_command, all messages starting with WM _ are standard messages, such as wm_mousemove, wm_lbuttonup,

Wm_keydown and wm_char.

Classes derived from cwnd can receive such messages.

Every time windows removes a message from the system message queue, it determines which window it sends and which thread created the window, and puts it into the window.

Creates a thread message queue. The thread Message Queue receives messages sent to the window created by the thread. The thread extracts the message from the message queue and sends it

Process the window as needed. In addition to the keyboard and mouse messages, the queue messages include wm_paint, wm_timer, and wm_quit.

Note: For standard messages, you do not need to specify the handler name, which is the default correspondence.

For example:

Message Processing function corresponding to macro name

On_wm_char wm_char onchar
On_wm_close wm_close onclose
On_wm_create wm_create oncreate
On_wm_destroy wm_destroy ondestroy
On_wm_lbuttondo wm_lbuttondown onlbuttondown
On_wm_lbuttonup wm_lbuttonup onlbuttonup
On_wm_mousemove wm_mousemove onmousemove
On_wm_paint wm_paint onpaint

............................

2. Command Message

Messages from menus, keys, or toolbar buttons are command messages.

Such messages are displayed in the form of wm_command. In MFC, different command messages are distinguished by the menu item ID. In SDK

OfWparamParameter Identification. Classes derived from csf-target can receive such messages.WparamRecords the menu items from which the message comes from.

For example:

On_command (idm_about, onabout)
On_command (idm_filenew, onfilenew)
On_command (idm_fileopen, onfileopen)
On_command (idm_filesave, onfilesave)

...........

3. announcement message

Messages generated by the control, such as buttons and the selection of the list box, will generate a notification message to notify the event to its parent window (usually a dialog box ).

Raw.

Such messages are presented in the form of wm_command or wm_notify. Classes derived from csf-target (for example, cdocument can accept command messages and

Messages can be advertised, but standard messages (queue messages) cannot be received.

Note: As the cwnd class is derived from the csf-target class, all classes derived from cwnd can receive standard messages, command messages, and

Advertise messages. Classes derived from the csf-target class can only receive command messages and announcement messages, but cannot accept standard messages.

For example:

Control macro Message Processing Function

Button on_bn_clicked (<ID>, <memberfxn>) memberfxn
ComboBox on_cbn_dblclk (<ID>, <memberfxn>) memberfxn
Edit on_en_setfocus (<ID>, <memberfxn>) memberfxn
ListBox on_lbn_dblclk (<ID>, <memberfxn>) memberfxn

........................................ ..

Differences between standard messages and non-standard messages:

Standard Message: Perform subsequent operations after control;

Non-standard message: Just a simple prompt.

MFC command message routing:

Afxwndproc (with the window function replaced)-> afxcallwndproc-> windowproc-> onwnddmsg-> (if it is a command message, oncommand is called; if it is

Call onnotify if the message is advertised)-> on1_msg

So is the advertised message wm_command or wm_policy?

Explanation 1: wm_notify is more powerful than wm_command and can store some additional information. wm_command is not supported by all controls.

Hold.

Explanation 2: wm_command messages, listview, toolbar, and tree messages are sent by Edit, button, or ListBox. If comctl32.lib is not connected during compilation

However. Common controls sends the wm_notify message because more information is required.

Send a message to the control in the dialog box:

You want to simulate sending a tcn_selchange message to the ctreectrl control.

You want to simulate sending a bn_clicked message to the cbutton control.

★From the classification of Windows messages, we know that both messages are advertised messages. Is it wm_command or wm_notify?

According to the above explanation, we use TCN_SELCHANGE--WM_NOTIFY, BN_CLICKED--WM_COMMAND.

Is that true? See msdn:

Tcn_selchange

Lpnmhdr = (lpnmhdr) lparam;

Notifies a tab control's parent window that the currently selected tab has changed. This message is sent in the form

A wm_policy message.

  • No return value.
Lpnmhdr
Address of an nmhdr structure. HwndfromMember is the handle to the tab control. IdfromMember is the Child Window identifier of the tab control. CodeMember is tcn_selchange.

From the above we can see that tcn_selchange is indeed presented in wm_notify, it contains a struct pointer in the formLparamMedium:

typedef struct tagNMHDR { HWND hwndFrom; UINT idFrom; UINT code; } NMHDR;

Contains information about a notification message.

Hwndfrom
Window handle to the control sending a message.
Idfrom
Identifier of the control sending a message.
Code
Notification code. This member can be a control-specific notification code or it can be one of the common notification codes.

Note: About nmhdr and on_notify (from: http://www.cnblogs.com/a-peng/archive/2007/11/18/963533.html)

The introduction of the nmhdr 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 the wm_notify structure:

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. On_notify message ing macro has the following syntax:

On_policy (wnotifycode, ID, memberfxn)

Where:

Wnotifycode: The notification message and notification code to process. For example, the lvn_keydown mentioned above;

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 );

Bn_clicked

The bn_clicked notification message is sent when the user clicks a button. The parent window of the button has es

This notification message throughWm_commandMessage.

BN_CLICKED 
idButton = (int) LOWORD(wParam); // identifier of button 
hwndButton = (HWND) lParam; // handle to button

From the above we can see that bn_clicked is indeed included inWm_command.

 

★How to dissolve a notification messageWm_commandAnd wm_notify?

Wm_notify

Idctrl = (INT) wparam;

Pnmh = (lpnmhdr) lparam;

Idctrl
Identifier of the common control sending the message.
Pnmh
Address of an nmhdr structure that contains the notification code and additional information.

Wm_command

Wyycode = hiword (wparam); // notification code

WID = loword (wparam); // item, control, or accelerator identifier

Hwndctl = (hwnd) lparam; // handle of control

Assembly parameters:

Lparam makelparam (

Word
Wlow,// Low-order word

Word Whigh// High-order word

);

Or

DWORD makelong (

Word
Wlow,// Low-order word of long value

Word Whigh// High-order word of long value);

★To send messages to a control, we can use the following two methods:

Long senddlgitemmessage (

Hwnd
Hdlg,// Handle of dialog box

Int Niddlgitem,// Identifier of control

Uint MSG,// Message to send

Wparam Wparam,// First Message Parameter

Lparam Lparam// Second Message Parameter

);

Or

Lresult sendmessage (

Hwnd
Hwnd,// Handle of destination window

Uint MSG,// Message to send

Wparam Wparam,// First Message Parameter

Lparam Lparam// Second Message Parameter

);

★Instance. Verification Successful:

// Send the tcn_selchange message in a simulated manner

Nmhdr;

Nmhdr. Code = tcn_selchange;
Nmhdr. hwndfrom = g_pmaindlg-> m_tabctrl.getsafehwnd ();
Nmhdr. idfrom = g_pmaindlg-> m_tabctrl.getdlgctrlid ();

: Senddlgitemmessage (g_pmaindlg-> m_hwnd, idc_tab1, wm_notify, makelong (tcn_selchange, 0), (lparam) (& nmhdr ));

 

// Send the bn_clicked message

: Sendmessage (g_pmaindlg-> messages, wm_command, makelparam (idc_rang_off, bn_clicked), (lparam) (: getdlgitem (g_pmaindlg-> messages, idc_rang_off )));

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.