How Mouse Messaging Operates

Unlike with keyboard messages, the system must be able to send mouse messages to any visible window in the system, even if it is not the active window. We can see this by using Spy++ to view mouse messages on a system-wide level. Start Spy++ and click the Spy Messages . . . menu item. The Message Options dialog will appear. Check the All Windows In System checkbox on the Windows tab, and check the Mouse checkbox on the Messages tab. Finally, click the OK button. This will start Spy++ recording mouse messages. As you move the mouse cursor over windows in the system, you will see that a window does not have to be active to receive mouse messages. The Spy++ output looks similar to this:

WM_SETCURSOR       (Sent)
WM_SETCURSOR       (Returned)
WM_MOUSEMOVE       (Posted)

This series of messages is repeated over and over again as the user moves the mouse across any visible windows on the desktop. Right- or left-clicking any visible windows also sends the appropriate mouse messages to that window.

A window does have to be visible to the mouse cursor to receive messages, though. Any window or part of a window hidden behind another window in the Z-order will not be able to receive mouse messages. You might notice that the Windows desktop also receives and processes mouse messages. The only window that does not seem to receive messages at any time is the Spy++ window. Spy++ watches for messages that are directed to any of its own windows and will not display them. This is by ...

Get Subclassing and Hooking with Visual Basic now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.