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.
  myControlOrForm_MouseClick(myControlOrForm,
                             new MouseEventArgs(MouseButtons.Left,
                                                1,
                                                point.X,
                                                point.Y,
                                                0));

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

Josh Minnie

Christian, family man, web developer. I love spending time with my family and my job is also my hobby. Owner of Nissi Solutions.