Skip to Content
C# Cookbook
book

C# Cookbook

by Stephen Teilhet, Jay Hilyard
January 2004
Beginner to intermediate
864 pages
22h 18m
English
O'Reilly Media, Inc.
Content preview from C# Cookbook

7.11. Using Windows Hooks to Manipulate the Mouse

Problem

Many new mice have more than just a left and right button. Nowadays mice come with several additional buttons and a mouse wheel. You need to allow your application to take advantage of these new mice features. Additionally, you might need to know where the current location of the mouse is on a particular window, whether it is on the client area of the window (where your menus, toolbars, and controls are placed in the window), the nonclient area of the window (window border, title bar, close button, etc.), or the x and y coordinates of the mouse pointer.

Solution

Use the mouse events that are built into the System.Windows.Forms.Form class.

Discussion

There are seven mouse events that exist in the System.Windows.Forms.Form class. These are, in the order in which they occur:

  • MouseEnter

  • MouseMove

  • MouseHover or MouseDown or MouseWheel

  • MouseUp (if MouseDown was the previously raised event)

  • MouseLeave

Most of these events accept a MouseEventArgs object that contains all the information about the mouse when the event is raised. The MouseEventArgs class contains the following data:

  • Which button the user is acting on

  • The number of times the mouse button was clicked

  • The direction and speed of the mouse wheel

  • The x and y coordinate of the mouse pointer

Your code can make use of any one or more of these events on the Form class along with the MouseEventArgs object.

See Also

See Recipe 7.10; Subclassing & Hooking with Visual Basic by Stephen ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C# Cookbook

C# Cookbook

Joe Mayo
C# Cookbook, 2nd Edition

C# Cookbook, 2nd Edition

Jay Hilyard, Stephen Teilhet
ASP.NET Cookbook

ASP.NET Cookbook

Michael A Kittel, Geoffrey T. LeBlond

Publisher Resources

ISBN: 0596003390Supplemental ContentCatalog PageErrata