[可视化编程]窗体和控件综合设计

效果展示

主界面

在这里插入图片描述

文件操作

在这里插入图片描述

编辑操作

在这里插入图片描述

格式操作

在这里插入图片描述

查看操作

在这里插入图片描述

帮助操作

在这里插入图片描述

源码

FormBack.cs

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 IntergratedDesign
{
    
    
    public partial class FormBack : Form
    {
    
    
        public FormBack()
        {
    
    
            InitializeComponent();
        }

        int documentCount = 1;   //文档计数

        public FormRichText formRichText;   //定义编辑窗体变量

        //
        //按钮触发事件
        //

        //新建
        private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
        {
    
    
            NewFile();   //方法调用
        }

        //打开
        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        {
    
    
            OpenFile();   //方法调用
        }

        //保存
        private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
        {
    
    
            if (documentCount != 1)
                Save();   //方法调用
        }

        //另存为
        private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)
        {
    
    
            SaveAs();   //方法调用
        }

        //退出
        private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
        {
    
    
            Close();
        }

        //撤销
        private void 撤销ZToolStripMenuItem_Click(object sender, EventArgs e)
        {
    
    
            FormRichText formRichText = (FormRichText)this.ActiveMdiChild;
            formRichText.richTextBox1.Undo();
        }

        //剪切
        private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
        {
    
    
            FormRichText formRichText = (FormRichText)this.ActiveMdiChild;
            formRichText.richTextBox1.Cut();
        }

        //复制
        private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
        {
    
    
            FormRichText formRichText = (FormRichText)this.ActiveMdiChild;
            formRichText.richTextBox1.Copy();
        }

        //粘贴
        private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
        {
    
    
            FormRichText formRichText = (FormRichText)this.ActiveMdiChild;
            formRichText.richTextBox1.Paste();
        }

        //删除
        private void 删除LToolStripMenuItem_Click(object sender, EventArgs e)
        {
    
    
            FormRichText formRichText = (FormRichText)this.ActiveMdiChild;
            if(formRichText.richTextBox1.SelectedText.Length > 0)
            {
    
    
                formRichText.richTextBox1.SelectedText = "";
            }
        }

        //查找
        private void 查找FToolStripMenuItem_Click(object sender, EventArgs e)
        {
    
    
            formRichText = (FormRichText)this.ActiveMdiChild;
            FormFind formFind = new FormFind(formRichText);
            formFind.Show();
        }

        //替换
        private void 替换RToolStripMenuItem_Click(object sender, EventArgs e)
        {
    
    
            formRichText = (FormRichText)this.ActiveMdiChild;
            FormFind formFind = new FormFind(formRichText);
            formFind.Show();
        }

        //全选
        private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
        {
    
    
            FormRichText formRichText = (FormRichText)this.ActiveMdiChild;
            formRichText.richTextBox1.SelectAll();
        }

        //字体设置
        private void 字体FToolStripMenuItem_Click(object sender, EventArgs e)
        {
    
    
            formRichText = (FormRichText)this.ActiveMdiChild;
            FontDialog fontDialog = new FontDialog();

            if (fontDialog.ShowDialog() != DialogResult.Cancel)
            {
    
    
                formRichText.richTextBox1.SelectionFont = fontDialog.Font;   //将当前选定的文件改变字体
            }
        }

        //字体颜色
        private void 字体颜色CToolStripMenuItem_Click(object sender, EventArgs e)
        {
    
    
            formRichText = (FormRichText)this.ActiveMdiChild;
            ColorDialog colorDialog = new ColorDialog();
            if (colorDialog.ShowDialog() != DialogResult.Cancel)
            {
    
    
                formRichText.richTextBox1.SelectionColor = colorDialog.Color;//将当前选定的文字改变字体
            }
        }

        //层叠
        private void 层叠ToolStripMenuItem_Click(object sender, EventArgs e)
        {
    
    
            this.LayoutMdi(MdiLayout.Cascade);
        }

        //水平平铺
        private void 水平平铺ToolStripMenuItem_Click(object sender, EventArgs e)
        {
    
    
            this.LayoutMdi(MdiLayout.TileHorizontal);
        }

        //垂直平铺
        private void 垂直平铺ToolStripMenuItem_Click(object sender, EventArgs e)
        {
    
    
            this.LayoutMdi(MdiLayout.TileVertical);
        }

        //帮助
        private void 帮助HToolStripMenuItem_Click(object sender, EventArgs e)
        {
    
    
            MessageBox.Show("开发人:Spring_Bear\n开发时间:" + DateTime.Now.ToString("yyyy-MM-dd "), "帮助");
        }

        //
        //方法定义
        //

        //新建文档
        private void NewFile()
        {
    
    
            formRichText = new FormRichText();   //创建编辑窗体的对象
            formRichText.MdiParent = this;   //设置子窗体的父文档
            formRichText.Text = "新建文档" + documentCount++;   //文档号
            formRichText.Show();   //显示子窗体
            formRichText.isSaved = false;
        }

        //打开文件

        private void OpenFile()
        {
    
    
            string str;

            formRichText = new FormRichText();  //创建编辑窗体的对象
            formRichText.MdiParent = this;    //设置子窗体的父文档

            //设置打开文件对话框要打开的文件类型的初始值
            openFileDialog1.Filter = "文本文件(*.txt)|*.txt|富文本  (*.rtf)|*.rtf|全部文件 (*.*|*.*";   //设置对话框要打开的文件类型

            if(openFileDialog1.ShowDialog() != DialogResult.Cancel)
            {
    
    
                str = openFileDialog1.FileName;   //读打开文件的文件名
                formRichText.Text = str;   //显示窗体标题为打开文件文件名
                MessageBox.Show(str + "打开成功!");

                formRichText.Show();   //显示子窗体
                formRichText.richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.PlainText);
                //通过富文本框的LoadFile()方法,将打开的文件内容读出并显示在富文本框中
                formRichText.isSaved = true;
            }
        }

        //保存文件
        private void Save()
        {
    
    
            String str;
            FormRichText formRichText = (FormRichText)this.ActiveMdiChild;

            //如果文档未曾修改,直接返回
            if(!formRichText.isChanged)
            {
    
    
                MessageBox.Show("文本内容未修改!");
                return;
            }

            //如果文档保存过(也就是不需要另存为)
            if(formRichText.isSaved)
            {
    
    
                formRichText = (FormRichText)this.ActiveMdiChild;   //获取当前窗口

                str = formRichText.Text.Trim();
                formRichText.richTextBox1.SaveFile(str, RichTextBoxStreamType.PlainText);
                formRichText.isChanged = false;
            }
            else   //否则,另存为
            {
    
    
                SaveAs();
            }
        }

        //另存为
        private void SaveAs()
        {
    
    
            string str;
            formRichText = (FormRichText)this.ActiveMdiChild;   //获取当前窗口

            saveFileDialog1.DefaultExt = "txt";
            saveFileDialog1.Filter = "文本文件(*.txt)|*.txt|全部文件(*.*)|*.*";

            if(saveFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
    
    
                str = saveFileDialog1.FileName;

                MessageBox.Show(str);

                if(str!="")
                {
    
    
                    formRichText.richTextBox1.SaveFile(str, RichTextBoxStreamType.PlainText);
                    formRichText.isSaved = true;
                    formRichText.isChanged = false;
                }
                else
                {
    
    
                    MessageBox.Show("请输入文件名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }

        private void FormBack_Load(object sender, EventArgs e)
        {
    
    
            this.timer1.Interval = 1000;
            this.timer1.Start();
        }

        private void timer1_Tick_1(object sender, EventArgs e)
        {
    
    
            this.toolStripStatusLabel1.Text = "系统当前时间:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
        }

        private void 时间日期DToolStripMenuItem_Click(object sender, EventArgs e)
        {
    
    
            MessageBox.Show("系统当前时间为:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), "实时时间");
        }
    }
}

FormBack.Designer.cs

namespace IntergratedDesign
{
    
    
    partial class FormBack
    {
    
    
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
    
    
            if (disposing && (components != null))
            {
    
    
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
    
    
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormBack));
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.文件NToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.新建NToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.打开OToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.保存SToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.另存为AToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
            this.打印PToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator();
            this.退出XToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.编辑EToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.撤销ZToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator4 = new System.Windows.Forms.ToolStripSeparator();
            this.剪切TToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.复制CToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.粘贴PToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.删除LToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator5 = new System.Windows.Forms.ToolStripSeparator();
            this.查找FToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.替换RToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator6 = new System.Windows.Forms.ToolStripSeparator();
            this.全选AToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.时间日期DToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.格式OToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.自动换行WToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.字体FToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.字体颜色CToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.查看VToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.层叠ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.水平平铺ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.垂直平铺ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.帮助HToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
            this.fontDialog1 = new System.Windows.Forms.FontDialog();
            this.colorDialog1 = new System.Windows.Forms.ColorDialog();
            this.printDocument1 = new System.Drawing.Printing.PrintDocument();
            this.printPreviewDialog1 = new System.Windows.Forms.PrintPreviewDialog();
            this.statusStrip1 = new System.Windows.Forms.StatusStrip();
            this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
            this.menuStrip1.SuspendLayout();
            this.statusStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // menuStrip1
            // 
            this.menuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20);
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
    
    
            this.文件NToolStripMenuItem,
            this.编辑EToolStripMenuItem,
            this.格式OToolStripMenuItem,
            this.查看VToolStripMenuItem,
            this.帮助HToolStripMenuItem});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(648, 28);
            this.menuStrip1.TabIndex = 1;
            this.menuStrip1.Text = "menuStrip1";
            // 
            // 文件NToolStripMenuItem
            // 
            this.文件NToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
    
    
            this.新建NToolStripMenuItem,
            this.打开OToolStripMenuItem,
            this.保存SToolStripMenuItem,
            this.另存为AToolStripMenuItem,
            this.toolStripSeparator1,
            this.打印PToolStripMenuItem,
            this.toolStripSeparator3,
            this.退出XToolStripMenuItem});
            this.文件NToolStripMenuItem.Name = "文件NToolStripMenuItem";
            this.文件NToolStripMenuItem.Size = new System.Drawing.Size(73, 24);
            this.文件NToolStripMenuItem.Text = "文件(&N)";
            // 
            // 新建NToolStripMenuItem
            // 
            this.新建NToolStripMenuItem.Name = "新建NToolStripMenuItem";
            this.新建NToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.N)));
            this.新建NToolStripMenuItem.Size = new System.Drawing.Size(194, 26);
            this.新建NToolStripMenuItem.Text = "新建(&N)";
            this.新建NToolStripMenuItem.Click += new System.EventHandler(this.新建NToolStripMenuItem_Click);
            // 
            // 打开OToolStripMenuItem
            // 
            this.打开OToolStripMenuItem.Name = "打开OToolStripMenuItem";
            this.打开OToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.O)));
            this.打开OToolStripMenuItem.Size = new System.Drawing.Size(194, 26);
            this.打开OToolStripMenuItem.Text = "打开(&O)";
            this.打开OToolStripMenuItem.Click += new System.EventHandler(this.打开OToolStripMenuItem_Click);
            // 
            // 保存SToolStripMenuItem
            // 
            this.保存SToolStripMenuItem.Name = "保存SToolStripMenuItem";
            this.保存SToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.S)));
            this.保存SToolStripMenuItem.Size = new System.Drawing.Size(194, 26);
            this.保存SToolStripMenuItem.Text = "保存(&S)";
            this.保存SToolStripMenuItem.Click += new System.EventHandler(this.保存SToolStripMenuItem_Click);
            // 
            // 另存为AToolStripMenuItem
            // 
            this.另存为AToolStripMenuItem.Name = "另存为AToolStripMenuItem";
            this.另存为AToolStripMenuItem.Size = new System.Drawing.Size(194, 26);
            this.另存为AToolStripMenuItem.Text = "另存为(&A)";
            this.另存为AToolStripMenuItem.Click += new System.EventHandler(this.另存为AToolStripMenuItem_Click);
            // 
            // toolStripSeparator1
            // 
            this.toolStripSeparator1.Name = "toolStripSeparator1";
            this.toolStripSeparator1.Size = new System.Drawing.Size(191, 6);
            // 
            // 打印PToolStripMenuItem
            // 
            this.打印PToolStripMenuItem.Name = "打印PToolStripMenuItem";
            this.打印PToolStripMenuItem.Size = new System.Drawing.Size(194, 26);
            this.打印PToolStripMenuItem.Text = "打印(&P)";
            // 
            // toolStripSeparator3
            // 
            this.toolStripSeparator3.Name = "toolStripSeparator3";
            this.toolStripSeparator3.Size = new System.Drawing.Size(191, 6);
            // 
            // 退出XToolStripMenuItem
            // 
            this.退出XToolStripMenuItem.Name = "退出XToolStripMenuItem";
            this.退出XToolStripMenuItem.Size = new System.Drawing.Size(194, 26);
            this.退出XToolStripMenuItem.Text = "退出(&X)";
            this.退出XToolStripMenuItem.Click += new System.EventHandler(this.退出XToolStripMenuItem_Click);
            // 
            // 编辑EToolStripMenuItem
            // 
            this.编辑EToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
    
    
            this.撤销ZToolStripMenuItem,
            this.toolStripSeparator4,
            this.剪切TToolStripMenuItem,
            this.复制CToolStripMenuItem,
            this.粘贴PToolStripMenuItem,
            this.删除LToolStripMenuItem,
            this.toolStripSeparator5,
            this.全选AToolStripMenuItem,
            this.toolStripSeparator2,
            this.查找FToolStripMenuItem,
            this.替换RToolStripMenuItem,
            this.toolStripSeparator6,
            this.时间日期DToolStripMenuItem});
            this.编辑EToolStripMenuItem.Name = "编辑EToolStripMenuItem";
            this.编辑EToolStripMenuItem.Size = new System.Drawing.Size(69, 24);
            this.编辑EToolStripMenuItem.Text = "编辑(&E)";
            // 
            // 撤销ZToolStripMenuItem
            // 
            this.撤销ZToolStripMenuItem.Name = "撤销ZToolStripMenuItem";
            this.撤销ZToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Z)));
            this.撤销ZToolStripMenuItem.Size = new System.Drawing.Size(197, 26);
            this.撤销ZToolStripMenuItem.Text = "撤销(&U)";
            this.撤销ZToolStripMenuItem.Click += new System.EventHandler(this.撤销ZToolStripMenuItem_Click);
            // 
            // toolStripSeparator4
            // 
            this.toolStripSeparator4.Name = "toolStripSeparator4";
            this.toolStripSeparator4.Size = new System.Drawing.Size(194, 6);
            // 
            // 剪切TToolStripMenuItem
            // 
            this.剪切TToolStripMenuItem.Name = "剪切TToolStripMenuItem";
            this.剪切TToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.X)));
            this.剪切TToolStripMenuItem.Size = new System.Drawing.Size(197, 26);
            this.剪切TToolStripMenuItem.Text = "剪切(&T)";
            this.剪切TToolStripMenuItem.Click += new System.EventHandler(this.剪切TToolStripMenuItem_Click);
            // 
            // 复制CToolStripMenuItem
            // 
            this.复制CToolStripMenuItem.Name = "复制CToolStripMenuItem";
            this.复制CToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.C)));
            this.复制CToolStripMenuItem.Size = new System.Drawing.Size(197, 26);
            this.复制CToolStripMenuItem.Text = "复制(&C)";
            this.复制CToolStripMenuItem.Click += new System.EventHandler(this.复制CToolStripMenuItem_Click);
            // 
            // 粘贴PToolStripMenuItem
            // 
            this.粘贴PToolStripMenuItem.Name = "粘贴PToolStripMenuItem";
            this.粘贴PToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.V)));
            this.粘贴PToolStripMenuItem.Size = new System.Drawing.Size(197, 26);
            this.粘贴PToolStripMenuItem.Text = "粘贴(&P)";
            this.粘贴PToolStripMenuItem.Click += new System.EventHandler(this.粘贴PToolStripMenuItem_Click);
            // 
            // 删除LToolStripMenuItem
            // 
            this.删除LToolStripMenuItem.Name = "删除LToolStripMenuItem";
            this.删除LToolStripMenuItem.ShortcutKeys = System.Windows.Forms.Keys.Delete;
            this.删除LToolStripMenuItem.Size = new System.Drawing.Size(197, 26);
            this.删除LToolStripMenuItem.Text = "删除(&L)";
            this.删除LToolStripMenuItem.Click += new System.EventHandler(this.删除LToolStripMenuItem_Click);
            // 
            // toolStripSeparator5
            // 
            this.toolStripSeparator5.Name = "toolStripSeparator5";
            this.toolStripSeparator5.Size = new System.Drawing.Size(194, 6);
            // 
            // 查找FToolStripMenuItem
            // 
            this.查找FToolStripMenuItem.Name = "查找FToolStripMenuItem";
            this.查找FToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.F)));
            this.查找FToolStripMenuItem.Size = new System.Drawing.Size(197, 26);
            this.查找FToolStripMenuItem.Text = "查找(&F)";
            this.查找FToolStripMenuItem.Click += new System.EventHandler(this.查找FToolStripMenuItem_Click);
            // 
            // 替换RToolStripMenuItem
            // 
            this.替换RToolStripMenuItem.Name = "替换RToolStripMenuItem";
            this.替换RToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.H)));
            this.替换RToolStripMenuItem.Size = new System.Drawing.Size(197, 26);
            this.替换RToolStripMenuItem.Text = "替换(&R)";
            this.替换RToolStripMenuItem.Click += new System.EventHandler(this.替换RToolStripMenuItem_Click);
            // 
            // toolStripSeparator6
            // 
            this.toolStripSeparator6.Name = "toolStripSeparator6";
            this.toolStripSeparator6.Size = new System.Drawing.Size(194, 6);
            // 
            // 全选AToolStripMenuItem
            // 
            this.全选AToolStripMenuItem.Name = "全选AToolStripMenuItem";
            this.全选AToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.A)));
            this.全选AToolStripMenuItem.Size = new System.Drawing.Size(197, 26);
            this.全选AToolStripMenuItem.Text = "全选(&A)";
            this.全选AToolStripMenuItem.Click += new System.EventHandler(this.全选AToolStripMenuItem_Click);
            // 
            // 时间日期DToolStripMenuItem
            // 
            this.时间日期DToolStripMenuItem.Name = "时间日期DToolStripMenuItem";
            this.时间日期DToolStripMenuItem.ShortcutKeys = System.Windows.Forms.Keys.F5;
            this.时间日期DToolStripMenuItem.Size = new System.Drawing.Size(197, 26);
            this.时间日期DToolStripMenuItem.Text = "时间/日期(&D)";
            this.时间日期DToolStripMenuItem.Click += new System.EventHandler(this.时间日期DToolStripMenuItem_Click);
            // 
            // 格式OToolStripMenuItem
            // 
            this.格式OToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
    
    
            this.自动换行WToolStripMenuItem,
            this.字体FToolStripMenuItem,
            this.字体颜色CToolStripMenuItem});
            this.格式OToolStripMenuItem.Name = "格式OToolStripMenuItem";
            this.格式OToolStripMenuItem.Size = new System.Drawing.Size(73, 24);
            this.格式OToolStripMenuItem.Text = "格式(&O)";
            // 
            // 自动换行WToolStripMenuItem
            // 
            this.自动换行WToolStripMenuItem.Name = "自动换行WToolStripMenuItem";
            this.自动换行WToolStripMenuItem.Size = new System.Drawing.Size(169, 26);
            this.自动换行WToolStripMenuItem.Text = "自动换行(&W)";
            // 
            // 字体FToolStripMenuItem
            // 
            this.字体FToolStripMenuItem.Name = "字体FToolStripMenuItem";
            this.字体FToolStripMenuItem.Size = new System.Drawing.Size(169, 26);
            this.字体FToolStripMenuItem.Text = "字体大小(&F)";
            this.字体FToolStripMenuItem.Click += new System.EventHandler(this.字体FToolStripMenuItem_Click);
            // 
            // 字体颜色CToolStripMenuItem
            // 
            this.字体颜色CToolStripMenuItem.Name = "字体颜色CToolStripMenuItem";
            this.字体颜色CToolStripMenuItem.Size = new System.Drawing.Size(169, 26);
            this.字体颜色CToolStripMenuItem.Text = "字体颜色(&C)";
            this.字体颜色CToolStripMenuItem.Click += new System.EventHandler(this.字体颜色CToolStripMenuItem_Click);
            // 
            // 查看VToolStripMenuItem
            // 
            this.查看VToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
    
    
            this.层叠ToolStripMenuItem,
            this.水平平铺ToolStripMenuItem,
            this.垂直平铺ToolStripMenuItem});
            this.查看VToolStripMenuItem.Name = "查看VToolStripMenuItem";
            this.查看VToolStripMenuItem.Size = new System.Drawing.Size(71, 24);
            this.查看VToolStripMenuItem.Text = "查看(&V)";
            // 
            // 层叠ToolStripMenuItem
            // 
            this.层叠ToolStripMenuItem.Name = "层叠ToolStripMenuItem";
            this.层叠ToolStripMenuItem.Size = new System.Drawing.Size(144, 26);
            this.层叠ToolStripMenuItem.Text = "层叠";
            this.层叠ToolStripMenuItem.Click += new System.EventHandler(this.层叠ToolStripMenuItem_Click);
            // 
            // 水平平铺ToolStripMenuItem
            // 
            this.水平平铺ToolStripMenuItem.Name = "水平平铺ToolStripMenuItem";
            this.水平平铺ToolStripMenuItem.Size = new System.Drawing.Size(144, 26);
            this.水平平铺ToolStripMenuItem.Text = "水平平铺";
            this.水平平铺ToolStripMenuItem.Click += new System.EventHandler(this.水平平铺ToolStripMenuItem_Click);
            // 
            // 垂直平铺ToolStripMenuItem
            // 
            this.垂直平铺ToolStripMenuItem.Name = "垂直平铺ToolStripMenuItem";
            this.垂直平铺ToolStripMenuItem.Size = new System.Drawing.Size(144, 26);
            this.垂直平铺ToolStripMenuItem.Text = "垂直平铺";
            this.垂直平铺ToolStripMenuItem.Click += new System.EventHandler(this.垂直平铺ToolStripMenuItem_Click);
            // 
            // 帮助HToolStripMenuItem
            // 
            this.帮助HToolStripMenuItem.Name = "帮助HToolStripMenuItem";
            this.帮助HToolStripMenuItem.Size = new System.Drawing.Size(73, 24);
            this.帮助HToolStripMenuItem.Text = "帮助(&H)";
            this.帮助HToolStripMenuItem.Click += new System.EventHandler(this.帮助HToolStripMenuItem_Click);
            // 
            // openFileDialog1
            // 
            this.openFileDialog1.FileName = "openFileDialog1";
            // 
            // printPreviewDialog1
            // 
            this.printPreviewDialog1.AutoScrollMargin = new System.Drawing.Size(0, 0);
            this.printPreviewDialog1.AutoScrollMinSize = new System.Drawing.Size(0, 0);
            this.printPreviewDialog1.ClientSize = new System.Drawing.Size(400, 300);
            this.printPreviewDialog1.Enabled = true;
            this.printPreviewDialog1.Icon = ((System.Drawing.Icon)(resources.GetObject("printPreviewDialog1.Icon")));
            this.printPreviewDialog1.Name = "printPreviewDialog1";
            this.printPreviewDialog1.Visible = false;
            // 
            // statusStrip1
            // 
            this.statusStrip1.ImageScalingSize = new System.Drawing.Size(20, 20);
            this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
    
    
            this.toolStripStatusLabel1});
            this.statusStrip1.Location = new System.Drawing.Point(0, 594);
            this.statusStrip1.Name = "statusStrip1";
            this.statusStrip1.Size = new System.Drawing.Size(648, 25);
            this.statusStrip1.TabIndex = 3;
            this.statusStrip1.Text = "statusStrip1";
            // 
            // toolStripStatusLabel1
            // 
            this.toolStripStatusLabel1.Name = "toolStripStatusLabel1";
            this.toolStripStatusLabel1.Size = new System.Drawing.Size(129, 20);
            this.toolStripStatusLabel1.Text = "系统当前时间为:";
            // 
            // timer1
            // 
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick_1);
            // 
            // toolStripSeparator2
            // 
            this.toolStripSeparator2.Name = "toolStripSeparator2";
            this.toolStripSeparator2.Size = new System.Drawing.Size(194, 6);
            // 
            // FormBack
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(648, 619);
            this.Controls.Add(this.statusStrip1);
            this.Controls.Add(this.menuStrip1);
            this.IsMdiContainer = true;
            this.MainMenuStrip = this.menuStrip1;
            this.Name = "FormBack";
            this.Text = "文本编辑器";
            this.Load += new System.EventHandler(this.FormBack_Load);
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.statusStrip1.ResumeLayout(false);
            this.statusStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.MenuStrip menuStrip1;
        private System.Windows.Forms.OpenFileDialog openFileDialog1;
        private System.Windows.Forms.SaveFileDialog saveFileDialog1;
        private System.Windows.Forms.FontDialog fontDialog1;
        private System.Windows.Forms.ColorDialog colorDialog1;
        private System.Drawing.Printing.PrintDocument printDocument1;
        private System.Windows.Forms.PrintPreviewDialog printPreviewDialog1;
        private System.Windows.Forms.ToolStripMenuItem 文件NToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 新建NToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 打开OToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 保存SToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 另存为AToolStripMenuItem;
        private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
        private System.Windows.Forms.ToolStripMenuItem 打印PToolStripMenuItem;
        private System.Windows.Forms.ToolStripSeparator toolStripSeparator3;
        private System.Windows.Forms.ToolStripMenuItem 退出XToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 编辑EToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 撤销ZToolStripMenuItem;
        private System.Windows.Forms.ToolStripSeparator toolStripSeparator4;
        private System.Windows.Forms.ToolStripMenuItem 剪切TToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 复制CToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 粘贴PToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 删除LToolStripMenuItem;
        private System.Windows.Forms.ToolStripSeparator toolStripSeparator5;
        private System.Windows.Forms.ToolStripMenuItem 查找FToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 替换RToolStripMenuItem;
        private System.Windows.Forms.ToolStripSeparator toolStripSeparator6;
        private System.Windows.Forms.ToolStripMenuItem 全选AToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 时间日期DToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 格式OToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 自动换行WToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 字体FToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 字体颜色CToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 查看VToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 帮助HToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 层叠ToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 水平平铺ToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem 垂直平铺ToolStripMenuItem;
        private System.Windows.Forms.StatusStrip statusStrip1;
        private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;
        private System.Windows.Forms.Timer timer1;
        private System.Windows.Forms.ToolStripSeparator toolStripSeparator2;
    }
}

