publicpartialclassMainForm:Form{publicMainForm(){InitializeComponent();// this is the defaultthis.WindowState=FormWindowState.Normal;this.StartPosition=FormStartPosition.WindowsDefaultBounds;// check if the saved bounds are nonzero and visible on any screenif(Settings.Default.WindowPosition!=Rectangle.Empty&&IsVisibleOnAnyScreen(Settings.Default.WindowPosition)){// first set the boundsthis.StartPosition=FormStartPosition.Manual;this.DesktopBounds=Settings.Default.WindowPosition;// afterwards set the window state to the saved value (which could be Maximized)this.WindowState=Settings.Default.WindowState;}else{// this resets the upper left corner of the window to windows standardsthis.StartPosition=FormStartPosition.WindowsDefaultLocation;// we can still apply the saved sizethis.Size=Settings.Default.WindowPosition.Size;}}private bool IsVisibleOnAnyScreen(Rectangle rect){foreach(Screen screen inScreen.AllScreens){if(screen.WorkingArea.IntersectsWith(rect)){returntrue;}}returnfalse;}protectedoverridevoidOnClosed(EventArgs e){base.OnClosed(e);// only save the WindowState if Normal or Maximizedswitch(this.WindowState){caseFormWindowState.Normal:caseFormWindowState.Maximized:Settings.Default.WindowState=this.WindowState;break;default:Settings.Default.WindowState=FormWindowState.Normal;break;}// reset window state to normal to get the correct bounds// also make the form invisible to prevent distracting the userthis.Visible=false;this.WindowState=FormWindowState.Normal;Settings.Default.WindowPosition=this.DesktopBounds;Settings.Default.Save();}}