文本编辑器的制作(C#)

一、实验要求

1、实验目的:

(1)综合运行各种控件,进一步熟悉可视化编程方式,特别是熟悉使用Windows的编程环境和风格。

(2)综合运用C#.NET提供的标准控件,如:菜单控件、公共对话框控件、RichTextBox控件、ToolStrip控件制作小型应用系统。

 

2、实验内容:

(1)参考系统提供的写字板功能,编写一个小型的文字编辑工具。

(2)对该文档编辑器,至少完成以下功能:

    文件:(新建、打开、保存、退出)

    编辑:(复制、剪切、粘贴、全选)

    格式:(自动换行、字体、颜色),格式的设置功能要区分是设置当前块的格式还是设置所有内容的格式;

    界面设计时应有工具栏,弹出菜单,状态栏等功能来提高操作的便利性;

 

二、设计思路

1、概要设计:

(1)总体设计思路:

         采用MDI设计思想,实现一个主窗体,多个子窗体操作;

         主窗体只是一个容器,子窗体是实现操作的主体;

         主窗体显示的是子窗体的操作,所有功能都放在子窗体上,实现主次的分层;

         每篇文档是放在一个独立的子窗体上进行编辑的;

         子窗体的操作是独立的,之间互不影响;

         关闭主窗体,会执行依次关闭各子窗体的操作;

 

(2)程序结构和所包含的操作:

主窗口:

         菜单栏:

                   文件:新建、打开、保存、另存为、退出;

                   编辑:撤销、还原、剪切、复制、粘贴、查找/替换、全选;

                  格式:自动换行、字体;

                   查看:窗体布局;

                   帮助:关于程序;

         快捷方式栏:新建、打开、保存、另存为、复制、剪切、粘贴、撤销、还原、居中、左                                     对齐、右对齐、查找;

         文字编辑栏:文字样式、字体、字体大小、加粗、斜体、下划线、字体颜色;

         其它:目录树、分隔栏、panel、imageList,timer等;

子窗口:

         菜单栏(合并):查找/替换、自动换行;

状态栏:光标位置、操作状态、进度条;

右键菜单栏:撤销、复制、剪切、粘贴、保存、另存为;

其它:colorDialog、fontDialog、timer等;

查找窗口:

         标签页、查找文字输入框、替换文字输入框、查找按钮、替换按钮、取消按钮等;

About窗口:

         Panel、pictureBox、timer等;

 

(3)窗体功能:

主窗口:

         所有子窗口的父容器,实现所有操作的接口;

子窗口:

         方法的主体、定义并实现大部分的操作,所有函数的入口,并传递给主窗体调用;

查找窗口:

         运行并执行当前活动子窗口的查找和替换工作,被子窗口调用;

About窗口:

         一个类似欢迎窗口的作用,用于程序退出时显示一些特别效果用的,没什么大功能;

 

2、数据类型:

(1)主要调用的系统函数:

    主窗口:

        OpenFileDialog openFile = new OpenFileDialog();//打开文件对话框

        SaveFileDialog saveFile = new SaveFileDialog();//保存文件对话框

        private MdiClient mdiClient;//当前窗体的客户区域

        TreeNode tRoot = new TreeNode("我的电脑", 0, 0);//加载目录树

    子窗口:

        HorizontalAlignment.Center; //居中

        HorizontalAlignment.Left; //左对齐

        HorizontalAlignment.Right; //右对齐

        Undo();//撤销

        Redo();//还原

        SelectAll();//全选

        Copy();//复制

        Cut();//剪切

        Paste();//粘贴

        FontStyle.Bold ;//加粗

        FontStyle.Italic; //斜体

        FontStyle.Underline; //下划线

        colorDialog1.ShowDialog();//颜色对话框

        fontDialog1.ShowDialog();//文字样式对话框

 

3、程序的基本结构:

主窗口

子窗口

子窗口

子窗口

子窗口

About窗口

查找窗口

 

4、详细设计:

主要函数及其功能的实现

加载目录树

        private void InitialTreeView()

        { //设置树根并加载;

            TreeNode tRoot = new TreeNode("我的电脑", 0, 0);

            this.treeView1.Nodes.Add(tRoot);

            foreach (string driver in Directory.GetLogicalDrives())

            {//加载所有硬盘到树上

                TreeNode cRoot = new TreeNode(driver, 1, 1);

                tRoot.Nodes.Add(cRoot);

                AddDirs(cRoot);

            }

            tRoot.Expand();

        }

        private void AddDirs(TreeNode node)

        {//加载目录

            try

            {

                DirectoryInfo curDir = new DirectoryInfo(GetPathFromNode(node));

                DirectoryInfo[] childDir = curDir.GetDirectories();

                FileInfo[] childFile = curDir.GetFiles();

                string name;

                for (int i = 0; i < childDir.Length; i++)

                {//加载目录信息

                    name = childDir[i].Name;

                    if (!name.Equals(".") && !name.Equals(".."))

                    {

                        node.Nodes.Add(new TreeNode(name, 2, 2));

                    }

                }

                for (int i = 0; i < childFile.Length; i++)

                {//加载文件信息

                    name = childFile[i].Name;

                    node.Nodes.Add(new TreeNode(name, 3, 3));

                }

            }

            catch { }

        }

        private string GetPathFromNode(TreeNode node)

        {//获取子结点

            if (node.Parent == null || node.Parent.Text == "我的电脑")

                return node.Text;

            return Path.Combine(GetPathFromNode(node.Parent), node.Text);

        }

打开文件

        public void fileOpen(string filename)

        {

            try

            {//执行打开操作;

            catch (Exception)

            { //打开失败,返回错误信息 }

        }

保存文件

        public bool Save()

        {

            if (//文件已经存在)

            {

                //直接按原路径保存;return true;

            }

            else

            {

                if (//另存为操作成功)

                    return true;

                else

                    return false;

            }

        }

        public bool SaveAs()

        {

            //设置默认信息

            if (//点击保存按钮)

            {

                //执行保存操作;return true;

            }

            else

                return false;

        }

是否保存

        public DialogResult IsSave()

        {//弹出保存提示框;}

        #endregion

        #region//退出

        public bool ExitFile()

        {

            if (//文档有改动)

            {

                //返回是否要保存提示框

                if (//要保存)

                {

                    if (//失败保存)

                        return false;

                    else

                        return true;

                }

                if (//取消)

                    return false;

            }

            return true;

        }

查找

        public void SearchText()

        {

            //获取当前活动子窗口

            //获取起始位置

            if (editor != null)

            {

                if (editor.SelectionLength > 0)

                    start += editor.SelectionLength;

                int end = editor.Find(this.txt_searchContent.Text, start, RichTextBoxFinds.MatchCase);//调用查找函数

                if (end < 0)

                {

                    MessageBox.Show("已到文件结尾,查找结束!", "查找结束对话框");

                    start = 0;

                    btn_search.Text = "查找";

                    btn_replaceAll.Enabled = false;

                    btn_replace.Enabled = false;

                }

                else

                {

                    btn_search.Text = "查找下一个";

                    btn_replace.Enabled = true;

                    btn_replaceAll.Enabled = true;

                }

            }

            else //文本内容为空

            {

                MessageBox.Show("文本没有内容!", "查找结束对话框");

                btn_search.Text = "查找";

                btn_replaceAll.Enabled = false;

                btn_replace.Enabled = false;

            }

        }

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自己的程序源码namespace zdm { partial class formFindReplace { /// /// 必需的设计器变量。 /// private System.ComponentModel.IContainer components = null; /// /// 清理所有正在使用的资源。 /// /// 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.buttonFind = new System.Windows.Forms.Button(); this.buttonReplace = new System.Windows.Forms.Button(); this.SuspendLayout(); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(35, 15); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(65, 12); this.label1.TabIndex = 0; this.label1.Text = "查找字符串"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(35, 60); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(65, 12); this.label2.TabIndex = 1; this.label2.Text = "替换字符串"; // // textBox1 // this.textBox1.Location = new System.Drawing.Point(135, 12); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(120, 21); this.textBox1.TabIndex = 2; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(135, 51); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(120, 21); this.textBox2.TabIndex = 3; // // buttonFind // this.buttonFind.Location = new System.Drawing.Point(49, 110); this.buttonFind.Name = "buttonFind"; this.buttonFind.Size = new System.Drawing.Size(75, 23); this.buttonFind.TabIndex = 4; this.buttonFind.Text = "查找下一处"; this.buttonFind.UseVisualStyleBackColor = true; this.buttonFind.Click += new System.EventHandler(this.buttonFind_Click); // // buttonReplace // this.buttonReplace.Location = new System.Drawing.Point(164, 110); this.buttonReplace.Name = "buttonReplace"; this.buttonReplace.Size = new System.Drawing.Size(91, 23); this.buttonReplace.TabIndex = 5; this.buttonReplace.Text = "替换查到字符"; this.buttonReplace.UseVisualStyleBackColor = true; this.buttonReplace.Click += new System.EventHandler(this.buttonReplace_Click); // // formFindReplace // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(292, 169); this.Controls.Add(this.buttonReplace); this.Controls.Add(this.buttonFind); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "formFindReplace"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "查找和替换"; this.TopMost = true; this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Button buttonFind; private System.Windows.Forms.Button buttonReplace; } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值