FormFind.cs

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 IntergratedDesign
{
    
    
    public partial class FormFind : Form
    {
    
    

        FormRichText parentForm; //定义包含富文本框的窗体变量
        private String strSearch = ""; // 表示要查找的字符串
        private String strReplace = "";// 表示要替换成的字符串
        private int searchPos = 0, lastSearchPos = 0;// 前者表示当前的查找位置,后者表示记录上次的查找位置
        private bool find;

        public FormFind(FormRichText parent)
        {
    
    
            this.parentForm = parent;
            InitializeComponent();
        }


        //查找按钮事件响应
        private void button1_Click(object sender, EventArgs e)
        {
    
    
            SearchText();
        }

        //替换按钮事件响应
        private void button2_Click(object sender, EventArgs e)
        {
    
    
            ReplaceText();
        }


        //查找方法定义
        private bool SearchText()
        {
    
    
            strSearch = findText.Text;//查找的文本
            strReplace = replaceText.Text;//替换的文本

            if (checkCase.Checked)// 表示区分大小写查找
            {
    
    
                if (radioDown.Checked) // 表示向下查找
                {
    
    
                    searchPos = parentForm.richTextBox1.Find(strSearch, searchPos, parentForm.richTextBox1.Text.Length, RichTextBoxFinds.MatchCase);
                }
                else// 表示向上查找
                {
    
    
                    this.searchPos = this.parentForm.richTextBox1.Find(this.strSearch, 0, searchPos, RichTextBoxFinds.MatchCase | RichTextBoxFinds.Reverse);
                }
            }
            else// 不区分大小写进行查找
            {
    
    
                if (this.radioDown.Checked)// 表示向下查找
                {
    
    
                    this.searchPos = this.parentForm.richTextBox1.Find(this.strSearch, searchPos, this.parentForm.richTextBox1.Text.Length, RichTextBoxFinds.None);
                }
                else// 表示向上查找
                {
    
    
                    this.searchPos = this.parentForm.richTextBox1.Find(this.strSearch, 0, searchPos, RichTextBoxFinds.Reverse);
                }
            }


            if (this.searchPos < 0)//如果未找到
            {
    
    
                this.searchPos = this.lastSearchPos;
                //回到上次查找位置
                find = false;//表示未找到
            }
            else//找到文本
            {
    
    
                if (this.radioDown.Checked)
                //如果是向下查找,设置新的查找位置,继续向下查找
                {
    
    
                    this.searchPos += this.strSearch.Length;
                    //新的查找位置是本次开始的查找位置加上查找到文本的长度
                }
                else//如果是向上查找,设置新的查找位置,继续向前查找
                {
    
    
                    this.searchPos -= this.strSearch.Length;
                }
            }
            this.lastSearchPos = this.searchPos;
            //开始查找,把查找位置保存
            return find;
        }

        private void FormFind_Load(object sender, EventArgs e)
        {
    
    

        }

        //替换按钮事件响应
        private void ReplaceText()
        {
    
    
            this.strSearch = this.findText.Text;//查找的文本
            this.strReplace = this.replaceText.Text;//替换的文本
            if (!SearchText())//调用查找方法查找是否存在
            {
    
    
                if (this.parentForm.richTextBox1.SelectedText.Length > 0)
                {
    
    
                    //如果找到,则进行替换
                    this.parentForm.richTextBox1.SelectedText = this.strReplace;
                }
            }
        }

    }
}

