记事本(改进但还不完善)

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 _6._30_对话框
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();  //关闭窗口
        }

        private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.Undo();  //撤销textBox1中的上一个操作

        }

        private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.Cut();   //剪切--将textBox1中所选内容移动到剪切板中
        }

        private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.Copy();     //复制--将textBox1中所选内容复制到剪切板中
        }

        private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.Paste();     //粘贴--用剪切板中的内容替换textBox1中所选内容
        }

        private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.SelectAll();  //选择textBox1中全部内容
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string num = textBox1.TextLength.ToString();    //textBox1中的字数
            zishu.Text = num;    //字数显示
        }

        private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fontDialog1.ShowColor = true;  //   显示颜色
            fontDialog1.ShowDialog();        //  打开
            textBox1.Font = fontDialog1.Font;  // 字体改变
            textBox1.ForeColor = fontDialog1.Color; // 颜色改变
        }

        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "文本文件|*.txt"; //限制显示打开的文件类型
            DialogResult dr = openFileDialog1.ShowDialog();

            if (dr == DialogResult.OK)    //点击确定
            {
                StreamReader sr = new StreamReader(openFileDialog1.FileName,UnicodeEncoding.GetEncoding("GB2312"));
                textBox1.Text = sr.ReadToEnd();
                sr.Close();   //关闭流通道
            }
        }
        string path = "";
        private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (path == "")   // 是否已保存过
            {
                saveFileDialog1.FileName = "新建文本文件.txt";
                saveFileDialog1.ShowDialog();
                path = saveFileDialog1.FileName;
            }
            StreamWriter sw = new StreamWriter(path);
            sw.Write(textBox1.Text);
            sw.Close();
        }

        private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.FileName = "新建文本文件.txt";
            saveFileDialog1.ShowDialog();
            path = saveFileDialog1.FileName;
            StreamWriter sw = new StreamWriter(path);
            sw.Write(textBox1.Text);
            sw.Close();
        }

        private void 打印设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pageSetupDialog1.Document = printDocument1; //要打印设置对象是 printDocument1
            pageSetupDialog1.ShowDialog();  //打开对话框
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {    //打印对象
            Font f = new Font("宋体", 14);     // 字体格式
            Brush b = new SolidBrush(Color.Black);  // 填充的前景色为黑色
            PointF p = new PointF(10, 10);      // 起始打印位置

            e.Graphics.DrawString(textBox1.Text, f, b, p);
           // e.Graphics.DrawString(要绘制的字符串,字符串的文本格式,绘制文本的颜色和纹理,绘制文本的左上角位置)
        }

        private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e)
        {
            printPreviewDialog1.Document = printDocument1;  //要打印预览对象是 printDocument1
            printPreviewDialog1.ShowDialog();
        }

        private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
        {
            printDialog1.Document = printDocument1;   //要打印对象是 printDocument1
            printDialog1.ShowDialog();
        }
    }
}

原文地址:https://www.cnblogs.com/a454966933/p/5630400.html