Skip to content

Instantly share code, notes, and snippets.

@papinko
Last active September 10, 2024 04:36
Show Gist options
  • Save papinko/da2caa26e8ae16f24b54f20c36aff7aa to your computer and use it in GitHub Desktop.
Save papinko/da2caa26e8ae16f24b54f20c36aff7aa to your computer and use it in GitHub Desktop.
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetCursorPos(ref Win32Point pt);
[StructLayout(LayoutKind.Sequential)]
struct Win32Point
{
public Int32 X;
public Int32 Y;
};
public static Point GetCurrentCursorPosition(Visual relativeTo)
{
Win32Point w32Mouse = new Win32Point();
GetCursorPos(ref w32Mouse);
if (relativeTo == null)
return new Point(w32Mouse.X, w32Mouse.Y);
else
return relativeTo.PointFromScreen(new Point(w32Mouse.X, w32Mouse.Y));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment