WINFORM学习手册——Mdi应用(文件管理菜单)

一、打开上一章的解决方案,将File菜单项改为下图样子:

image

二、双击每一菜单项,写入单击事件代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Mdi
{
    public partial class MainForm : Form
    {
        int Count = 0;//记录子窗体的数量
        Dictionary<string, string> FilePathDic = new Dictionary<string, string>();//记录窗体的保存路径
        public MainForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 创建子窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ChildForm childForm = new ChildForm();
            childForm.MdiParent = this;
            childForm.Text = "子窗体" + (++Count);
            childForm.Show();
        }
        /// <summary>
        /// 层叠布局
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.Cascade);
        }
        /// <summary>
        /// 横向布局            
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void theHorizontallyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileHorizontal);
        }
        /// <summary>
        /// 竖向布局
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void theVerticallyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileVertical);
        }
        /// <summary>
        /// 上一步
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void undoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild != null)
            {
                var childForm = (ChildForm)this.ActiveMdiChild;
                childForm.richTextBox1.Undo();
            }
        }
        /// <summary>
        /// 下一步
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void redoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild != null)
            {
                var childForm = (ChildForm)this.ActiveMdiChild;
                childForm.richTextBox1.Redo();
            }
        }
        /// <summary>
        /// 剪切
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild != null)
            {
                var childForm = (ChildForm)this.ActiveMdiChild;
                childForm.richTextBox1.Cut();
            }
        }
        /// <summary>
        /// 复制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild != null)
            {
                var childForm = (ChildForm)this.ActiveMdiChild;
                childForm.richTextBox1.Copy();
            }
        }
        /// <summary>
        /// 粘贴
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild != null)
            {
                var childForm = (ChildForm)this.ActiveMdiChild;
                childForm.richTextBox1.Paste();
            }
        }
        /// <summary>
        /// 全选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild != null)
            {
                var childForm = (ChildForm)this.ActiveMdiChild;
                childForm.richTextBox1.SelectAll();
            }
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild != null)
            {
                var childForm = (ChildForm)this.ActiveMdiChild;
                string title = childForm.Text;
                string path = string.Empty;
                //判断是否已经保存过
                if (FilePathDic.ContainsKey(title))
                {
                    path = FilePathDic[title];
                }
                if (string.IsNullOrEmpty(path))
                {
                    path = GetSavePath();
                }
                if (!string.IsNullOrEmpty(path))
                {
                    childForm.richTextBox1.SaveFile(path);
                    FilePathDic[title] = path;
                }

            }
        }
        /// <summary>
        /// 另存为
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild != null)
            {
                var childForm = (ChildForm)this.ActiveMdiChild;
                string title = childForm.Text;
                string path = GetSavePath();
                if (!string.IsNullOrEmpty(path))
                {
                    childForm.richTextBox1.SaveFile(path);
                    FilePathDic[title] = path;
                }

            }
        }
        /// <summary>
        /// 获取保存路径
        /// </summary>
        /// <returns></returns>
        private string GetSavePath()
        {
            string path = string.Empty;
            var dialog = new SaveFileDialog();
            dialog.Filter = "|*.rtf";
            dialog.AddExtension = true;
            var result = dialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                path = dialog.FileName;
            }
            return path;
        }
        /// <summary>
        /// 关闭当前页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild != null)
            {
                var childForm = (ChildForm)this.ActiveMdiChild;
                childForm.Close();
            }
        }
        /// <summary>
        /// 关闭主窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 打开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var dialog = new OpenFileDialog();
            dialog.Filter = "|*.rtf";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                var title = "子窗体" + (++Count);
                ChildForm childForm = new ChildForm();
                childForm.Text = title;
                childForm.MdiParent = this;
                childForm.richTextBox1.LoadFile(dialog.FileName);
                FilePathDic.Add(title, dialog.FileName);
                childForm.Show();
            }
        }


    }
}
posted @ 2014-01-07 21:12  争世不悔  阅读(447)  评论(0编辑  收藏  举报