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

📄 childform.cs

📁 在CSharpNotepad中可以创建和编辑简单文本文档
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CSharpNotepad
{
    public partial class childForm : Form
    {   
        #region 全局变量

        private mainForm _Owner;    //标记该子窗口的父窗口

        public string fileName;     //当前子窗口加载的文件名
        public string filePath;     //当前子窗口加载的文件路径
        public string fileType;     //当前子窗口加载的文件类型,".rtf"或".txt"

        public bool isNewDoc;       //是否是本程序新建的文档
                                    //说明:如是则无法直接保存,需调用另存为应用:主窗口保存文件函数的判断

        private int row, col;                   //光标所在的行和列

        //public bool docHasData = false;         //是否加载了文件
        //public bool docHasChanged = false;      //文本是否发生改变
        //public bool docHasSaved = false;        //当前文件是否保存

        public Font currentFont;                            //记录当前文本格式信息
        public HorizontalAlignment currentAlignment;        //记录当前文本对齐方式
        
        #endregion


        #region 构造函数

        public childForm()
        {
            InitializeComponent();
        }

        public childForm(mainForm _Parent)
		{
			InitializeComponent();

            this._Owner = _Parent;   //设置该子窗口的父窗口

            //this.currentFont = new Font("宋体", 10.0F); //设置默认文本格式
        }

        #endregion


        #region 其他方法

        private void ChildForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.Text.IndexOf("*", 0) != -1)
            {
                string strask = "文件 " + this.fileName + " 的文字已经改变。\n想保存文件吗?";
                DialogResult asksave = MessageBox.Show(strask, "Notepad", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
                switch (asksave)
                {
                    case DialogResult.Yes: _Owner.保存ToolStripMenuItem.PerformClick(); break;
                    case DialogResult.No: break;
                    case DialogResult.Cancel: e.Cancel = true; break;
                }
            }

            if (_Owner.MdiChildren.Length == 1 || e.Cancel == true)
            {
               
            }
        }
    
        private void ChildForm_Closed(object sender, EventArgs e)
        {
            this._Owner.tabControl1.TabPages.RemoveAt(this._Owner.tabControl1.SelectedIndex);
            if (this._Owner.tabControl1.TabCount == 0)
            {
                this._Owner.tabControl1.Height = 0;
                this._Owner.tbCurrentFormClose.Height = 0;
            }
            this._Owner.CheckActiveMdiChild();
        }

        #endregion


        #region 文本框richTextBox1的事件处理函数

        private void CheckTextChanged()
        {
            if (richTextBox1.CanRedo == true)
            {
                if (this.Text.IndexOf("*", 0) == -1)
                {
                    ///this._Owner.tabControl1.SelectedTab.Text = this.fileName;
                    this.Text = this.fileName;
                }
            }
            else
            {
                if (this.Text.IndexOf("*", 0) == -1)
                {
                    //this._Owner.tabControl1.SelectedTab.Text = string.Concat(this.fileName, "*");
                    this.Text = string.Concat(this.fileName, "*");
                }
            }
        }
        
        private void GetRowAndCol() // 获取行和列的数据
        {
            int begin = 0;
            int end = richTextBox1.SelectionStart;
            this.row = richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart);
            while (begin < end)
            {
                if (this.row == richTextBox1.GetLineFromCharIndex(begin))
                {
                    break;
                }
                else
                {
                    begin++;
                }
            }
            this.col = end - begin;
            this.row++;
            this.col++;

            this._Owner.toolStripStatusRow.Text = this.row.ToString();
            this._Owner.toolStripStatusCol.Text = this.col.ToString();
        }

        public void GetCurrentFontAndAlignment() // 获取当前文本格式和字形信息
        {
            currentFont = richTextBox1.SelectionFont;
            currentAlignment = richTextBox1.SelectionAlignment;
            //******************************************
            //还要判断选中的文本有不同的字体样式
            //******************************************
        }

        public void richTextBox1_Click(object sender, EventArgs e)
        {
            this.Activate();
            this.GetRowAndCol();
        }

        private void richTextBox1_KeyDown(object sender, EventArgs e)
        {
            this.Activate();
            this.GetRowAndCol();
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            this.GetRowAndCol();
            this.CheckTextChanged();
        }

        private void richTextBox1_SelectionChanged(object sender, EventArgs e)
        {
            this.GetCurrentFontAndAlignment();

            if (richTextBox1.SelectionFont != null)
            {
                this._Owner.SetCurrentFont();
            }

            this._Owner.SetCurrentAlignment();
        }

        private void richTextBox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Link;
            else
                e.Effect = DragDropEffects.None;
        }

        private void richTextBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
        {
            if (this.Text.IndexOf("*", 0) != -1 || this.isNewDoc == false)
            {
                this._Owner.MainForm_DragDrop(sender, e);
                return;
            }
            if (this.richTextBox1.Text.Length == 0 && this.isNewDoc == true)
            {
                this.filePath = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString(); ;
                FileInfo finfo = new FileInfo(this.filePath);

                if (finfo.Extension == ".rtf")
                {
                    this.richTextBox1.LoadFile(this.filePath, RichTextBoxStreamType.RichText);
                    this.Icon = Properties.Resources.RtfIcon;
                }
                else if (finfo.Extension == ".txt")
                {
                    this.richTextBox1.LoadFile(this.filePath, RichTextBoxStreamType.PlainText);
                    this.Icon = Properties.Resources.TxtIcon;
                }
                else
                {
                    MessageBox.Show("不能打开此格式的文件,请检查后再试!", "Notepad", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                
                this.fileName = finfo.Name;
                this.fileType = finfo.Extension;
                this.isNewDoc = false;

                this._Owner.tabControl1.SelectedTab.Text = this.fileName;
                
                this.Name = this.fileName;
                this.Text = this.fileName;
            }
        }
        
        #endregion


        #region 文字编辑功能

        public void Undo() //撤消
        {
            richTextBox1.Undo();
        }
        public void Redo() //恢复
        {
            richTextBox1.Redo();
        }
        public void Cut() //剪切
        {
            richTextBox1.Cut();
            richTextBox1.SelectedText = "";
        }

        public void Copy() //复制
        {
            richTextBox1.Copy();
        }

        public void Paste() //粘贴
        {
            richTextBox1.Paste();
        }

        public void Delete() //删除
        {
            richTextBox1.SelectedText = "";
        }
        
        public void SelectAll() //全选
        {
            richTextBox1.SelectAll();
        }

        #endregion


        #region 右键事件处理

        private void 撤消ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Undo();
        }
        private void 恢复RToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Redo();
        }
        private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Cut();
        }
        private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Copy();
        }
        private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Paste();
        }
        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Delete();
        }
        private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.SelectAll();
        }

        #endregion


        #region ****设置文本的字体和字号

        public void SetFontFamilyName(string newFontFamilyName, float FontSize) //单独设置字体
        {
            if (richTextBox1.SelectionLength > 0)
            {
                richTextBox1.SelectionFont = new Font(newFontFamilyName, FontSize);
            }

            currentFont = richTextBox1.SelectionFont;
            richTextBox1.Focus();
        }

        public void SetFontSize(string FontFamilyName, float newFontSize) //单独设置字号
        {
            if (richTextBox1.SelectionLength > 0)
            {
                richTextBox1.SelectionFont = new Font(FontFamilyName, newFontSize);
            }

            currentFont = richTextBox1.SelectionFont;
            richTextBox1.Focus();
        }

        #endregion


        #region 设置文本的颜色

        public void SetFontColor()
        {
            /*
            ColorDialog cdg = new ColorDialog();
            cdg.AllowFullOpen = true;
            cdg.AnyColor = true;
            cdg.FullOpen = true;
            cdg.ShowHelp = true;

            if (cdg.ShowDialog() == DialogResult.OK)
            {
                if (flag == 1)
                    richTextBox1.ForeColor = cdg.Color;
                if (flag == 0)
                    richTextBox1.BackColor = cdg.Color;
            }*/

        }
        #endregion


        #region 设置文本的字形(粗体/斜体/下划线)

        public void SetBold()
        {
            if (currentFont.Bold)
                richTextBox1.SelectionFont = new Font(currentFont, currentFont.Style ^ FontStyle.Bold);
            else richTextBox1.SelectionFont = new Font(currentFont, currentFont.Style | FontStyle.Bold);
        }

        public void SetItalic()
        {
            if (currentFont.Italic)
                richTextBox1.SelectionFont = new Font(currentFont, currentFont.Style ^ FontStyle.Italic);
            else richTextBox1.SelectionFont = new Font(currentFont, currentFont.Style | FontStyle.Italic);
        }

        public void SetUnderLine()
        {
            if (currentFont.Underline)
                richTextBox1.SelectionFont = new Font(currentFont, currentFont.Style ^ FontStyle.Underline);
            else richTextBox1.SelectionFont = new Font(currentFont, currentFont.Style | FontStyle.Underline);
        }

        #endregion


        #region 设置文本的对齐格式

        public void SetLeftAlign() //左对齐
        {
            richTextBox1.SelectionAlignment = HorizontalAlignment.Left;
        }

        public void SetMiddleAlign() //居中
        {
            richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
        }

        public void SetRightAlign() //右对齐
        {
            richTextBox1.SelectionAlignment = HorizontalAlignment.Right;
        }

        #endregion
    }
}

⌨️ 快捷键说明

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