WM_COMMAND與WM_NOTIFY

加速鍵是產生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結構爲第一項的一個更大的結構體。這樣就可以包含了很多的子控件想給父窗體提供的信息了,甚至可以自己去定義這種的結構體。
    這就是這幾種消息的差別點了。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章