If you don't want to reference Forms you can use interop to get the cursor position:


SUBMITTED BY: menamagice

DATE: Aug. 9, 2017, 2:01 a.m.

FORMAT: Text only

SIZE: 834 Bytes

HITS: 238

  1. 64
  2. down vote
  3. If you don't want to reference Forms you can use interop to get the cursor position:
  4. /// <summary>
  5. /// Struct representing a point.
  6. /// </summary>
  7. [StructLayout(LayoutKind.Sequential)]
  8. public struct POINT
  9. {
  10. public int X;
  11. public int Y;
  12. public static implicit operator Point(POINT point)
  13. {
  14. return new Point(point.X, point.Y);
  15. }
  16. }
  17. /// <summary>
  18. /// Retrieves the cursor's position, in screen coordinates.
  19. /// </summary>
  20. /// <see>See MSDN documentation for further information.</see>
  21. [DllImport("user32.dll")]
  22. public static extern bool GetCursorPos(out POINT lpPoint);
  23. public static Point GetCursorPosition()
  24. {
  25. POINT lpPoint;
  26. GetCursorPos(out lpPoint);
  27. //bool success = User32.GetCursorPos(out lpPoint);
  28. // if (!success)
  29. return lpPoint;
  30. }

comments powered by Disqus