Windows is capable of handling a mouse with up to 3 buttons and a mouse wheel. Mouse messages are generated when the user moves the mouse, presses the mouse buttons or scrolls the mouse wheel, within the client area and non-client area. The non-client area is the area of a window consisting of borders, title bar, menu bar, minimize/maximize button and exit button.

Client Area Mouse Messages

Listed below are some of the more common Windows client area mouse messages together with their MFC macro names and associated message map macro handling function

The message map handler function will be of the following format

afx_msg void OnMsgName (UINT nFlags, CPoint point)

Where

point – contains the cursor location reported in device coordinates relative to the upper left corner of the window’s client area.
nFlags – contains additional information about the mouse state and Shift and Ctrl as detailed below.

MK_LBUTTON – The left mouse button is pressed.
MK_MBUTTON – The middle mouse button is pressed.
MK_RBUTTON – The right mouse button is pressed.
MK_CONTROL – The Ctrl key is pressed.
MK_SHIFT – The Shift key is pressed.

Nonclient-Area Mouse Messages

When the mouse is clicked inside or moved over a window’s nonclient area, a nonclient-area mouse message is generated. The table lists below details these nonclient-area mouse messages.

 
Message Message-Map Macro Handling Function
WM_NCLBUTTONDOWN ON_WM_NCLBUTTONDOWN OnNcLButtonDown
WM_NCLBUTTONUP ON_WM_NCLBUTTONUP OnNcLButtonUp
WM_NCLBUTTONDBLCLK ON_WM_NCLBUTTONDBLCLK OnNcLButtonDblClk
WM_NCMBUTTONDOWN ON_WM_NCMBUTTONDOWN OnNcMButtonDown
WM_NCMBUTTONUP ON_WM_NCMBUTTONUP OnNcMButtonUp
WM_NCMBUTTONDBLCLK ON_WM_NCMBUTTONDBLCLK OnNcMButtonDblClk
WM_NCRBUTTONDOWN ON_WM_NCRBUTTONDOWN OnNcRButtonDown
WM_NCRBUTTONUP ON_WM_NCRBUTTONUP OnNcRButtonUp
WM_NCRBUTTONDBLCLK ON_WM_NCRBUTTONDBLCLK OnNcRButtonDblClk
WM_NCMOUSEMOVE ON_WM_NCMOUSEMOVE OnNcMouseMove

The message map handler function will be of the following format

afx_msg void OnMsgName (UINT nHitTest, CPoint point)

where

nHitTest – contains a hit-test code that identifies where the nonclient area event occurred. A selection of these hit-test codes is shown in the list below.

Value Corresponding Location
HTCAPTION The title bar
HTCLOSE The close button
HTGROWBOX The restore button (same as HTSIZE)
HTHSCROLL The window’s horizontal scroll bar
HTMENU The menu bar
HTREDUCE The minimize button
HTSIZE The restore button (same as HTGROWBOX)
HTSYSMENU The system menu box
HTVSCROLL The window’s vertical scroll bar
HTZOOM The maximize button


Point
– specifies the location in the window at which the event occurred however for nonclient-area mouse messages, point.x and point.y contain screen coordinates as opposed to client coordinates. Screen coordinates can be converted to client coordinates with the CWnd member function ScreenToClient().

Miscellaneous Mouse Messages

WM_NCHITTEST

Before a window receives a client-area or nonclient-area mouse message, it receives a WM_NCHITTEST message accompanied by the cursor’s screen coordinates. Windows uses this message to determine whether to send a client-area or nonclient-area mouse message. For more detailed information about the parameters associated with the message use the following

https://docs.microsoft.com/en-us/windows/win32/inputdev/wm-nchittest

The Mouse Wheel

The WM_MOUSEWHEEL message is sent when the mouse wheel is rotated.

MFC’s ON_WM_MOUSEWHEEL macro maps WM_MOUSEWHEEL messages to the message handler OnMouseWheel.

The prototype of OnMouseWheel is:

BOOL OnMouseWheel (UINT nFlags, short zDelta, CPoint point)

Where
The nFlags and point parameters are identical to those passed to OnLButtonDown.  zDelta is the distance the wheel was rotated. The zDelta value is calibrated in multiples or divisions of WHEEL_DELTA, which is 120. A value less than zero indicates rotating while a value greater than zero indicates rotating forward (away from the user).

Double Dlicks

For a window to register a double click the window must be set up to be notified of a double click event by including the WNDCLASS style CS_DBLCLKS during Windows registration. This is set by default in a frame windows declaration. The MFC Message-Map Macro and associated Handling Function for dealing with a double click are

ON_WM_LBUTTONDBLCLK – OnLButtonDblClk(UINT, CPoint)
ON_WM_RBUTTONDBLCLK – OnRButtonDblClk(UINT, CPoint)
ON_WM_MBUTTONDBLCLK – OnMButtonDblClk(UINT, CPoint)

Capturing the Mouse

A window procedure normally receives mouse messages only when the mouse cursor is positioned over the client or nonclient area of the window however a program might need to receive mouse messages when the mouse is outside the window. For example, if a mouse button is clicked inside a window but the mouse moves outside the window’s client area before releasing that button then the window will not receive the button-up event. To remedy this problem, windows allows the application to ‘capture’ the mouse and continue to receive mouse messages when a cursor moves outside the application windows. Windows will then continue to receive messages until the button is released or the capture is cancelled. The mouse is captured with CWnd member function SetCapture() and released with CWnd member function ReleaseCapture(). These functions are normally executed in the button-down and button-up handlers

The Hourglass Cursor

When an application undertakes a lengthy processing task the usual procedure is to display an hourglass to indicate that the application is “busy.” The CWaitCursor class allows any application to display a wait cursor. To display a WaitCursor define the CWaitCursor object variable before the code that performs the lengthy operation; this will cause the object’s constructor to be displayed automatically. When the object goes out of scope its destructor will set the cursor to the previous cursor.

void LengthyFunction( )
{
   CWaitCursor wait;   // display wait cursor
   //lengthy process
} // wait cursor removed when function goes out of scope

Changing the mouse icon

The CButton member function SetCursor() allows an application to change the mouse cursor. The syntax for this function is

HCURSOR SetCursor(HCURSOR hCursor);

Where hCursor is handle to the cursor. The cursor can be created by the CreateCursor() function or loaded by the LoadCursor() or LoadImage() function. If this parameter is NULL, the cursor is removed from the screen.
The return value is the handle to the previous cursor or NULL if there was no previous cursor.

For further information about setting the cursor icon go to the following link
https://support.microsoft.com/en-gb/help/131991/how-to-change-the-mouse-pointer-for-a-window-in-mfc-by-using-visual-c

Example

The following short program demonstrates how Windows handles messages from both the client and non-client areas of the screen, together with the alt and ctrl keys. Output describing the area clicked and the coordinate of the area clicked is displayed in the main window.

Display Code Download Code