private void Window_LocationChanged(object sender, EventArgs e) { if (!_isRepositioning) { // Position was changed by Windows, e.g. due to screen resizing or app reloading // Snap to screen boundaries _isRepositioning = true; var scale = LayoutUtilities.GetDpiScale(this); scale = DpiUtilities.GetSystemScale(); scale = SystemScale; var screen = System.Windows.Forms.Screen.FromPoint(new System.Drawing.Point((int)Left, (int)Top)); Left = Math.Min(Math.Max(Left, screen.WorkingArea.Left / scale.X), screen.WorkingArea.Right / scale.X - ActualWidth); Top = Math.Min(Math.Max(Top, screen.WorkingArea.Top / scale.Y), screen.WorkingArea.Bottom / scale.Y - ActualHeight); // Make sure docking state is correct var dockState = ToolsDockState(scale, screen); Trace.WriteLine("Window position changed, new position: (" + Left + ", " + Top + "), Current dock state:" + _currentDockState + ", Dock state for position:" + dockState); if (dockState != _currentDockState) { // Adjust tool window position to match the old dock state switch (_currentDockState) { case DockState.Top: Top = screen.WorkingArea.Top / scale.Y; break; case DockState.Left: Left = screen.WorkingArea.Left / scale.X; break; case DockState.Right: Left = screen.WorkingArea.Right / scale.X - ActualWidth; break; case DockState.Bottom: Top = screen.WorkingArea.Bottom / scale.Y - ActualHeight; break; case DockState.Middle: Left = (screen.WorkingArea.Left + screen.WorkingArea.Right) / 2 / scale.X - ActualWidth / 2; Top = (screen.WorkingArea.Top + screen.WorkingArea.Bottom) / 2 / scale.Y - ActualHeight / 2; break; } Trace.WriteLine("Adjusted dockState:" + _currentDockState + ", adjusted position: (" + Left + ", " + Top + ")"); } _isRepositioning = false; Settings.Default.ToolDocking = _currentDockState.ToString(); Settings.Default.ToolPosition = new System.Drawing.Point((int)Left, (int)Top); Settings.Default.Save(); } }
private void Window_MouseMove(object sender, MouseEventArgs e) { if (_dragging && e.LeftButton == MouseButtonState.Pressed) { // Get the current mouse position Point mousePos = e.GetPosition(null); Vector diff = mousePos - _startPoint; // Snap to screen boundaries _isRepositioning = true; var scale = LayoutUtilities.GetDpiScale(this); scale = DpiUtilities.GetSystemScale(); scale = SystemScale; //scale = new DpiScale(); var screen = System.Windows.Forms.Screen.FromPoint(System.Windows.Forms.Control.MousePosition); System.Diagnostics.Debug.WriteLine("Screen.Left = " + screen.WorkingArea.Left.ToString()); Left = Math.Min(Math.Max(Left + diff.X, screen.WorkingArea.Left / scale.X), screen.WorkingArea.Right / scale.X - ActualWidth); Top = Math.Min(Math.Max(Top + diff.Y, screen.WorkingArea.Top / scale.Y), screen.WorkingArea.Bottom / scale.Y - ActualHeight); var dockState = ToolsDockState(scale, screen); if (dockState == DockState.Left || dockState == DockState.Right) { if (dockState == DockState.Right && StackContainer.Orientation != Orientation.Vertical) { // Adjust for snapping to the right Left += ActualWidth - ActualHeight; _startPoint.X -= ActualWidth - ActualHeight; } } else { if (_currentDockState == DockState.Right && StackContainer.Orientation != Orientation.Horizontal) { // Adjust for snapping from the right Left += ActualWidth - ActualHeight; _startPoint.X -= ActualWidth - ActualHeight; } } _isRepositioning = false; AdjustOrientation(dockState); _currentDockState = dockState; } }