using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; namespace DocApp { public partial class MDIParentForm : Form { [DllImport("user32.dll")] static extern bool SetForegroundWindow(IntPtr hWnd); public MDIParentForm() { InitializeComponent(); } // For use by Main in processing a file passed via the command line public void OpenDocument(string fileName) { // Bring app to front to show result of document opening this.Activate(); SetForegroundWindow(this.Handle); // Is file already open? foreach( MDIChildForm openForm in this.MdiChildren ) { if( !string.IsNullOrEmpty(openForm.FileName) ) { if( openForm.FileName.ToLower() == fileName.ToLower() ) { openForm.Activate(); return; } } } // If file not open, let child do the opening MDIChildForm child = new MDIChildForm(); HookMDIChildFileDocument(child); if( !child.OpenDocument(fileName) ) { child.Close(); return; } // Show child child.MdiParent = this; child.Show(); } private void newToolStripMenuItem_Click(object sender, EventArgs e) { // Create a new instance of the child form. MDIChildForm child = new MDIChildForm(); HookMDIChildFileDocument(child); // Make it a child of this MDI form before showing it. child.MdiParent = this; child.Show(); } private void openToolStripMenuItem_Click(object sender, EventArgs e) { this.OpenDocument(null); } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { Application.Exit(); } private void ToolBarToolStripMenuItem_Click(object sender, EventArgs e) { toolStrip.Visible = toolBarToolStripMenuItem.Checked; } private void MDIChildFileDocument_ReadDocument(object sender, FileDocumentControlLibrary.SerializeDocumentEventArgs e) { // Add to MRU menu this.mruMenuManager.Add(e.Filename); } private void MDIChildFileDocument_WriteDocument(object sender, FileDocumentControlLibrary.SerializeDocumentEventArgs e) { // Add to MRU menu this.mruMenuManager.Add(e.Filename); // Let file document take care of saving e.Handled = false; } private void MDIChild_FormClosing(object sender, FormClosingEventArgs e) { UnhookMDIChildFileDocument((MDIChildForm)sender); } private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e) { statusStrip.Visible = statusBarToolStripMenuItem.Checked; } private void CascadeToolStripMenuItem_Click(object sender, EventArgs e) { LayoutMdi(MdiLayout.Cascade); } private void TileVerticleToolStripMenuItem_Click(object sender, EventArgs e) { LayoutMdi(MdiLayout.TileVertical); } private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e) { LayoutMdi(MdiLayout.TileHorizontal); } private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e) { LayoutMdi(MdiLayout.ArrangeIcons); } private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e) { foreach( Form childForm in MdiChildren ) { childForm.Close(); } } private void aboutToolStripMenuItem_Click(object sender, EventArgs e) { AboutBox dlg = new AboutBox(); dlg.Show(); } private void HookMDIChildFileDocument(MDIChildForm mdiChild) { mdiChild.FileDocument.ReadDocument += new FileDocumentControlLibrary.SerializeDocumentEventHandler(MDIChildFileDocument_ReadDocument); mdiChild.FileDocument.WriteDocument += new FileDocumentControlLibrary.SerializeDocumentEventHandler(MDIChildFileDocument_WriteDocument); mdiChild.FormClosing += new FormClosingEventHandler(MDIChild_FormClosing); } private void UnhookMDIChildFileDocument(MDIChildForm mdiChild) { mdiChild.FileDocument.ReadDocument -= new FileDocumentControlLibrary.SerializeDocumentEventHandler(MDIChildFileDocument_ReadDocument); mdiChild.FileDocument.WriteDocument -= new FileDocumentControlLibrary.SerializeDocumentEventHandler(MDIChildFileDocument_WriteDocument); mdiChild.FormClosing -= new FormClosingEventHandler(MDIChild_FormClosing); } private void mruMenuManager_MruMenuItemClick(object sender, MruControlLibrary.MruMenuItemClickEventArgs e) { this.OpenDocument(e.Filename); } private void mruMenuManager_MruMenuItemFileMissing(object sender, MruControlLibrary.MruMenuItemFileMissingEventArgs e) { DialogResult res = MessageBox.Show("Remove " + e.Filename + "?", Application.ProductName, MessageBoxButtons.YesNo); if( res == DialogResult.Yes ) e.RemoveFromMru = true; } private void contentsToolStripMenuItem_Click(object sender, EventArgs e) { ShowHelpContents(); } private void helpToolStripButton_Click(object sender, EventArgs e) { ShowHelpContents(); } private void ShowHelpContents() { Help.ShowHelp(this, "DocAppHelp.chm"); } private void dragAndDropFileComponent_FileDropped(object sender, DragAndDropFileControlLibrary.FileDroppedEventArgs e) { // Process each file foreach( string filename in e.Filenames ) { // Only open files with the appropriate extension string extension = Path.GetExtension(filename); if( extension == ".dxt" ) { OpenDocument(filename); } else { MessageBox.Show("Can't open files of type " + extension); } } } } }