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:
1
2
3
4
5
6
7
8
9
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.