Freigeben über


Receiving Notification from Common Controls

Common controls are child windows that send notification messages to the parent window when events, such as input from the user, occur in the control.

The application relies on these notification messages to determine what action the user wants it to take. Most common controls send notification messages as WM_NOTIFY messages. Windows controls send most notification messages as WM_COMMAND messages. CWnd::OnNotify is the handler for the WM_NOTIFY message. As with CWnd::OnCommand, the implementation of OnNotify dispatches the notification message to OnCmdMsg for handling in message maps. The message-map entry for handling notifications is ON_NOTIFY. For more information, see Technical Note 61: ON_NOTIFY and WM_NOTIFY Messages.

Alternately, a derived class can handle its own notification messages using "message reflection." For more information, see Technical Note 62: Message Reflection for Windows Controls.

Retrieving the Cursor Position in a Notification Message

On occasion, it is useful to determine the current position of the cursor when certain notification messages are received by a common control. For example, it would be helpful to determine the current cursor location when a common control receives a NM_RCLICK notification message.

There is a simple way to accomplish this by calling CWnd::GetCurrentMessage. However, this method only retrieves the cursor position at the time the message was sent. Because the cursor may have been moved since the message was sent you must call CWnd::GetCursorPos to get the current cursor position.

Hinweis

CWnd::GetCurrentMessage should only be called within a message handler.

Add the following code to the body of the notification message handler (in this example, NM_RCLICK):

CPoint cursorPos;
cursorPos.x = GetCurrentMessage()->pt.x;
cursorPos.y = GetCurrentMessage()->pt.y;

At this point, the mouse cursor location is stored in the cursorPos object.

See Also

Reference

Making and Using Controls

Concepts

Controls (MFC)