记事本程序

Anchor:控件与容器周围的距离保持不变

Dock:定义容器要停靠到哪一边,重要的一个是Fill填充

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;
using System.IO;

namespace 简单记事本
{
    public partial class Form1 : Form
    {
        private bool IsSave = false;
        string fileName = null;

        public Form1()
        {
            InitializeComponent();
            this.Text = "无标题-猫的记事本";
            
        }

        private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (IsSave == true)
            {
                DialogResult re = MessageBox.Show("是否将更改保存到 " + this.Text.Substring(0, this.Text.Length - 6) + "?", "询问", MessageBoxButtons.YesNoCancel);
                if (re == DialogResult.Yes)
                {
                    SaveFile();
                }
                else if(re == DialogResult.Cancel)
                {
                    return;//什么也不做,直接返回
                }
               
            }

            txtText.Text = "";
            fileName = "";
            this.Text = "无标题-猫的记事本";

        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            //打开文件      
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "打开文件";
            ofd.Filter = "文本文件|*.txt|所有文件|*.*";

            //读取文件步骤: 
            //1. 声明文件流,即在内存申请一个空间,让文件和这个空间建立联系
            //2. 创建读写器,用来操作文件流对象
            //3. 读操作
            //4. 释放读取器
            //5. 释放文件流对象


            if(ofd.ShowDialog() == DialogResult.OK)
            {
                //一打开指定文件,则立刻修改标题为文件名
                fileName = ofd.FileName;
                //                             只显示文件名,不显示文件路径
                this.Text = fileName.Substring(fileName.IndexOf("\") + 1) + "-猫的记事本";

                //1 声明文件流
                //                             1文件路径,2,3打开-读取,4打开文件后,其他进程对此文件的操作
                FileStream fs = new FileStream(ofd.FileName, FileMode.Open,FileAccess.Read,FileShare.Read);

                //fs创建了与文件相连的通道,而sr可用来操纵fs,进行文件的读取

                //2 创建读写器
                //StreamReader形参类型是Stream类,故可以接受其子类FileStream
                //StreamReader(fst)英文可以正常,汉字不能正常时,为编码问题,添加编码方式
                StreamReader sr = new StreamReader(fs,Encoding.Default);

                //3 读取
                //sr.ReadLine();//读取一行
                //sr.ReadToEnd();//读取到最后
                //sr.EndOfStream;
                while(sr.EndOfStream == false)
                {
                    string line = sr.ReadLine() + "
";
                    txtText.Text += line;
                }

                //或者一次性读完 
                //txtText.Text = sr.ReadToEnd();

                //4 关闭
                sr.Close();
                fs.Close();

                IsSave = false;

            }
        }

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

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

        private void SaveFile()
        {
            //写入文件步骤: 
            //1. 声明文件流,即在内存申请一个空间,让文件和这个空间建立联系
            //2. 创建写入器,用来操作文件流对象
            //3. 写操作
            //4. 释放写入器
            //5. 释放文件流对象

            if(fileName == null || fileName == "")
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Title = "保存文件";
                sfd.Filter = "文本文件|*.txt|所有文件|*.*";

                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    fileName = sfd.FileName;
                    this.Text = fileName.Substring(fileName.IndexOf("\") + 1) + "-猫的记事本";
                }
                else
                {
                    MessageBox.Show("文件打开失败!");
                    return;
                }
            } 

            FileStream fs = new FileStream(fileName, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs, Encoding.Default);
            sw.Write(txtText.Text);

            //sw写磁盘文件时,先写内存的fs,只有将 fs填满或者执行了sw关闭才将fs中内容写入磁盘
            sw.Close();
            fs.Close();

            IsSave = false;
        }

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

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (IsSave == true)
            {
                DialogResult re = MessageBox.Show("是否将更改保存到 "+this.Text.Substring(0,this.Text.Length - 6)+"?", "询问", MessageBoxButtons.YesNoCancel);
                if (re == DialogResult.Yes)
                {
                    SaveFile();
                }
                else if (re == DialogResult.No)
                {

                }
                else
                {
                    e.Cancel = true;
                }

                IsSave = false;
            }
        }

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

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

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

        private void 查找ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int currPos = txtText.SelectionStart;//选中的当前位置

            int findPos = txtText.Text.IndexOf("aa", currPos);
            if(findPos != -1)
            {
                txtText.Select(findPos, 2);
            }
        }

        private void 字体ToolStripMenuItem_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_FormClosed(object sender, FormClosedEventArgs e)
        {
            //关闭后创建配置文件,记录文档的位置信息
            int x = this.Location.X;
            int y = this.Location.Y;
            int w = this.Size.Width;
            int h = this.Size.Height;

            //此时自动创建文件流对象
            StreamWriter sw = new StreamWriter(Application.StartupPath + "\app.dll", false);
            sw.WriteLine(x.ToString());
            sw.WriteLine(y.ToString());
            sw.WriteLine(w.ToString());
            sw.WriteLine(h.ToString());

            sw.Close();
        }

        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());

                this.Location = new Point(x, y);
                this.Size = new Size(w, h);
                sr.Close();
            }
        }
    }
}

  

原文地址:https://www.cnblogs.com/my-cat/p/7285516.html