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 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); }