记事本

界面

后台代码:

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

namespace 对话框控件
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        //自定义换行
        private void 自定义CToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (textBox1.WordWrap)
            {
                textBox1.WordWrap = false;
                textBox1.ScrollBars = ScrollBars.Both;
                自定义CToolStripMenuItem.Text = "自动换行";
            }
            else
            {
                textBox1.WordWrap = true;
                textBox1.ScrollBars = ScrollBars.Vertical;
                自定义CToolStripMenuItem.Text = "取消自动换行";
            }
        }
        //设置字体与颜色
        private void 选项OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult f = fontDialog1.ShowDialog();
            if (f == DialogResult.OK)
            {
                textBox1.Font = fontDialog1.Font;
                textBox1.ForeColor = fontDialog1.Color;
            }
        }
        //复制
        private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.Copy();
        }
        //粘贴
        private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.Paste();
        }
        //全选
        private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.SelectAll();
        }
        //剪切
        private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.Cut();
        }
        private string FileName;
        private string chushi;
        //打开
        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = openFileDialog1.ShowDialog();
            openFileDialog1.Filter = "文本文件|*.txt;*.avi;*.jpg";//打开文件的格式
            if (dr == DialogResult.OK)//判断是否点击打开开按钮
            {
                string filename = openFileDialog1.FileName;//获取文件的路径
                FileName = filename;
                //通过读入流进行文件读取
                StreamReader sr = new StreamReader(filename, Encoding.Default);//流的读取  流只是一个通道
                textBox1.Text = sr.ReadToEnd();//从头读到尾
                sr.Close();//必须要关闭流
                chushi = textBox1.Text;
            }
        }
        //保存
        private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "文本文件|*.txt";
            if (FileName == null)//新建的文本文件  弹出对话框
            {
                DialogResult dr = saveFileDialog1.ShowDialog();
                if (dr == DialogResult.OK)//说明点击保存了
                {
                    string filename = saveFileDialog1.FileName;//获取路径
                    //写入流,可以在硬盘上创建文件,并为文件写入信息
                    StreamWriter sw = new StreamWriter(filename);//流写写入 new建取一个缓存区
                    sw.Write(this.textBox1.Text);
                    sw.Close();
                }
            }
            else
            {
                //写入流,可以在硬盘上创建文件,并为文件写入信息
                StreamWriter sw = new StreamWriter(FileName);
                sw.Write(this.textBox1.Text);
                sw.Close();
                bianhua = this.textBox1.Text.Substring(chushi.Length);
            }
        }
        //打印
        private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = printDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                printDocument1.Print();//打印的对象
            }
        }
        //打印预览
        private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e)
        {   
            printPreviewDialog1.Document = printDocument1;
            printPreviewDialog1.ShowDialog();
        }
        //字符统计
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            label2.Text = textBox1.Text.Length.ToString();
        }
        private string bianhua;//成员变量 如果是保存的文本选中 不是撤销
        private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.Undo();
        }
        //新建
        private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text.Length > 0)
            {
                DialogResult drg = MessageBox.Show("是否进行保存?", "保存对话框", MessageBoxButtons.YesNo);
                if (DialogResult.Yes == drg)
                {
                    if (FileName == null)
                    {
                        DialogResult dr = saveFileDialog1.ShowDialog();
                        if (dr == DialogResult.OK)
                        {
                            string filename = saveFileDialog1.FileName;
                            //写入流,可以在硬盘上创建文件,并为文件写入信息
                            StreamWriter sw = new StreamWriter(filename);
                            sw.Write(textBox1.Text);
                            sw.Close();
                        }
                    }
                    else
                    {
                        //写入流,可以在硬盘上创建文件,并为文件写入信息
                        StreamWriter sw = new StreamWriter(FileName);
                        sw.Write(this.textBox1.Text);
                        sw.Close();
                    }
                }
            }
            FileName = null;
            this.textBox1.Text = "";
        }
        //另存为
        private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "文本文件(*.txt)|*.txt|word文件(*.doc)|*.doc";
            DialogResult dr = saveFileDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                string filename = saveFileDialog1.FileName;
                //写入流,可以在硬盘上创建文件,并为文件写入信息
                StreamWriter sw = new StreamWriter(filename);
                sw.Write(this.textBox1.Text);
                sw.Close();
            }
        }
        //绘制
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)//事件//e事件数据  就是printDocument1_PrintPage事件
        {
            Font f = new Font("宋体", 38);//字体与字体的大小
            Brush b = new SolidBrush(Color.Black);//画刷样式及其颜色
            e.Graphics.DrawString(textBox1.Text, f, b, 0, 0);
        }
        //页面设置
        private void 页面设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pageSetupDialog1.Document = printDocument1;//为页面设置对话框指定打印对象   连接
            pageSetupDialog1.ShowDialog();//打开页面对话框
        }
    }
}
posted @ 2016-09-11 08:52  露西&哈特菲利亚  阅读(154)  评论(0编辑  收藏  举报