Exemple #1
0
        /// <summary>
        ///   Save the position of a form to the user settings. Hides the window
        ///   as a side-effect.
        /// </summary>
        /// <param name = "name">The name to use when writing the position to the
        ///   settings</param>
        private void SavePosition(String name)
        {
            try
            {
                var rectangle =
                    WindowState == FormWindowState.Normal
                        ? DesktopBounds
                        : RestoreBounds;

                var formWindowState =
                    WindowState == FormWindowState.Maximized
                        ? FormWindowState.Maximized
                        : FormWindowState.Normal;

                // Write to the user settings:
                if (_windowPositionList == null)
                {
                    _windowPositionList = WindowPositionList.Load();
                }
                WindowPosition windowPosition = _windowPositionList.Get(name);
                // Don't save location when we center modal form
                if (windowPosition != null && Owner != null && _windowCentred)
                {
                    if (rectangle.Width <= windowPosition.Rect.Width && rectangle.Height <= windowPosition.Rect.Height)
                    {
                        rectangle.Location = windowPosition.Rect.Location;
                    }
                }

                int deviceDpi = GetCurrentDeviceDpi();
                var position  = new WindowPosition(rectangle, deviceDpi, formWindowState, name);
                _windowPositionList.AddOrUpdate(position);
                _windowPositionList.Save();
            }
            catch (Exception)
            {
                //TODO: howto restore a corrupted config?
            }
        }
        /// <summary>
        ///   Save the position of a form to the user settings. Hides the window
        ///   as a side-effect.
        /// </summary>
        /// <param name = "name">The name to use when writing the position to the
        ///   settings</param>
        private void SavePosition(string name)
        {
            if (!_needsPositionSave)
            {
                return;
            }

            _needsPositionSave = false;

            try
            {
                var rectangle = WindowState == FormWindowState.Normal
                    ? DesktopBounds
                    : RestoreBounds;

                var formWindowState = WindowState == FormWindowState.Maximized
                    ? FormWindowState.Maximized
                    : FormWindowState.Normal;

                if (_windowPositionList == null)
                {
                    _windowPositionList = WindowPositionList.Load();

                    if (_windowPositionList == null)
                    {
                        return;
                    }
                }

                WindowPosition windowPosition = _windowPositionList.Get(name);

                // Don't save location when we center modal form
                if (windowPosition != null && Owner != null && _windowCentred)
                {
                    if (rectangle.Width <= windowPosition.Rect.Width && rectangle.Height <= windowPosition.Rect.Height)
                    {
                        rectangle.Location = windowPosition.Rect.Location;
                    }
                }

                var position = new WindowPosition(rectangle, DpiUtil.DpiX, formWindowState, name);
                _windowPositionList.AddOrUpdate(position);
                _windowPositionList.Save();
            }
            catch
            {
                // TODO: how to restore a corrupted config?
            }
        }
        /// <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)
            {
                return;
            }

            _windowCentred = StartPosition == FormStartPosition.CenterParent;

            var position = LookupWindowPosition(GetType().Name);

            if (position == null)
            {
                return;
            }

            _needsPositionRestore = false;

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

                return;
            }

            SuspendLayout();

            StartPosition = FormStartPosition.Manual;

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

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

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

                DesktopLocation = location;
            }
            else
            {
                // Calculate location for modal form with parent
                Location = new Point(
                    Owner.Left + (Owner.Width / 2) - (Width / 2),
                    Math.Max(0, Owner.Top + (Owner.Height / 2) - (Height / 2)));
            }

            if (WindowState != position.State)
            {
                WindowState = position.State;
            }

            ResumeLayout();

            return;

            Rectangle?FindWindowScreen(Point location)
            {
                var distance = new SortedDictionary <float, Rectangle>();

                foreach (var rect in Screen.AllScreens.Select(screen => screen.WorkingArea))
                {
                    if (rect.Contains(location) && !distance.ContainsKey(0.0f))
                    {
                        return(null); // title in screen
                    }

                    int midPointX = rect.X + (rect.Width / 2);
                    int midPointY = rect.Y + (rect.Height / 2);
                    var d         = (float)Math.Sqrt(((location.X - midPointX) * (location.X - midPointX)) +
                                                     ((location.Y - midPointY) * (location.Y - midPointY)));
                    distance.Add(d, rect);
                }

                return(distance.FirstOrDefault().Value);
            }

            WindowPosition LookupWindowPosition(string name)
            {
                try
                {
                    if (_windowPositionList == null)
                    {
                        _windowPositionList = WindowPositionList.Load();
                    }

                    var pos = _windowPositionList?.Get(name);

                    if (pos != null && !pos.Rect.IsEmpty)
                    {
                        return(pos);
                    }
                }
                catch
                {
                    // TODO: how to restore a corrupted config?
                }

                return(null);
            }
        }