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

📄 mainform.cs

📁 在CSharpNotepad中可以创建和编辑简单文本文档
💻 CS
📖 第 1 页 / 共 3 页
字号:
            this.MdiChildren[this.CurrentFormIndexOf()].Activate(); //激活该子窗口
            this.CheckActiveMdiChild();         //设置控件的可见性
        }

        #endregion


        #region 主窗口标题栏事件处理的方法

        #region 文件--文件操作

        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            newfileForm nf = new newfileForm(this);
            nf.ShowDialog(this);

            if (this.newfileType != "null")
            {
                // 创建一个子窗口对象,用来实现新建一个文档
                childForm childForm = new childForm(this);

                childForm.fileType = this.newfileType;
                childForm.fileName = "文档" + (++count).ToString() + childForm.fileType;
                childForm.isNewDoc = true;

                childForm.Name = childForm.fileName;
                childForm.Text = childForm.fileName;
                childForm.MdiParent = this;
                childForm.Show();
                if (childForm.fileType == ".rtf")
                    childForm.Icon = Properties.Resources.RtfIcon;
                else
                    childForm.Icon = Properties.Resources.TxtIcon;

                this.currentForm = childForm;       //设置当前子窗口为活动窗口
                this.NewTabPage();                  //新建标签页
                this.MdiChildren[this.CurrentFormIndexOf()].Activate(); //激活该子窗口
                this.CheckActiveMdiChild();         //设置控件的可见性
            }
        }
        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // 弹出打开文件的对话框
            this.openFileDialog1.Filter = "RTF 文档(*.rtf)|*.rtf|文本文档(*.txt)|*.txt|所有文件(*.*)|*.*";
            this.openFileDialog1.FileName = null;

            if (this.openFileDialog1.ShowDialog() == DialogResult.OK && this.openFileDialog1.FileName.Length > 0)
            {
                // 创建一个子窗口对象,用来实现打开一个文档,并设置该子窗口为活动窗口
                childForm childForm = new childForm(this);

                childForm.filePath = openFileDialog1.FileName;
                FileInfo finfo = new FileInfo(childForm.filePath);

                childForm.fileName = finfo.Name;
                childForm.fileType = finfo.Extension;
                childForm.isNewDoc = false;

                switch (finfo.Extension)
                {
                    case ".rtf":
                        childForm.richTextBox1.LoadFile(childForm.filePath, RichTextBoxStreamType.RichText);
                        childForm.Icon = Properties.Resources.RtfIcon;
                        break;
                    case ".txt":
                        childForm.richTextBox1.LoadFile(childForm.filePath, RichTextBoxStreamType.PlainText);
                        childForm.Icon = Properties.Resources.TxtIcon;
                        break;
                    default:
                        MessageBox.Show("不能打开此格式的文件,请检查后再试!", "Notepad", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                }

                childForm.Name = childForm.fileName;
                childForm.Text = childForm.fileName;
                childForm.MdiParent = this;
                childForm.Show();

                this.currentForm = childForm;       //设置当前子窗口为活动窗口
                this.NewTabPage(childForm.fileName);//新建标签页
                this.MdiChildren[this.CurrentFormIndexOf()].Activate(); //激活该子窗口
                this.CheckActiveMdiChild();         //设置控件的可见性
            }
        }
        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //保存文件
            if (this.MdiChildren.Length == 0)
                return;
            if (this.currentForm.isNewDoc == true || this.currentForm.filePath == "")
            {
                this.另存为ToolStripMenuItem.PerformClick();
            }
            else
            {
                if (this.currentForm.fileType == ".rtf")
                {
                    this.currentForm.richTextBox1.SaveFile(this.currentForm.filePath, RichTextBoxStreamType.RichText);
                    this.currentForm.Icon = Properties.Resources.RtfIcon;
                }
                if (this.currentForm.fileType == ".txt")
                {
                    this.currentForm.richTextBox1.SaveFile(this.currentForm.filePath, RichTextBoxStreamType.PlainText);
                    this.currentForm.Icon = Properties.Resources.TxtIcon;
                }
                this.currentForm.Text = this.currentForm.fileName.Replace("*", "");//去除标志未保存的*号
            }
        }
        private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.MdiChildren.Length == 0)
                return;

            // 弹出保存文件的对话框
            this.saveFileDialog1.Filter = "RTF 文档(*.rtf)|*.rtf|文本文档(*.txt)|*.txt|所有文件(*.*)|*.*";
            this.saveFileDialog1.FileName = null;

            if (this.saveFileDialog1.ShowDialog() == DialogResult.OK && this.saveFileDialog1.FileName.Length > 0)
            {
                this.currentForm.filePath = saveFileDialog1.FileName;
                FileInfo finfo = new FileInfo(this.currentForm.filePath);

                this.currentForm.fileName = finfo.Name;
                this.currentForm.fileType = finfo.Extension;
                this.currentForm.isNewDoc = false;

                switch (finfo.Extension)
                {
                    case ".rtf":
                        this.currentForm.richTextBox1.SaveFile(this.currentForm.filePath, RichTextBoxStreamType.RichText);
                        this.currentForm.Icon = Properties.Resources.RtfIcon;
                        break;
                    case ".txt":
                        this.currentForm.richTextBox1.SaveFile(this.currentForm.filePath, RichTextBoxStreamType.PlainText);
                        this.currentForm.Icon = Properties.Resources.TxtIcon;
                        break;
                    default:
                        MessageBox.Show("不能保存为此格式的文件!", "Notepad", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                }

                this.currentForm.Name = this.currentForm.fileName;
                this.currentForm.Text = this.currentForm.fileName;

                this.ReSetTabPageName(this.CurrentFormIndexOf(), this.currentForm.fileName); //重新设置标签的名称
            } 
        }

        private void 打印设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.MdiChildren.Length == 0)
                return;
            this.printDialog1.Document = this.printDocument1;
            this.printDialog1.ShowDialog();
        }
        private void 打印预览ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.MdiChildren.Length == 0)
                return;
            //设置Document属性
            this.printPreviewDialog1.Document = this.printDocument1;
            try
            {
                //显示打印预览窗口
                this.printPreviewDialog1.ShowDialog();
            }
            catch (Exception excep)
            {
                //显示打印出错消息
                MessageBox.Show(excep.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.MdiChildren.Length == 0)
                return;

            this.printDialog1.Document = this.printDocument1;

            StringReader reader = new StringReader(this.currentForm.richTextBox1.Text);

            if (this.printDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    this.printDocument1.Print();
                }
                catch (Exception excep)
                {
                    MessageBox.Show(excep.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.printDocument1.PrintController.OnEndPrint(this.printDocument1, new PrintEventArgs());
                }
            }
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //this.MainForm_FormClosing(sender, e);
        }

        #endregion

        #region 编辑--文本内容

        private void 撤消ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.MdiChildren.Length == 0)
                return;
            currentForm.Undo();
        }
        private void 恢复ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.MdiChildren.Length == 0)
                return;
            currentForm.Redo();
        }
        private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.MdiChildren.Length == 0)
                return;
            currentForm.Cut();
        }
        private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.MdiChildren.Length == 0)
                return;
            currentForm.Copy();
        }
        private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.MdiChildren.Length == 0)
                return;
            currentForm.Paste();
        }
        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.MdiChildren.Length == 0)
                return;
            currentForm.Delete();
        }
        private void 查找ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.MdiChildren.Length == 0)
                return;
            searchAndreplaceForm sp = new searchAndreplaceForm(this, "tabPageSearch");
            sp.ShowDialog(this);
        }
        private void 替换ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.MdiChildren.Length == 0)
                return;
            searchAndreplaceForm sp = new searchAndreplaceForm(this, "tabPageReplace");
            sp.ShowDialog(this);
        }
        private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.MdiChildren.Length == 0)
                return;
            currentForm.SelectAll();
        }
        private void 时间和日期ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.MdiChildren.Length == 0)
                return;
            datetimeForm dt = new datetimeForm(this);
            dt.ShowDialog(this);
        }

        #endregion

        #region 格式--设置文本格式

        private void 自动换行AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.MdiChildren.Length == 0)
                return;
            if (自动换行AToolStripMenuItem.Checked)
            {
                this.currentForm.richTextBox1.WordWrap = false;
                自动换行AToolStripMenuItem.Checked = false;
            }
            else
            {
                this.currentForm.richTextBox1.WordWrap = true;
                自动换行AToolStripMenuItem.Checked = true;
            }
            this.currentForm.richTextBox1_Click(sender, e);
        }

        private void fontDialog1_Apply(object sender, EventArgs e) //“应用”按钮事件
        {
            FontDialog fontDialog1 = (FontDialog)sender;
            this.currentForm.richTextBox1.SelectionFont = this.fontDialog1.Font;
        }
        private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.MdiChildren.Length == 0)
                return;
            this.fontDialog1.Apply += new EventHandler(this.fontDialog1_Apply);
            if (this.fontDialog1.ShowDialog() == DialogResult.OK)
            {
                this.currentForm.richTextBox1.SelectionFont = this.fontDialog1.Font;
            }
        }

        private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.MdiChildren.Length == 0)
                return;

            if (this.colorDialog1.ShowDialog() == DialogResult.OK)
            {
                this.currentForm.richTextBox1.SelectionColor = this.colorDialog1.Color;
            }
        }

        private void 项目符号样式ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (项目符号样式ToolStripMenuItem.Checked == true)
            {
                this.currentForm.richTextBox1.SelectionBullet = false;
                项目符号样式ToolStripMenuItem.Checked = false;
                tsbProject.Checked = false;
            }
            else
            {
                this.currentForm.richTextBox1.SelectionBullet = true;
                项目符号样式ToolStripMenuItem.Checked = true;
                tsbProject.Checked = true;
            }
        }

        private void 段落ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            paragraphForm pf = new paragraphForm(this);
            pf.ShowDialog(this);
        }

        private void 左对齐ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.MdiChildren.Length == 0)
                return;
            this.currentForm.SetLeftAlign();
            左对齐ToolStripMenuItem.Checked = true;
            居中ToolStripMenuItem.Checked = false;
            右对齐ToolStripMenuItem.Checked = false;
        }

        private void 居中ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.MdiChildren.Length == 0)
                return;
            this.currentForm.SetMiddleAlign();
            左对齐ToolStripMenuItem.Checked = false;
            居中ToolStripMenuItem.Checked = true;
            右对齐ToolStripMenuItem.Checked = false;
        }

        private void 右对齐ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.MdiChildren.Length == 0)
                return;
            this.currentForm.SetRightAlign();
            左对齐ToolStripMenuItem.Checked = false;
            居中ToolStripMenuItem.Checked = false;
            右对齐ToolStripMenuItem.Checked = true;
        }

        #region 填充显示比例数据

        private void GetDisplayRatio()
        {
            int[] displayRation = new int[] { 10, 25, 50, 75, 100, 150, 200, 500 };
            for (int i = 0; i < displayRation.Length; i++)
                tscbDisplayRatio.Items.Add(displayRation[i].ToString() + "%");
        }

        private void 显示比例ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            displayratioForm dr = new displayratioForm(this);
            dr.ShowDialog(this);

⌨️ 快捷键说明

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