#region Using directives using System; using System.Collections.ObjectModel; using System.Windows.Forms; using Microsoft.VisualBasic.ApplicationServices; #endregion namespace DocApp { public class SingleMDIApplication : WindowsFormsApplicationBase { internal static SingleMDIApplication Application = new SingleMDIApplication(); // Must call base constructor to ensure correct initial // WindowsFormsApplicationBase configuration public SingleMDIApplication() : base() { // This ensures the underlying single-SDI framework is employed, // and OnStartupNextInstance is fired this.IsSingleInstance = true; } // Load MDI parent form and first MDI child form protected override void OnCreateMainForm() { this.MainForm = new MDIParentForm(); this.OpenDocument(this.CommandLineArgs); } // Load subsequent MDI child form protected override void OnStartupNextInstance(StartupNextInstanceEventArgs e) { this.OpenDocument(e.CommandLine); } private void OpenDocument(ReadOnlyCollection args) { // Get file name, if provided string fileName = (args.Count > 0 ? args[0] : null); if( string.IsNullOrEmpty(fileName) ) return; // Ask MDI parent to create a new MDI child and open // file, if one was passed ((MDIParentForm)this.MainForm).OpenDocument(fileName); } } }