Joshua Minnie

Just a place for me to write my thoughts down

Find Mouse Position Whenever You Need It

When responding to an event from a control, some events provide back the mouse coordinates, but not all of them do. So how do you get the mouse coordinates when your in an event that doesn’t provide them in the event args?

Well, the answer is simple: Cursor.Position. Cursor.Position provides a convenient way to get the cursor’s position relative to the screen. However, in some cases you want it relative to a control that’s on the form, or even the form itself. Here’s how you can do that:

  Point point = myControlOrForm.PointToClient(Cursor.Position);
  // Now fire an appropriate event with MouseEventArgs or whatever
  // you may need to do with the point.
                             new MouseEventArgs(MouseButtons.Left,

Note: The Cursor object is found in the System.Windows.Forms namespace.