⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mdiparentform.cs

📁 中文名:Windows Forms 程序设计 英文名:Windows Forms Programming in c# 作者: Chris Sells 翻译: 荣耀 蒋贤哲 出版社:人民
💻 CS
字号:
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);
        }
      }
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -