Exemplo n.º 1
0
        /// <summary>
        ///   Restores the position of a form from the user settings. Does
        ///   nothing if there is no entry for the form in the settings, or the
        ///   setting would be invisible on the current display configuration.
        /// </summary>
        protected virtual void RestorePosition()
        {
            if (!_needsPositionRestore)
            {
                return;
            }

            if (WindowState == FormWindowState.Minimized)
            {
                // TODO: do we still need to assert when restored it is shown on the correct monitor?
                return;
            }

            var position = _windowPositionManager.LoadPosition(this);

            if (position is null)
            {
                return;
            }

            _needsPositionRestore = false;

            var workingArea = _getScreensWorkingArea();

            if (!workingArea.Any(screen => screen.IntersectsWith(position.Rect)))
            {
                if (position.State == FormWindowState.Maximized)
                {
                    WindowState = FormWindowState.Maximized;
                }

                return;
            }

            SuspendLayout();

            var windowCentred = StartPosition == FormStartPosition.CenterParent;

            StartPosition = FormStartPosition.Manual;

            if (FormBorderStyle == FormBorderStyle.Sizable ||
                FormBorderStyle == FormBorderStyle.SizableToolWindow)
            {
                Size = DpiUtil.Scale(position.Rect.Size, originalDpi: position.DeviceDpi);
            }

            if (Owner is null || !windowCentred)
            {
                var location = DpiUtil.Scale(position.Rect.Location, originalDpi: position.DeviceDpi);

                if (WindowPositionManager.FindWindowScreen(location, workingArea) is Rectangle rect)
                {
                    location.Y = rect.Y;
                }

                DesktopLocation = location;
            }
 public static bool IsDisplayedOn10Percent(Rectangle screen, Rectangle window)
 => WindowPositionManager.IsDisplayedOn10Percent(screen, window);
 internal TestAccessor(WindowPositionManager windowPositionManager)
 {
     _windowPositionManager = windowPositionManager;
 }