private void spawnInMonitor() { Rect rect = Rect.Empty; // Use the active window to determine the active monitor Win32.GUITHREADINFO gui = new Win32.GUITHREADINFO(); gui.cbSize = Marshal.SizeOf(gui); bool success = Win32.GetGUIThreadInfo(0, ref gui); if (success) { if (gui.hwndActive != IntPtr.Zero) { Win32.RECT windowRect = new Win32.RECT(); bool success2 = Win32.GetWindowRect(gui.hwndActive, ref windowRect); if (success2) { rect = windowRect.asRect(); } } } // If that fails, use the cursor to determine the active monitor if (rect.IsEmpty) { int left = System.Windows.Forms.Cursor.Position.X; int top = System.Windows.Forms.Cursor.Position.Y; rect = new Rect(left, top, 0, 0); } PlacementSide monitorPlacement = ((App)Application.Current).Preferences.monitorPlacement; WindowUtils.PutWindowNear(this, WindowUtils.MonitorWorkAreaFromRect(rect), monitorPlacement, PlacementInOut.INSIDE); }
internal static void PutWindowNear(Window window, Rect rect, PlacementSide side, PlacementInOut inOut) { Point corner = SelectPlacementSide(side, rect); Rect workArea = MonitorWorkAreaFromRect(rect); Rect windowRect = RectFromPlacementSide(side, inOut, corner, (int)window.ActualWidth, (int)window.ActualHeight); if (windowRect.Left < workArea.Left) { windowRect.X += workArea.Left - windowRect.Left; } else if (windowRect.Right > workArea.Right) { windowRect.X -= windowRect.Right - workArea.Right; } if (windowRect.Top < workArea.Top) { windowRect.Y += workArea.Top - windowRect.Top; } else if (windowRect.Bottom > workArea.Bottom) { windowRect.Y -= windowRect.Bottom - workArea.Bottom; } window.Left = windowRect.X; window.Top = windowRect.Y; }
internal static Point SelectPlacementSide(PlacementSide side, Rect windowRect) { Point point = new Point(); switch (side) { case PlacementSide.TOP_LEFT: case PlacementSide.CENTER_LEFT: case PlacementSide.BOTTOM_LEFT: point.X = windowRect.Left; break; case PlacementSide.TOP_CENTER: case PlacementSide.CENTER: case PlacementSide.BOTTOM_CENTER: default: point.X = (windowRect.Left + windowRect.Right) / 2; break; case PlacementSide.TOP_RIGHT: case PlacementSide.CENTER_RIGHT: case PlacementSide.BOTTOM_RIGHT: point.X = windowRect.Right; break; } switch (side) { case PlacementSide.TOP_LEFT: case PlacementSide.TOP_CENTER: case PlacementSide.TOP_RIGHT: point.Y = windowRect.Top; break; case PlacementSide.CENTER_LEFT: case PlacementSide.CENTER: case PlacementSide.CENTER_RIGHT: default: point.Y = (windowRect.Top + windowRect.Bottom) / 2; break; case PlacementSide.BOTTOM_LEFT: case PlacementSide.BOTTOM_CENTER: case PlacementSide.BOTTOM_RIGHT: point.Y = windowRect.Bottom; break; } return(point); }
private void windowPlacement_SelectionChanged(object sender, SelectionChangedEventArgs e) { PlacementSide item = (PlacementSide)e.AddedItems[0]; insideOutsidePlacement.IsEnabled = (item != PlacementSide.CENTER); }
internal static Rect RectFromPlacementSide(PlacementSide side, PlacementInOut inOut, Point corner, int width, int height) { Rect rect = new Rect(); switch (side) { case PlacementSide.TOP_LEFT: case PlacementSide.CENTER_LEFT: case PlacementSide.BOTTOM_LEFT: rect.X = corner.X; break; case PlacementSide.TOP_CENTER: case PlacementSide.CENTER: case PlacementSide.BOTTOM_CENTER: default: rect.X = corner.X - width / 2; break; case PlacementSide.TOP_RIGHT: case PlacementSide.CENTER_RIGHT: case PlacementSide.BOTTOM_RIGHT: rect.X = corner.X - width; break; } switch (side) { case PlacementSide.TOP_LEFT: case PlacementSide.TOP_CENTER: case PlacementSide.TOP_RIGHT: rect.Y = corner.Y; break; case PlacementSide.CENTER_LEFT: case PlacementSide.CENTER: case PlacementSide.CENTER_RIGHT: default: rect.Y = corner.Y - height / 2; break; case PlacementSide.BOTTOM_LEFT: case PlacementSide.BOTTOM_CENTER: case PlacementSide.BOTTOM_RIGHT: rect.Y = corner.Y - height; break; } rect.Width = width; rect.Height = height; /* Change some values for outside placement. If the placement is * on the top or bottom, clamp the opposite side of the Unicodex * window to that edge. Otherwise, if it's center-left or * center-right, clamp the opposite side in the left/right * direction. True-center positioning is unchanged. */ if (inOut == PlacementInOut.OUTSIDE) { switch (side) { case PlacementSide.TOP_LEFT: case PlacementSide.TOP_CENTER: case PlacementSide.TOP_RIGHT: rect.Y -= height; break; case PlacementSide.BOTTOM_LEFT: case PlacementSide.BOTTOM_CENTER: case PlacementSide.BOTTOM_RIGHT: rect.Y += height; break; case PlacementSide.CENTER_LEFT: rect.X -= width; break; case PlacementSide.CENTER_RIGHT: rect.X += width; break; } } return(rect); }