FormFind.Designer.cs

namespace IntergratedDesign
{
    
    
    partial class FormFind
    {
    
    
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
    
    
            if (disposing && (components != null))
            {
    
    
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
    
    
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.checkCase = new System.Windows.Forms.CheckBox();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.radioUp = new System.Windows.Forms.RadioButton();
            this.radioDown = new System.Windows.Forms.RadioButton();
            this.findText = new System.Windows.Forms.TextBox();
            this.replaceText = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(32, 36);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(67, 15);
            this.label1.TabIndex = 0;
            this.label1.Text = "查找内容";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(32, 87);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(52, 15);
            this.label2.TabIndex = 1;
            this.label2.Text = "替换为";
            // 
            // checkCase
            // 
            this.checkCase.AutoSize = true;
            this.checkCase.Location = new System.Drawing.Point(35, 136);
            this.checkCase.Name = "checkCase";
            this.checkCase.Size = new System.Drawing.Size(104, 19);
            this.checkCase.TabIndex = 2;
            this.checkCase.Text = "区分大小写";
            this.checkCase.UseVisualStyleBackColor = true;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(337, 27);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(50, 31);
            this.button1.TabIndex = 3;
            this.button1.Text = "查找";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(337, 78);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(50, 31);
            this.button2.TabIndex = 4;
            this.button2.Text = "替换";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // radioUp
            // 
            this.radioUp.AutoSize = true;
            this.radioUp.Location = new System.Drawing.Point(157, 135);
            this.radioUp.Name = "radioUp";
            this.radioUp.Size = new System.Drawing.Size(58, 19);
            this.radioUp.TabIndex = 5;
            this.radioUp.Text = "向上";
            this.radioUp.UseVisualStyleBackColor = true;
            // 
            // radioDown
            // 
            this.radioDown.AutoSize = true;
            this.radioDown.Checked = true;
            this.radioDown.Location = new System.Drawing.Point(239, 136);
            this.radioDown.Name = "radioDown";
            this.radioDown.Size = new System.Drawing.Size(58, 19);
            this.radioDown.TabIndex = 6;
            this.radioDown.TabStop = true;
            this.radioDown.Text = "向下";
            this.radioDown.UseVisualStyleBackColor = true;
            // 
            // findText
            // 
            this.findText.Location = new System.Drawing.Point(105, 31);
            this.findText.Name = "findText";
            this.findText.Size = new System.Drawing.Size(192, 25);
            this.findText.TabIndex = 7;
            // 
            // replaceText
            // 
            this.replaceText.Location = new System.Drawing.Point(105, 81);
            this.replaceText.Name = "replaceText";
            this.replaceText.Size = new System.Drawing.Size(192, 25);
            this.replaceText.TabIndex = 8;
            // 
            // FormFind
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(426, 175);
            this.Controls.Add(this.replaceText);
            this.Controls.Add(this.findText);
            this.Controls.Add(this.radioDown);
            this.Controls.Add(this.radioUp);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.checkCase);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Name = "FormFind";
            this.Text = "查找和替换";
            this.Load += new System.EventHandler(this.FormFind_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.CheckBox checkCase;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.RadioButton radioUp;
        private System.Windows.Forms.RadioButton radioDown;
        private System.Windows.Forms.TextBox findText;
        private System.Windows.Forms.TextBox replaceText;
    }
}

FormRichText.cs

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 IntergratedDesign
{
    
    
    public partial class FormRichText : Form
    {
    
    
        public FormRichText()
        {
    
    
            InitializeComponent();
        }

        public bool isChanged = true;
        public bool isSaved;

        private void FormRichText_Load(object sender, EventArgs e)
        {
    
    

        }

        public Font SelectionFont {
    
     get; internal set; }

        internal void SaveFile(string fileName, RichTextBoxStreamType plainText)
        {
    
    
            throw new NotImplementedException();
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
    
    
            isChanged = true;
        }
    }
}

FormRichText.Designer.cs

namespace IntergratedDesign
{
    
    
    partial class FormRichText 
    {
    
    
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
    
    
            if (disposing && (components != null))
            {
    
    
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
    
    
            this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
            this.saveFileDialog2 = new System.Windows.Forms.SaveFileDialog();
            this.fontDialog1 = new System.Windows.Forms.FontDialog();
            this.richTextBox1 = new System.Windows.Forms.RichTextBox();
            this.SuspendLayout();
            // 
            // richTextBox1
            // 
            this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.richTextBox1.Location = new System.Drawing.Point(0, 0);
            this.richTextBox1.Name = "richTextBox1";
            this.richTextBox1.Size = new System.Drawing.Size(537, 397);
            this.richTextBox1.TabIndex = 0;
            this.richTextBox1.Text = "";
            // 
            // FormRichText
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(537, 397);
            this.Controls.Add(this.richTextBox1);
            this.Name = "FormRichText";
            this.Text = "文本编辑";
            this.Load += new System.EventHandler(this.FormRichText_Load);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.SaveFileDialog saveFileDialog1;
        private System.Windows.Forms.SaveFileDialog saveFileDialog2;
        private System.Windows.Forms.FontDialog fontDialog1;
        public System.Windows.Forms.RichTextBox richTextBox1;

    }
}

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IntergratedDesign
{
    
    
    static class Program
    {
    
    
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
    
    
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FormBack());
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_51008866/article/details/115588066