Example #1
0
        public static void SaveWindowProperties(DarkForm form, ConfigurationBase config)
        {
            var name = "Window_" + form.Name;

            // Don't save anything to config, if form is minimized
            if (form.WindowState == FormWindowState.Minimized)
            {
                return;
            }

            config.GetType().GetProperty(name + "_Maximized")?.SetValue(config, form.WindowState == FormWindowState.Maximized);

            // Set size only if form isn't maximized, otherwise we'll get corrupted coordinates.
            if (form.WindowState != FormWindowState.Maximized)
            {
                config.GetType().GetProperty(name + "_Size")?.SetValue(config, form.Size);
                config.GetType().GetProperty(name + "_Position")?.SetValue(config, form.Location);
            }
        }
Example #2
0
        public static void LoadWindowProperties(DarkForm form, ConfigurationBase config)
        {
            var name = "Window_" + form.Name;

            var size = config.GetType().GetProperty(name + "_Size")?.GetValue(config);
            var pos  = config.GetType().GetProperty(name + "_Position")?.GetValue(config);
            var max  = config.GetType().GetProperty(name + "_Maximized")?.GetValue(config);

            if (size is Size)
            {
                form.Size = (Size)size;
            }
            if (pos is Point)
            {
                form.Location = (Point)pos;
            }
            if (max is bool)
            {
                form.WindowState = (bool)max ? FormWindowState.Maximized : FormWindowState.Normal;
            }

            if (form.Location.X == -1 && form.Location.Y == -1)
            {
                if (form.ParentForm == null)
                {
                    form.StartPosition = FormStartPosition.CenterScreen;
                }
                else
                {
                    form.StartPosition = FormStartPosition.CenterParent;
                }
            }
            else
            {
                form.StartPosition = FormStartPosition.Manual;
            }

            // Properly clamp screen coords to fix issues with out-of-bounds windows
            if (form.Location.X < 0)
            {
                form.Location = new Point(0, form.Location.Y);
            }
            if (form.Location.Y < 0)
            {
                form.Location = new Point(form.Location.X, 0);
            }

            // Also clamp max. width/height if we're not on multidisplay config
            if (Screen.AllScreens.Length == 1)
            {
                var w = Screen.PrimaryScreen.Bounds.Width - form.Width;
                var h = Screen.PrimaryScreen.Bounds.Height - form.Height;
                if (form.Location.X >= w)
                {
                    form.Location = new Point(w, form.Location.Y);
                }
                if (form.Location.Y >= h)
                {
                    form.Location = new Point(form.Location.X, h);
                }
            }
        }