Search code examples
c#winformscursor-position

Why does Cursor.Position have a wrong value while debugging?


In a debug session, Cursor.Position = {X = 1867 Y = 538} and Screen.FromPoint(Cursor.Position) returns a Screen object that has the bounds {X = 0 Y = 0 Width = 1366 Height = 768}.

If I deactivate the first screen, the Screen object has the bounds {X = 0 Y = 0 Width = 1680 Height = 1050} (the second screen has bigger resolution than the first screen).

So I see that the Screen.Bounds rectangle does not tell me the rectangle that is the screen relative to the global resolution that includes both screens, but it tells me the size of the screen, with X = Y = 0.

The problem is that the cursor position {X = 2203 Y = 579} should be on the second screen. I thought of the possibilities:

  1. the cursor position is locked while doing a drag-drop operation.
  2. the first screen having a smaller resolution than the second screen, may create some virtual space above the first screen.

Either with the first/second screen/s enabled or disabled, I have the same problem:

MyListView.PointToClient(Cursor.Position)

returns a point with {X = 1787 Y = -79}, Y being negative, it won't work to find the ListViewItem under the mouse cursor.

MyListView.Bounds = {X = 3 Y = 38 Width = 406 Height = 219}.

I moved the program window fixed on the left-top corner of the first screen, and the same thing takes place, the Cursor.Position is {X = 2231 Y = 571}.


Solution

  • Now I see that while debugging, the Cursor.Position gets updated (it is not locked while the execution is locked at a breakpoint).

    Finally, the problem was that I checked a variabile to not be equal to -1, instead of to be equal to -1.