/// <summary> /// Save changes on exit if necessary /// </summary> /// <param name="sender">The sender of the event</param> /// <param name="e">The event arguments</param> private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { BaseContentEditor content; string state; if(cancellationTokenSource != null) { if(cancellationTokenSource.IsCancellationRequested) return; if(MessageBox.Show("A build is currently taking place. Do you want to abort it and exit?", Constants.AppName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { e.Cancel = true; return; } miCancelBuild_Click(sender, e); return; } if(!projectExplorer.AskToSaveProject()) e.Cancel = true; else foreach(IDockContent dockContent in dockPanel.Contents) { content = dockContent as BaseContentEditor; if(content != null && !content.CanClose) { e.Cancel = true; break; } } if(!e.Cancel) { // Save the current window size and position if possible WINDOWPLACEMENT wp = new WINDOWPLACEMENT(); wp.length = Marshal.SizeOf(typeof(WINDOWPLACEMENT)); UnsafeNativeMethods.GetWindowPlacement(this.Handle, out wp); Settings.Default.WindowPlacement = wp; // Save the content state. If open, hide some of the windows // so that they default to hidden when re-opened. if(outputWindow != null && outputWindow.Visible) outputWindow.Hide(); if(entityReferencesWindow != null && entityReferencesWindow.Visible) entityReferencesWindow.Hide(); // Dispose of the preview window to get rid of its temporary project and build files if(previewWindow != null) { previewWindow.Dispose(); previewWindow = null; } // Save the default window state using(MemoryStream ms = new MemoryStream()) { dockPanel.SaveAsXml(ms, Encoding.UTF8); state = Encoding.UTF8.GetString(ms.ToArray()); Settings.Default.ContentEditorDockState = state; } Settings.Default.Save(); this.KillWebServer(); // Save per user project state if wanted if(projectExplorer.CurrentProject != null && Settings.Default.PerUserProjectState) { // If hidden, unhide it so that DockPanel can write to it. We'll hide it again afterwards. bool isHidden = (File.Exists(projectExplorer.CurrentProject.Filename + WindowStateSuffix) && (File.GetAttributes(projectExplorer.CurrentProject.Filename + WindowStateSuffix) & FileAttributes.Hidden) != 0); try { Cursor.Current = Cursors.WaitCursor; if(isHidden) File.SetAttributes(projectExplorer.CurrentProject.Filename + WindowStateSuffix, FileAttributes.Normal); dockPanel.SaveAsXml(projectExplorer.CurrentProject.Filename + WindowStateSuffix); if(isHidden) File.SetAttributes(projectExplorer.CurrentProject.Filename + WindowStateSuffix, FileAttributes.Hidden); } finally { Cursor.Current = Cursors.Default; } } } }
internal static extern bool GetWindowPlacement(IntPtr hWnd, out WINDOWPLACEMENT placement);
internal static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref WINDOWPLACEMENT placement);
/// <summary> /// Save changes on exit if necessary /// </summary> /// <param name="sender">The sender of the event</param> /// <param name="e">The event arguments</param> private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { BaseContentEditor content; string state; if(buildThread != null && buildThread.IsAlive) { if(MessageBox.Show("A build is currently taking place. Do " + "you want to abort it and exit?", Constants.AppName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { e.Cancel = true; return; } miCancelBuild_Click(sender, e); } if(!projectExplorer.AskToSaveProject()) e.Cancel = true; else foreach(IDockContent dockContent in dockPanel.Contents) { content = dockContent as BaseContentEditor; if(content != null && !content.CanClose) { e.Cancel = true; break; } } if(!e.Cancel && previewWindow != null) e.Cancel = !previewWindow.CanClose; if(!e.Cancel) { // Save the current window size and position if possible WINDOWPLACEMENT wp = new WINDOWPLACEMENT(); wp.length = Marshal.SizeOf(typeof(WINDOWPLACEMENT)); UnsafeNativeMethods.GetWindowPlacement(this.Handle, out wp); Settings.Default.WindowPlacement = wp; // Save the content state. If open, hide some of the windows // so that they default to hidden when re-opened. if(outputWindow != null && outputWindow.Visible) outputWindow.Hide(); if(entityReferencesWindow != null && entityReferencesWindow.Visible) entityReferencesWindow.Hide(); if(previewWindow != null && previewWindow.Visible) previewWindow.Hide(); using(MemoryStream ms = new MemoryStream()) { dockPanel.SaveAsXml(ms, Encoding.UTF8); state = Encoding.UTF8.GetString(ms.ToArray()); Settings.Default.ContentEditorDockState = state; } // Dispose of the preview window to get rid of its temporary // project and build files. if(previewWindow != null) { previewWindow.Close(); previewWindow = null; } Settings.Default.Save(); this.KillWebServer(); } }