//===================================================================== /// <summary> /// Get the project explorer window /// </summary> /// <returns>The project explorer window or null if not found</returns> private ProjectExplorerWindow FindProjectExplorerWindow() { ProjectExplorerWindow projectExplorer = null; foreach (IDockContent content in this.DockPanel.Contents) { projectExplorer = content as ProjectExplorerWindow; if (projectExplorer != null) { break; } } return(projectExplorer); }
//===================================================================== /// <summary> /// Load the last used project on startup /// </summary> /// <param name="sender">The sender of the event</param> /// <param name="e">The event arguments</param> private void MainForm_Load(object sender, EventArgs e) { StringCollection mruList = Settings.Default.MruList; string state; int idx = 0; projectExplorer = new ProjectExplorerWindow(); projectProperties = new ProjectPropertiesWindow(); // Set the current directory to My Documents so that people don't save stuff in the SHFB folder by // default. try { Directory.SetCurrentDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)); } catch { // This doesn't always work if they use a mapped network drive for My Documents. In that case, // just ignore the failure and stay put. } // If not starting out minimized, attempt to load the last used window size and position if(this.WindowState != FormWindowState.Minimized) { WINDOWPLACEMENT wp = (WINDOWPLACEMENT)Settings.Default.WindowPlacement; if(wp.length == Marshal.SizeOf(typeof(WINDOWPLACEMENT))) { wp.flags = 0; if(wp.showCmd == UnsafeNativeMethods.SW_SHOWMINIMIZED) wp.showCmd = UnsafeNativeMethods.SW_SHOWNORMAL; UnsafeNativeMethods.SetWindowPlacement(this.Handle, ref wp); } } // Restore default content state if present and shift isn't held down state = Settings.Default.ContentEditorDockState; if(!String.IsNullOrEmpty(state) && (Control.ModifierKeys & Keys.Shift) == 0) { byte[] stateBytes = Encoding.UTF8.GetBytes(state.ToCharArray()); using(MemoryStream ms = new MemoryStream(stateBytes)) { dockPanel.LoadFromXml(ms, DeserializeState); } } // If not restored or something when wrong and the project explorer doesn't have a dock panel, show // them by default. if(projectExplorer.DockPanel == null) { projectExplorer.Show(dockPanel); projectProperties.Show(dockPanel); projectExplorer.Focus(); } try { // Check for the SHFBROOT environment variable. It may not be present yet if a reboot hasn't // occurred after installation. In such cases, set it to the proper folder for this process so // that projects can be loaded and built. if(Environment.GetEnvironmentVariable("SHFBROOT") == null) { string shfbRootPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); MessageBox.Show("The SHFBROOT system environment variable was not found. This variable " + "is usually created during installation and may require a reboot. It has been defined " + "temporarily for this process as:\n\nSHFBROOT=" + shfbRootPath, Constants.AppName, MessageBoxButtons.OK, MessageBoxIcon.Information); Environment.SetEnvironmentVariable("SHFBROOT", shfbRootPath); } } catch(Exception ex) { MessageBox.Show("Unable to detect or set SHFBROOT environment variable. Reason: " + ex.Message, Constants.AppName, MessageBoxButtons.OK, MessageBoxIcon.Error); } // Create the MRU on first use if(mruList == null) { Settings.Default.MruList = new StringCollection(); this.CloseProject(); } else { // Get rid of projects that no longer exist or that are not compatible while(idx < mruList.Count) if(!File.Exists(mruList[idx]) || mruList[idx].EndsWith(".shfb", StringComparison.OrdinalIgnoreCase)) mruList.RemoveAt(idx); else idx++; if(mruList.Count == 0) this.CloseProject(); else try { this.CreateProject(mruList[0], true); } catch(InvalidProjectFileException pex) { System.Diagnostics.Debug.Write(pex); MessageBox.Show("The project file format is invalid: " + pex.Message, Constants.AppName, MessageBoxButtons.OK, MessageBoxIcon.Error); } catch(Exception ex) { System.Diagnostics.Debug.Write(ex); MessageBox.Show(ex.Message, Constants.AppName, MessageBoxButtons.OK, MessageBoxIcon.Error); } } }