I am working on an application that will draw a screen capture box on the screen around the user's mouse coordinates. I am trying to get this to work across multiple monitors. Using Cursor.Position I can get the global coordinates and determine what screen the user is on, but because the coordinates I receive are global and not relative to the monitor it is on, I am running into difficulties converting the global coordinates to screen-relative positioning.
I can use basic logic to get the relative coordinates when the monitors are all lined up vertically, but I am unsure how to approach the problem when the monitors are set up in a unique fashion (i.e. Not all monitors are the same size, one is to the right of two vertical monitors, etc.)
Here is what I have so far:
_screens = ScreenHelper.GetMonitorsInfo();
CursorPosition = Cursor.Position;
var currentDevice = Screen.FromPoint(CursorPosition);
if (!currentDevice.Primary)
{
// If the current screen is not the primary monitor, we need to calculate the cursor's current position relative to the screen.
//Find the position in the screens array that the cursor is located, then the position of the primary display.
var cursorIndex = _screens.IndexOf(_screens.Find(x => x.DeviceName == currentDevice.DeviceName));
var primaryIndex = _screens.IndexOf(_screens.Find(x => x.DeviceName == Screen.PrimaryScreen.DeviceName));
//Cursor is to the right of primary screen.
if (primaryIndex > cursorIndex)
{
for (int i = cursorIndex + 1; i <= primaryIndex; i++)
{
CursorPosition = new Point(CursorPosition.X - _screens[i].HorizontalResolution, CursorPosition.Y);
}
}
//Cursor is to the left of primary screen.
else
{
for (int i = cursorIndex - 1; i >= primaryIndex; i--)
{
CursorPosition = new Point(CursorPosition.X + _screens[i].HorizontalResolution, CursorPosition.Y);
}
}
}
public static List<DeviceInfo> GetMonitorsInfo()
{
_result = new List<DeviceInfo>();
EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, MonitorEnum, IntPtr.Zero);
return _result;
}
[DllImport("user32.dll")]
private static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, MonitorEnumDelegate lpfnEnum, IntPtr dwData);
I'm part way there, but I am unsure how to take into account horizontal or even diagonally relative monitor positioning. How can I effectively get the Mouse Coordinates relative to the screen the cursor is currently on?