/// <summary> /// Move window to new screen and scale it as necisarry /// </summary> /// <param name="newScreen"></param> public static void MoveActiveWindowToScreen(Screen newScreen) { IntPtr handle = GetForegroundWindow(); Rect rect = new Rect(); GetWindowRect(handle, ref rect); Rectangle childRect = rect.ToRectangle(); Screen currentScreen = GetScreenContainingWindow(childRect); Rectangle currentWorkingArea = currentScreen.WorkingArea; double xPosPercentage = (1.0*childRect.X - currentWorkingArea.X) / currentWorkingArea.Width; double yPosPercentage = (1.0 * childRect.Y - currentWorkingArea.Y) / currentWorkingArea.Height; int newX = (int)(newScreen.WorkingArea.X + newScreen.WorkingArea.Width * xPosPercentage); int newY = (int)(newScreen.WorkingArea.Y + newScreen.WorkingArea.Height * yPosPercentage); if (newScreen.WorkingArea.Width != currentScreen.WorkingArea.Width || newScreen.WorkingArea.Height != currentScreen.WorkingArea.Height) { //different size working area/resolution //scale window to new resolution double widthPercentage = 1.0 * (rect.Width - resizeOffset.X) / currentWorkingArea.Width; double heightPercentage = 1.0 * (rect.Height - resizeOffset.Y) / currentWorkingArea.Height; int newWidth = (int)(newScreen.WorkingArea.Width * widthPercentage); int newHeight = (int)(newScreen.WorkingArea.Height * heightPercentage); newWidth += resizeOffset.X; newHeight += resizeOffset.Y; MoveActiveWindowTo(newX, newY, newWidth, newHeight, false); } else MoveActiveWindowTo(newX, newY, false); }
/// <summary> /// Returns the screen containing the currently active window /// </summary> /// <returns></returns> public static Screen GetScreenActiveWindowIsOn() { IntPtr handle = GetForegroundWindow(); Rect rect = new Rect(); GetWindowRect(handle, ref rect); Rectangle childRect = rect.ToRectangle(); return GetScreenContainingWindow(childRect); }
/// <summary> /// Move this window off this screen in the given direction eg (1,0) for right. /// </summary> /// <param name="dir">Direction to move window in units of the active screens' resolution.</param> public static void MoveActiveWindowOffScreenInDirection(Point dir) { if(Screen.AllScreens.Length==1)return; IntPtr handle = GetForegroundWindow(); Rect rect = new Rect(); GetWindowRect(handle, ref rect); Rectangle childRect = rect.ToRectangle(); Screen currentScreen = GetScreenContainingWindow(childRect); Rectangle workingArea = currentScreen.WorkingArea; childRect.Offset(workingArea.Width * dir.X, workingArea.Height * dir.Y); Screen newScreen = GetScreenContainingWindow(childRect); if (WrapLeftRightScreens) { if (newScreen == currentScreen) { if (dir.Y == 0) { if (dir.X == 1)//wrap right to left most newScreen = GetLeftMostScreen(); else if (dir.X == -1)//wrap Left to right most newScreen = GetRightMostScreen(); } } } MoveActiveWindowToScreen(newScreen); }
public static RectangleF GetActiveWindowRelativeRectangleF() { IntPtr handle = GetForegroundWindow(); Rect rect = new Rect(); GetWindowRect(handle, ref rect); Rectangle childRect = rect.ToRectangle(); Rectangle workingSpace = GetScreenContainingWindow(childRect).WorkingArea; float relativeX = 1f * (childRect.X - positionOffset.X - workingSpace.X) / workingSpace.Width; float relativeY = 1f * (childRect.Y - positionOffset.Y - workingSpace.Y) / workingSpace.Height; float relativeW = 1f * (childRect.Width - resizeOffset.X) / workingSpace.Width; float relativeH = 1f * (childRect.Height - resizeOffset.Y) / workingSpace.Height; return new RectangleF(relativeX, relativeY, relativeW, relativeH); }