#region Using directives using System; using System.Collections.ObjectModel; using System.Windows.Forms; using Microsoft.VisualBasic.ApplicationServices; #endregion namespace MultiSDISample { public class MultiSDIApplication : WindowsFormsApplicationBase { private static MultiSDIApplication application; internal static MultiSDIApplication Application { get { if( application == null ) { application = new MultiSDIApplication(); } return application; } } public MultiSDIApplication() { // This ensures the underlying single-SDI framework is employed, // and OnStartupNextInstance is fired this.IsSingleInstance = true; // Needed for multi-SDI since no form is the main form this.ShutdownStyle = ShutdownMode.AfterAllFormsClose; } // Create first top-level form protected override void OnCreateMainForm() { this.MainForm = this.CreateTopLevelWindow(this.CommandLineArgs); } // Create subsequent top-level form protected override void OnStartupNextInstance(StartupNextInstanceEventArgs e) { this.CreateTopLevelWindow(e.CommandLine); } private TopLevelForm CreateTopLevelWindow(ReadOnlyCollection args) { // Get file name, if provided string fileName = (args.Count > 0 ? args[0] : null); // Create a new top-level form return TopLevelForm.CreateTopLevelWindow(fileName); } #region Menu management - was in MultiSDIApplicationContext file public void AddTopLevelForm(Form form) { // Add form to collection of forms and // watch for it to activate and close form.Activated += Form_Activated; form.FormClosed += Form_FormClosed; // Set initial top level form to activate if( this.OpenForms.Count == 1 ) this.MainForm = form; } public void AddWindowMenu(ToolStripMenuItem windowMenu) { // Handle tool strip menu item's drop down opening event windowMenu.DropDownOpening += windowMenu_DropDownOpening; } void Form_Activated(object sender, EventArgs e) { // Whichever form activated last is the current top-level form this.MainForm = (Form)sender; } void Form_FormClosed(object sender, FormClosedEventArgs e) { // Set a new "main" if necessary Form form = (Form)sender; if( (form == this.MainForm) && (this.OpenForms.Count > 0) ) { this.MainForm = (Form)this.OpenForms[0]; } form.Activated -= Form_Activated; form.FormClosed -= Form_FormClosed; } void windowMenu_DropDownOpening(object sender, EventArgs e) { ToolStripMenuItem menu = (ToolStripMenuItem)sender; // Clear current menu if( menu.DropDownItems.Count > 0 ) { menu.DropDown.Dispose(); } menu.DropDown = new ToolStripDropDown(); // Populate menu with one item for each open top-level form foreach( Form form in this.OpenForms ) { ToolStripMenuItem item = new ToolStripMenuItem(); item.Text = form.Text; item.Tag = form; menu.DropDownItems.Add(item); item.Click += WindowMenuItem_Click; // Check currently active window if( form == this.MainForm ) item.Checked = true; } } void WindowMenuItem_Click(object sender, EventArgs e) { // Activate top-level form based on selection ((Form)((ToolStripMenuItem)sender).Tag).Activate(); } #endregion } }