WM_COMMAND与WM_NOTIFY

来源:互联网 发布:c语言输出正三角形 编辑:程序博客网 时间:2024/05/16 19:41

加速键是产生WM_COMMAND讯息(有些情况下是WM_SYSCOMMAND)的键组合。许多时候,程式使用加速键来重复常用功能表项的动作(然而,加速键还可以用于执行非功能表功能)。

 

例如,许多Windows程式都有一个包含「Delete」或「Clear」选项的「Edit」功能表,这些程式习惯上都将Del键指定为该选项的加速键。使用者可以通过「 Alt 键」从功能表中选择「 Delete 」选项,或者只需按下加速键 Del 。当视窗讯息处理程式收到一个WM_COMMAND讯息时,它不必确定使用的是功能表还是加速键。

  • WM_COMMAND

The WM_COMMAND message is sent when the user selects a command item from a menu, when a control sends a notification message to its parent window, or when an accelerator keystroke is translated.

Syntax

WM_COMMAND    WPARAM wParam    LPARAM lParam;    

Parameters

wParam
The high-order word specifies the notification code if the message is from a control. If the message is from an accelerator, this value is 1. If the message is from a menu, this value is zero.

The low-order word specifies the identifier of the menu item, control, or accelerator.

lParam
Handle to the control sending the message if the message is from a control. Otherwise, this parameter is NULL.

Return Value

If an application processes this message, it should return zero.

  • WM_NOTIFY

The WM_NOTIFY message is sent by a common control to its parent window when an event has occurred or the control requires some information.

Syntax

 

To send this message, call the SendMessage function as follows.
lResult = SendMessage(     // returns LRESULT in lResult    (HWND) hWndControl,     // handle to destination control    (UINT) WM_NOTIFY,     // message ID    (WPARAM) wParam,     // = (WPARAM) (int) idCtrl;   (LPARAM) lParam     // = (LPARAM) (LPNMHDR) pnmh;);  

Parameters

idCtrl
Identifier of the common control sending the message. This identifier is not guaranteed to be unique. An application should use the hwndFrom or idFrom member of the NMHDR structure (passed as the lParam parameter) to identify the control.
pnmh
Pointer to an NMHDR structure that contains the notification code and additional information. For some notification messages, this parameter points to a larger structure that has the NMHDR structure as its first member.

Return Value

The return value is ignored except for notification messages that specify otherwise.

 

  • 子窗体和父窗体:

子窗体被触发时,向父窗体发送一个WM_COMMAND消息,父窗体的窗口函数处理这个消息,进行相关的处理。lParam表示子窗口句柄,LOWORD(wParam)表示子窗口ID,HIWORD (wParam)表示通知码(例如单击,双击,SETFOCUS等)。


WM_MESSAGE、WM_COMMAND、WM_NOTIFY等消息有什么不同?   

WM_MESSAGE是最普通的WINDOWS消息,对于这种类型的消息没什么好说的。那WM_COMMAND和WM_NOTIFY消息都是WINDOWS CONTROL给它的父窗体发的消息,那这两种消息有什么不同呢?WM_COMMAND消息其实是早期的(WIN3.X时代)子窗体消息,子窗体给父窗体发送消息,父窗体就捕获WM_COMMAND来处理子窗体的消息。但是这个消息只包括了有限的信息,例如wParam包括了子窗口ID和通知码,lParam则包括了子窗口句柄,就这点信息了,如果想知道一些额外的信息的话(例如,鼠标点在了子控件的位置)就要借助于其他的WM_*消息。所以对于新型的WIN32控件,微软就增加了一个新的NOTIFICATION消息,这个消息的参数是这样的:wParam包含了控件ID,而lParam则包含了一个结构体的指针,这个结构体是NMHDR结构或者以NMHDR结构为第一项的一个更大的结构体。这样就可以包含了很多的子控件想给父窗体提供的信息了,甚至可以自己去定义这种的结构体。
    这就是这几种消息的差别点了。