自己写的记事本程序(功能不完善)

public partial class Form1 : Form
    {
        bool IsTextChanged = false;
        public Form1()
        {
            InitializeComponent();
        }

        private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.txtText.Text = "" ;
            IsTextChanged = false;
        }

        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "打开文件";
            ofd.Filter = "文本文件|*.txt|所有文件|*.*";
            if(ofd.ShowDialog()==DialogResult.OK)
            {
                //第一步:声明一个文件流
                FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                //第二步:创建读写器
                StreamReader sr=new StreamReader(fs,Encoding.Default);
                //第三步:读操作
                //xtText.Text = sr.ReadToEnd();      //读到最后结束
                while(sr.EndOfStream==false)
                {
                    string line=sr.ReadLine();        //读取一行
                    txtText.Text=txtText.Text+line+"\r\n";
                }
                //第四步:关闭读写器
                sr.Close();
                //第五步:关闭文件流
                fs.Close();
            }
            IsTextChanged = false;
        }

        private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveText();
        }

        protected void SaveText()
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Title = "保存文件";
            sfd.Filter = "文本文件|*.txt|所有文件|*.*";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                //声明一个文件流
                FileStream fs = new FileStream(sfd.FileName, FileMode.Create, FileAccess.Write);
                //创建写入器
                StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                //写入操作
                sw.Write(txtText.Text);
                //关闭写入器
                sw.Close();
                //关闭文件流
                fs.Close();
            }
        }

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

        private void txtText_TextChanged(object sender, EventArgs e)
        {
            IsTextChanged = true;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (IsTextChanged == true)
            {
                DialogResult dr=MessageBox.Show("文本内容已经改变,是否保存?", "询问", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    //保存文件
                    SaveText();
                }
                else if (dr == DialogResult.No)
                {
                    //取消保存
                }
                else
                {
                    e.Cancel = true;
                }
            }
            //记事本关闭时保存窗体的位置及大小,再次打开时记忆上次关闭时的状态
            int x = this.Location.X;
            int y = this.Location.Y;
            int w = this.Size.Width;
            int h = this.Size.Height;
            //规定一个配置文件叫 app.dll,,其实是一个文本文档
            StreamWriter sw = new StreamWriter(Application.StartupPath + "app.dll");
            sw.WriteLine(x.ToString());
            sw.WriteLine(y.ToString());
            sw.WriteLine(w.ToString());
            sw.WriteLine(h.ToString());
            sw.WriteLine(txtText.ForeColor.R);
            sw.WriteLine(txtText.ForeColor.G);            //把关闭时的字体颜色也保存下来
            sw.WriteLine(txtText.ForeColor.B);
            sw.Close();
        }

        private void 剪切SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtText.Cut();
        }

        private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtText.Copy();
        }

        private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtText.Paste();
        }

        private void 查找杏仁ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int currPos = txtText.SelectionStart;
            int findPos = txtText.Text.IndexOf("杏仁", currPos);
            if (findPos != -1)
            {
                //找到杏仁了
                txtText.Select(findPos, 2);
            }
        }

        private void 字体FToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FontDialog fd = new FontDialog();
            fd.Font = txtText.Font;
            if (fd.ShowDialog() == DialogResult.OK)
            {
                txtText.Font = fd.Font;
            }
        }

        private void 字体颜色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ColorDialog cd = new ColorDialog();
            cd.Color = txtText.ForeColor;
            if (cd.ShowDialog() == DialogResult.OK)
            {
                txtText.ForeColor = cd.Color;
            }
        }

        private void 背景颜色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ColorDialog cd = new ColorDialog();
            cd.Color = txtText.BackColor;
            if (cd.ShowDialog() == DialogResult.OK)
            {
                txtText.BackColor = cd.Color;
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string appPath = Application.StartupPath + "app.dll";
            if (File.Exists(appPath))
            {
                StreamReader sr = new StreamReader(appPath);
                int x = Convert.ToInt32(sr.ReadLine());
                int y = Convert.ToInt32(sr.ReadLine());
                int w = Convert.ToInt32(sr.ReadLine());
                int h = Convert.ToInt32(sr.ReadLine());
                int r = Convert.ToInt32(sr.ReadLine());
                int g = Convert.ToInt32(sr.ReadLine());
                int b = Convert.ToInt32(sr.ReadLine());

                this.Location = new Point(x, y);
                this.Size = new Size(w, h);
                this.txtText.ForeColor = Color.FromArgb(r, g, b);

                sr.Close();
            }
        }   
       
   
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值