private void AttachFormStateAutoSaver(MdiContainerWithState mdiContainerWithState) { PersistableFormState formState = mdiContainerWithState.State.FormState; formState.AttachTo(mdiContainerWithState.Form); formState.Changed += (_, __) => AutoSaveMdiContainerList(); }
private void RestoreMdiContainerState(MdiContainerWithState mdiContainerWithState) { MdiContainerForm mdiContainerForm = mdiContainerWithState.Form; PersistableFormState formState = mdiContainerWithState.State.FormState; bool boundsInitialized = false; Rectangle targetBounds = formState.Bounds; // If all bounds are known initialize from those. // Do make sure the window ends up on a visible working area. targetBounds.Intersect(Screen.GetWorkingArea(targetBounds)); if (targetBounds.Width >= mdiContainerForm.MinimumSize.Width && targetBounds.Height >= mdiContainerForm.MinimumSize.Height) { mdiContainerForm.SetBounds(targetBounds.Left, targetBounds.Top, targetBounds.Width, targetBounds.Height, BoundsSpecified.All); boundsInitialized = true; } // Determine a window state independently if no formState was applied successfully. if (!boundsInitialized) { SetDefaultSizeAndPosition(mdiContainerForm); } // Restore maximized setting after setting the Bounds. if (formState.Maximized) { mdiContainerForm.WindowState = FormWindowState.Maximized; } }
internal MdiContainerForm CreateNewMdiContainerForm() { var mdiContainerForm = new MdiContainerForm(); RegisterMdiContainerFormEvents(mdiContainerForm); var formWithDefaultState = new MdiContainerWithState(mdiContainerForm, new MdiContainerState(new PersistableFormState(false, Rectangle.Empty))); mdiContainers.Add(formWithDefaultState); mdiContainerForm.Load += (_, __) => { SetDefaultSizeAndPosition(mdiContainerForm); AttachFormStateAutoSaver(formWithDefaultState); }; return(mdiContainerForm); }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (!HasSession) { Close(); } else { #if DEBUG PieceImages.DeployRuntimePieceImageFiles(); #endif // Load chess piece images. PieceImages.LoadChessPieceImages(); Visible = false; if (Session.Current.TryGetAutoSaveValue(SettingKeys.Windows, out IEnumerable <PersistableFormState> states)) { // First restore all states, then attach auto-save events. // Create all forms before doing anything else. foreach (PersistableFormState formState in states) { mdiContainers.Add(new MdiContainerWithState(new MdiContainerForm(), new MdiContainerState(formState))); } // Activate from back to front. for (int i = mdiContainers.Count - 1; i >= 0; i--) { MdiContainerWithState formWithState = mdiContainers[i]; formWithState.Form.Load += (_, __) => RestoreMdiContainerState(formWithState); if (i > 0) { // Activate by activating the docked control. formWithState.Form.DockedControl.EnsureActivated(); } else { // Most recently activated window should open command line arguments. // Alternative is to open an additional new window, but this is inconsistent with ReceivedMessageFromAnotherInstance(). // TODO: maybe turn this into a preference? E.g. "open_files_in_new_window_on_launch". formWithState.Form.OpenCommandLineArgs(commandLineArgs); } } // Only now register auto-save events. foreach (MdiContainerWithState formWithState in mdiContainers) { RegisterMdiContainerFormEvents(formWithState.Form); AttachFormStateAutoSaver(formWithState); } } else { ShowNewMdiContainerForm(commandLineArgs); } } }