Your browser doesn't support JavaScript Working with the Mouse - Windows Programming

Working with the Mouse

Windows can handle 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 and non-client area. The non-client area is the area of a window consisting of borders, a title bar, a menu bar, a minimize/maximize button and an 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 the Message-Map Macro Handling Function

DescriptionMessage map macroHandling function
Left mouse button pressed.ON_WM_LBUTTONDOWNOnLButtonDown
Left mouse button released.ON_WM_LBUTTONUPOnLButtonUp
Left mouse button double-clicked.ON_WM_LBUTTONDBLCLKOnLButtonDblClk
Middle mouse button pressed.ON_WM_MBUTTONDOWNOnMButtonDown
Middle mouse button released.ON_WM_MBUTTONUPOnMButtonUp
Middle mouse button double-clicked.ON_WM_MBUTTONDBLCLKOnMButtonDblClk
Right mouse button pressed.ON_WM_RBUTTONDOWNOnRButtonDown
Right mouse button released.ON_WM_RBUTTONUPOnRButtonUp
Right mouse button double-clicked.ON_WM_RBUTTONDBLCLKOnRButtonDblClk
Cursor moved over client area.ON_WM_MOUSEMOVEOnMouseMove

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

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

MessageMessage-Map MacroHandling Function
WM_NCLBUTTONDOWNON_WM_NCLBUTTONDOWNOnNcLButtonDown
WM_NCLBUTTONUPON_WM_NCLBUTTONUPOnNcLButtonUp
WM_NCLBUTTONDBLCLKON_WM_NCLBUTTONDBLCLKOnNcLButtonDblClk
WM_NCMBUTTONDOWNON_WM_NCMBUTTONDOWNOnNcMButtonDown
WM_NCMBUTTONUPON_WM_NCMBUTTONUPOnNcMButtonUp
WM_NCMBUTTONDBLCLKON_WM_NCMBUTTONDBLCLKOnNcMButtonDblClk
WM_NCRBUTTONDOWNON_WM_NCRBUTTONDOWNOnNcRButtonDown
WM_NCRBUTTONUPON_WM_NCRBUTTONUPOnNcRButtonUp
WM_NCRBUTTONDBLCLKON_WM_NCRBUTTONDBLCLKOnNcRButtonDblClk
WM_NCMOUSEMOVEON_WM_NCMOUSEMOVEOnNcMouseMove

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 in the window’s nonclient area the event occurred. A selection of these hit-test codes is shown in the list below.

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

To register a double click, a 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 window. 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 instantiate a CWaitCursor object variable before the code that performs the lengthy operation, the object’s constructor will automatically cause the wait cursor to be displayed. 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 a 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.

Download Code