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; } }
private void RegisterMdiContainerFormEvents(MdiContainerForm mdiContainerForm) { mdiContainerForm.FormClosed += (sender, _) => { if (FindMdiContainer((MdiContainerForm)sender, out int index)) { mdiContainers.RemoveAt(index); if (mdiContainers.Count > 0) { AutoSaveMdiContainerList(); } else { // Close the entire process after the last form is closed. // When the application is reopened, the state of this last form is restored. Close(); } } }; mdiContainerForm.Activated += (sender, _) => { // Bring to front of list if activated. if (FindMdiContainer((MdiContainerForm)sender, out int index)) { var activatedFormWithState = mdiContainers[index]; mdiContainers.RemoveAt(index); mdiContainers.Insert(0, activatedFormWithState); AutoSaveMdiContainerList(); } }; }
private static void SetDefaultSizeAndPosition(MdiContainerForm mdiContainerForm) { // Show in the center of the monitor where the mouse currently is. var activeScreen = Screen.FromPoint(MousePosition); Rectangle workingArea = activeScreen.WorkingArea; // Two thirds the size of the active monitor's working area. workingArea.Inflate(-workingArea.Width / 6, -workingArea.Height / 6); // Update the bounds of the form. mdiContainerForm.SetBounds(workingArea.X, workingArea.Y, workingArea.Width, workingArea.Height, BoundsSpecified.All); }
private bool FindMdiContainer(MdiContainerForm form, out int index) { // Linear search. for (index = 0; index < mdiContainers.Count; index++) { if (mdiContainers[index].Form == form) { return(true); } } return(false); }
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); }
public MdiContainerWithState(MdiContainerForm form, MdiContainerState state) { Form = form; State = state; }