手写代码--->模仿记事本

来源:互联网 发布:淘宝手机图片尺寸 编辑:程序博客网 时间:2024/05/21 05:42

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace _5._29记事本
{
    public partial class Form1 : Form
    {
        bool IsTextChanged = false;
        string TextFileName = "";
        public Form1()
        {
         
            InitializeComponent();
        }

        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtTest.Text = "";
            IsTextChanged = false;
            TextFileName = "";//////认为它是没有名字的
        }

        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "打开文件";
            ofd.Filter = "文本文件|*.txt|所有文件|*.*";
           // ofd.InitialDirectory="c:\\";
            ofd.ShowDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
               
               //txtTest.Text=File.ReadAllLines(ofd.FileName);
                ///第一步:定义一个文件流
                FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read,FileShare.ReadWrite);
                ////第二步:创建一个读取器
                StreamReader sr = new StreamReader(fs,Encoding.Default);
               //////第三步:sr.ReadLine();////读取一行
                 while(sr.EndOfStream==false)
                {
                    string line = sr.ReadLine();///读取一行
                    txtTest.Text = txtTest.Text +line+"\r\n";
                }
                string TextFileName = ofd.FileName;
                //txtTest.Text=sr.ReadToEnd();/////读取到最(可以读全部)
                /////第四步:关闭读取器
                sr.Close();
                ////第五步:关闭文件流
                fs.Close();
                IsTextChanged = false;//////人写代码时才把false改为true
            }
        }

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

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

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

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(IsTextChanged==true)
            {
               DialogResult re= MessageBox.Show("文本内容已经改变,是否保存?","询问",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question);
                if(re==DialogResult.OK)
                {
                    ///要保存
                    SaveText();
                }
                else if (re == 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",false);
            sw.WriteLine(x.ToString());
            sw.WriteLine(y.ToString());
            sw.WriteLine(w.ToString());
            sw.WriteLine(h.ToString());
            sw.WriteLine(txtTest.ForeColor.R);
            sw.WriteLine(txtTest.ForeColor.G);
            sw.WriteLine(txtTest.ForeColor.B);
            sw.Close();
        }
        protected void SaveText()
        {
            if (TextFileName == "")
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Title = "打开文件";
                sfd.Filter = "文本文件|*.txt|所有文件|*.*";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    //第一步:声明文件流对象
                    FileStream fs = new FileStream(sfd.FileName, FileMode.Create);
                    //////第二步:创建写入器
                    StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                    //////第三步:写操作
                    sw.Write(txtTest.Text);
                    /////第四步:关闭写入器
                    sw.Close();
                    ////// 第五步:关闭文件流
                    fs.Close();
                    IsTextChanged = false;
                    TextFileName = sfd.FileName;
                }
            }
            else
            {
                //第一步:声明文件流对象
                FileStream fs = new FileStream(TextFileName, FileMode.Create);
                //////第二步:创建写入器
                StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                //////第三步:写操作
                sw.Write(txtTest.Text);
                /////第四步:关闭写入器
                sw.Close();
                ////// 第五步:关闭文件流
                fs.Close();
                IsTextChanged = false;
            }
        }

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

        private void 剪贴TToolStripMenuItem_Click(object sender, EventArgs e)
        {
            txtTest.Cut();
        }

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

        private void 查找FToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int currPos = txtTest.SelectionStart;
            int findPos = txtTest.Text.IndexOf("中国", currPos);
            if (findPos != -1)
            {
                ///////说明找到了
                txtTest.Select(findPos, 2);
            }
        }

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

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

        private void 背景颜色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ColorDialog cd = new ColorDialog();
            cd.Color = txtTest.BackColor;
            if (cd.ShowDialog() == DialogResult.OK)
            {
                txtTest.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);
                txtTest.ForeColor = Color.FromArgb(r,g,b);
                sr.Close();
            }
        }
    }
}
(5月30号,有待完善!)