private void OnStateChanged(object sender, EventArgs e) { var screen = Screen.FromHandle(new WindowInteropHelper(this).Handle); var thickness = new Thickness(0); if (WindowState != WindowState.Maximized) { var currentDpiScaleFactor = SystemHelper.GetCurrentDPIScaleFactor(); var workingArea = screen.WorkingArea; MaxHeight = (workingArea.Height + 16) / currentDpiScaleFactor; MaxWidth = double.PositiveInfinity; if (WindowState != WindowState.Maximized) { SetMaximizeButtonsVisibility(Visibility.Visible, Visibility.Collapsed); } } else { thickness = GetDefaultMarginForDpi(); if (_previousState == WindowState.Minimized || Math.Abs(Left - _positionBeforeDrag.X) < DoubleTolerance && Math.Abs(Top - _positionBeforeDrag.Y) < DoubleTolerance) { thickness = GetFromMinimizedMarginForDpi(); } SetMaximizeButtonsVisibility(Visibility.Collapsed, Visibility.Visible); } _layoutRoot.Margin = thickness; _previousState = WindowState; }
protected CustomWindow() { var currentDpiScaleFactor = SystemHelper.GetCurrentDPIScaleFactor(); var screen = Screen.FromHandle(new WindowInteropHelper(this).Handle); SizeChanged += OnSizeChanged; StateChanged += OnStateChanged; Loaded += OnLoaded; var workingArea = screen.WorkingArea; MaxHeight = (workingArea.Height + 16) / currentDpiScaleFactor; SystemEvents.DisplaySettingsChanged += SystemEvents_DisplaySettingsChanged; AddHandler(MouseLeftButtonUpEvent, new MouseButtonEventHandler(OnMouseButtonUp), true); AddHandler(MouseMoveEvent, new MouseEventHandler(OnMouseMove)); }
private void OnMouseMove(object sender, MouseEventArgs e) { if (!_isMouseButtonDown) { return; } var currentDpiScaleFactor = SystemHelper.GetCurrentDPIScaleFactor(); var position = e.GetPosition(this); var screen = PointToScreen(position); var x = _mouseDownPosition.X - position.X; var y = _mouseDownPosition.Y - position.Y; if (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)) > 1) { var actualWidth = _mouseDownPosition.X; if (_mouseDownPosition.X <= 0) { actualWidth = 0; } else if (_mouseDownPosition.X >= ActualWidth) { actualWidth = _widthBeforeMaximize; } if (WindowState == WindowState.Maximized) { ToggleWindowState(); Top = (screen.Y - position.Y) / currentDpiScaleFactor; Left = (screen.X - actualWidth) / currentDpiScaleFactor; CaptureMouse(); } _isManualDrag = true; Top = (screen.Y - _mouseDownPosition.Y) / currentDpiScaleFactor; Left = (screen.X - actualWidth) / currentDpiScaleFactor; } }