tags:

views:

245

answers:

4

I don't understand why Cursor.Position is different from MouseEventArgs.Location, they should be the same, don't they?

Edit: The answer seems to suggest that Cursor.Position== PointToScreen(MouseEventArgs.Location), but my testing indicates otherwise.

A: 

I think Cursor.Position is relative to the desktop, whereas MouseEventArgs.Location is relative to the control that fired the event

Rob Fonseca-Ensor
My testing indicates that `PointToScreen(MouseEventArgs.Position)` is not equaled to `Cursor.Position`.
Ngu Soon Hui
A: 

MouseEventArgs.Position is relative to the client rectangle of the control, Cursor.Position is in screen coordinate.

Benny
My testing indicates that `PointToScreen(MouseEventArgs.Position)` is not equaled to `Cursor.Position`.
Ngu Soon Hui
I tested, they are the same.
Benny
+1  A: 

Hello,

MouseEventArgs.location gives you the cursor position relative to the control during the event.

Windows.Forms.Cursor.Position gives you the cursor position relative to the desktop.

Obviously, these two need not be and are not same. The latter can be used without an event as such too unlike MouseEventArgs.Location

Srivatsan Iyer
My testing indicates that `PointToScreen(MouseEventArgs.Position)` is not equaled to `Cursor.Position`.
Ngu Soon Hui
That's because MouseEventArgs.Position is the mouse position at the time of the event and Cursor.Position is the current position.
Tommy Carlier
+6  A: 

MouseEventArgs.Position is the mouse cursor position relative to the control during the event, at the time of the event.

Cursor.Position is the current mouse cursor position relative to the desktop.

Tommy Carlier
This is *the* answer I am seeking for; if you are running in a debugger, and you set the control size to be the screen size, the two can differ, and this was what confused me.
Ngu Soon Hui
even the control size is the screen size, but the client size is not the screen size.
Benny
I either don't understand the answer, or it is incorrect. Made a simle form with a button and labels with information about discussed values. Pressed a button and then didn't move mouse. Got in the tace labels on the form: Cursor.Position = {X=159,Y=246}, e.Location = {X=11,Y=6}, PointToScreen(e.Location) = {X=69,Y=86}.
Dmitry Tashkinov
Since I didn't move mouse between the button press and the information output, "time of the event" must not have mattered.
Dmitry Tashkinov
Bear in mind that `PointToScreen` and `PointToClient` are relative to the control you called them on. So `Form.PointToScreen()` will not be the same as `Form1.Button1.PointToScreen()`
Pondidum