using System; using System.Windows; using System.Windows.Forms; using Cursor = System.Windows.Forms.Cursor; using Point = System.Drawing.Point; using Size = System.Drawing.Size; namespace JD.ScreenWrappers { /// /// Helper class for GUI stuff. /// public static class GuiUtil { /// /// Wraps the coordinate around the given control. /// Also places to mouse to the new location, if wrapping is needed. /// /// /// /// public static void WrapAround(Window control, ref double lastMousePositionX, ref double lastMousePositionY) { Size screenSize = ComputeTotalScreenSize(); Point cursorPosition = Cursor.Position; double viewWidth = control.Width; double viewHeight = control.Height; bool isNewPosition = false; if (lastMousePositionX < 0 || cursorPosition.X == 0) { lastMousePositionX = viewWidth - 1; isNewPosition = true; } else if (lastMousePositionX >= viewWidth || cursorPosition.X == screenSize.Width - 1) { lastMousePositionX = 0; isNewPosition = true; } if (lastMousePositionY < 0 || cursorPosition.Y == 0) { lastMousePositionY = viewHeight - 1; isNewPosition = true; } else if (lastMousePositionY >= viewHeight || cursorPosition.Y == screenSize.Height - 1) { lastMousePositionY = 0; isNewPosition = true; } if (isNewPosition) { System.Windows.Point newWPFPosition = control.PointToScreen(new System.Windows.Point(lastMousePositionX, lastMousePositionY)); Point newPosition = new Point(Convert.ToInt32(newWPFPosition.X), Convert.ToInt32(newWPFPosition.Y)); if (cursorPosition != newPosition) { Cursor.Position = newPosition; } } } /// /// Wraps the coordinate around the given control. /// Also places to mouse to the new location, if wrapping is needed. /// /// /// /// public static void WrapAround(IControl control, ref int lastMousePositionX, ref int lastMousePositionY) { Size screenSize = ComputeTotalScreenSize(); Point cursorPosition = Cursor.Position; int viewWidth = control.Size.Width; int viewHeigth = control.Size.Height; bool isNewPosition = false; if (lastMousePositionX < 0 || cursorPosition.X == 0) { lastMousePositionX = viewWidth - 1; isNewPosition = true; } else if (lastMousePositionX >= viewWidth || cursorPosition.X == screenSize.Width - 1) { lastMousePositionX = 0; isNewPosition = true; } if (lastMousePositionY < 0 || cursorPosition.Y == 0) { lastMousePositionY = viewHeigth - 1; isNewPosition = true; } else if (lastMousePositionY >= viewHeigth || cursorPosition.Y == screenSize.Height - 1) { lastMousePositionY = 0; isNewPosition = true; } if (isNewPosition) { Point newPosition = control.PointToScreen(new Point(lastMousePositionX, lastMousePositionY)); if (cursorPosition != newPosition) { Cursor.Position = newPosition; } } } public static Size ComputeTotalScreenSize() { int screenWidth = 0; int screenHeight = 0; foreach (Screen s in Screen.AllScreens) { screenWidth = System.Math.Max(screenWidth, s.Bounds.Right); screenHeight = System.Math.Max(screenHeight, s.Bounds.Bottom); } return new Size(screenWidth, screenHeight); } /// /// Overload for the real thing. /// public static void WrapAround(Control control, ref int lastMousePositionX, ref int lastMousePositionY) { WrapAround(new WindowsFormsControl(control), ref lastMousePositionX, ref lastMousePositionY); } } }