6 out of 11 rated this helpful - Rate this topic

WM_NOTIFY message

Applies to: desktop apps only

Sent by a common control to its parent window when an event has occurred or the control requires some information.

Parameters

wParam

The 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.

lParam

A 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.

Remarks

The destination of the message must be the HWND of the parent of the control. This value can be obtained by using GetParent, as shown in the following example, where m_controlHwnd is the HWND of the control itself.


NMHDR nmh;
nmh.code = CUSTOM_SELCHANGE;    // Message type defined by control.
nmh.idFrom = GetDlgCtrlID(m_controlHwnd);
nmh.hwndFrom = m_controlHwnd;
SendMessage(GetParent(m_controlHwnd), 
    WM_NOTIFY, 
    (WPARAM)m_controlHwnd, 
    (LPARAM)&nmh);

Applications handle the message in the window procedure of the parent window, as shown in the following example, which handles the notification message sent by the custom control in the previous example.


INT_PTR CALLBACK DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_NOTIFY:
        switch (((LPNMHDR)lParam)->code)
        {
        case CUSTOM_SELCHANGE:
            if (((LPNMHDR)lParam)->idFrom == IDC_CUSTOMLISTBOX1)
            {
                ...   // Respond to message.
                return TRUE;
            }
            break; 
        ... // More cases on WM_NOTIFY switch.
        break;
        }
    ...  // More cases on message switch.
    }
    return FALSE;
}

Some notifications, chiefly those that have been in the API for a long time, are sent as WM_COMMAND messages. For more information, see Control Messages.

If the message handler is in a dialog box procedure, you must use the SetWindowLong function with DWL_MSGRESULT to set a return value.

For Windows 2000 and later systems, the WM_NOTIFY message cannot be sent between processes.

Many notifications are available in both ANSI and Unicode formats. The window sending the WM_NOTIFY message uses the WM_NOTIFYFORMAT message to determine which format should be used. See WM_NOTIFYFORMAT for further discussion.

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Winuser.h

See also

WM_NOTIFYFORMAT

 

 

Send comments about this topic to Microsoft

Build date: 3/6/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
how to use WM_NOTIFY with c#

// Notify message header structure.

[

StructLayout(LayoutKind.Sequential)]

publicstructNMHDR

{

publicIntPtr hwndFrom;

publicint idFrom;

publicint code;

}



protected overridevoid WndProc( ref Message message )

{


const int WM_NOTIFY = 0x004E;

const int HDN_BEGINTRACKA = -6;


switch ( message.Msg )

{

case WM_NOTIFY:

NMHDR nmhdr = (NMHDR)message.GetLParam(typeof(NMHDR));


switch(nmhdr.code)

{

case HDN_BEGINTRACKA: // proces...

break;

}


break ; //nmhdr.code

}


break; //message.Msg

//-----------------

default:

base.WndProc(ref message);

break;

}

}

Restating the obvious about the WM_NOTIFY message
Raymond Chen wrote an excellent article about the WM_NOTIFY message, in which he states more clearly what is written about it in the documentation.
http://blogs.msdn.com/oldnewthing/archive/2009/08/21/9877791.aspx
Example is incorrect?

The documentation says that the WPARAM should be the control id, but the example passes the HWND instead.

If the API documentation is correct, the example should be:

NMHDR nmh;
nmh.code = CUSTOM_SELCHANGE; // Message type defined by control.
nmh.idFrom = GetDlgCtrlID(m_controlHwnd);
nmh.hwndFrom = m_controlHwnd;
SendMessage(GetParent(m_controlHwnd), 
  WM_NOTIFY, 
  (WPARAM)nmh.idFrom, 
  (LPARAM)&nmh);
Example is incorrect?
The documentation says that the WPARAM should be the control id, but the example passes the HWND instead.

If the API documentation is correct, the example should be:

NMHDR nmh;
nmh.code = CUSTOM_SELCHANGE; // Message type defined by control.
nmh.idFrom = GetDlgCtrlID(m_controlHwnd);
nmh.hwndFrom = m_controlHwnd;
SendMessage(GetParent(m_controlHwnd),
WM_NOTIFY,
(WPARAM)nmh.idFrom,
(LPARAM)&nmh);

Note on Order of Message Handling

"case: WM_NOTIFY" should be the first case in your "switch (message)" in your message loop.

This may be to ensure that lParam does not get defiled by a different case?

My program was randomly crashing until I moved the WM_NOTIFY case to the front of my message switch.

Value
WM_NOTIFY = &H4E