超级记事本

本文介绍了一款增强型记事本应用的功能实现细节,包括文件操作、文本编辑、外观定制及外部应用程序调用等功能。文章通过具体代码展示了如何使用 C# 实现这些功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里写图片描述
这里写图片描述

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


namespace 超级记事本
{
    public partial class Form1 : Form
    {
        ColorDialog colordig = new ColorDialog();
        private string txtFileName = "";
        private string filePath = "";
        public Form1()
        {
            InitializeComponent();
            this.skinEngine1.SkinFile = ".Skins/SteelBlack.ssk";//加载皮肤控件
        }
        // 用于存储当前操作的文件的名称

        MoreTools con = new MoreTools();//加载工具类

        // “保存”
        private void Save()
        {
            if (!txtFileName.Equals(""))
            {
                SaveFileDialog saveFile = new SaveFileDialog();
                // 默认保存格式
                saveFile.Filter = "文本文件(*.txt)|*.txt";
                StreamWriter sw = new StreamWriter(filePath, false);
                sw.Write(inputInfo.Text);
                sw.Close();

                MessageBox.Show("文件保存成功!", "温馨提示");
                filePath = saveFile.FileName;
                // 把标题改为打开的文件的名称
                Text = txtFileName + "-记事本";
            }
            else
            {
                // 成员变量为“”,说明文件第一次保存,执行“另存为”操作
                HoldFile();
            }
        }

        private void HoldFile()
        {
            // 若用户选择另保存文件
            SaveFileDialog saveFile = new SaveFileDialog();
            // 默认保存格式
            saveFile.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            if (saveFile.ShowDialog() == DialogResult.OK)
            {
                StreamWriter sw = new StreamWriter(saveFile.FileName, false);
                sw.WriteLine(inputInfo.Text);
                sw.Close();
                MessageBox.Show("文件保存成功!", "温馨提示");
                filePath = saveFile.FileName;
            }
            // 判断是第一次保存还是第二次
            if (txtFileName.Equals(""))
            {
                FileInfo fileInfo = new FileInfo(saveFile.FileName);
                Text = fileInfo.Name + "记事本";
                txtFileName = fileInfo.Name;
            }
            else
            {
                // 把标题改为打开的文件的名称
                Text = txtFileName + "记事本";
            }
        }



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

        private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.inputInfo.SelectAll();
            //全选_Click(sender,e);
        }

        private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                StreamReader sr = new StreamReader(openFile.FileName);
                inputInfo.Text = sr.ReadToEnd();
                sr.Close();
                FileInfo fileInfo = new FileInfo(openFile.FileName);
                // 把标题改为打开的文件的名称
                Text = "*" + fileInfo.Name + "超级记事本";
                txtFileName = fileInfo.Name;
            }
        }      
     private void 页面设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                this.pageSetupDialog1 = new PageSetupDialog();
                pageSetupDialog1.ShowDialog();
                pageSetupDialog1.ShowHelp.ToString();
            }
            catch (Exception)
            {
                con.notePad();


            }
            finally
            {
                MessageBox.Show("不能设置");

            }
        }

        private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (inputInfo.Text.Length < 1)
            {
                MessageBox.Show("请确保要打印的文件的内容不为空!", "温馨提示");
                return;
            }
            else
            {
                // 设置Document的属性
                this.printDialog1.Document = this.printDocument1;
                this.printDialog1.PrinterSettings = this.pageSetupDialog1.PrinterSettings;
                if (this.printDialog1.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        this.printDocument1.Print();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // 退出时应提示用户是否保存当前文本文件
            DialogResult result = MessageBox.Show("是否将更改保存?", "Mickey温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
            if (result == DialogResult.Yes)
            {
                Save();
                Application.Exit();
            }
            else if (result == DialogResult.No)
            {
                Application.Exit();
            }
        }
        private string selectedInfo = "";
        private void 编辑EToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if ((inputInfo.SelectedText.Equals("")) && (selectedInfo.Equals("")))
            {
                剪切ToolStripMenuItem.Enabled = false;
                复制ToolStripMenuItem.Enabled = false;
                粘贴ToolStripMenuItem.Enabled = false;
                删除ToolStripMenuItem.Enabled = false;
            }
            else
            {
                剪切ToolStripMenuItem.Enabled = true;
                复制ToolStripMenuItem.Enabled = true;
                粘贴ToolStripMenuItem.Enabled = true;
                删除ToolStripMenuItem.Enabled = true;
            }
        }

        private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.inputInfo.Undo();
        }

        private void 剪切ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            selectedInfo = inputInfo.SelectedText;
            this.inputInfo.Cut();
        }

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

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
             this.inputInfo.SelectedText = "";
        }

        private void 查找ToolStripMenuItem_Click(object sender, EventArgs e)
        {
             if (inputInfo.Text == string.Empty)
            {
                MessageBox.Show("请确保要查找的文件的内容不为空!", "Mickey温馨提示");
            }

        }

        private void 时间日期ToolStripMenuItem_Click(object sender, EventArgs e)
        {
             inputInfo.Text += "现在时间是:" + DateTime.Now.ToString();
        }

        private void 自动换行WToolStripMenuItem_Click(object sender, EventArgs e)
        {
             if (自动换行WToolStripMenuItem.Checked == true)
            {
                inputInfo.WordWrap = true;
            }
            else
            {
                inputInfo.WordWrap = false;
            }
        }

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

        }

        private void 查看VToolStripMenuItem_Click(object sender, EventArgs e)
        {
             if (inputInfo.Text.Length > 0)
            {
                状态栏ToolStripMenuItem.Enabled = true;
            }
            else
            {
                状态栏ToolStripMenuItem.Enabled = false;
            }
        }

        private void 状态栏ToolStripMenuItem_Click(object sender, EventArgs e)
        {
             if (状态栏ToolStripMenuItem.Checked == true)
            {
                状态栏ToolStripMenuItem.Checked = false;
                statusStrip1.Visible = false;
            }
            else
            {
                状态栏ToolStripMenuItem.Checked = true;
                //statusStrip1ToolStripMenuItem.Visible = true;
            }
        }

        private void 查看帮助ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string help = @"help.txt";
            Help.ShowHelp(this, help);
        }

        private void 关于记事本AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 about = new AboutBox1();
            about.Show();
        }

        private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
        {

            // 新建一个文本时,若输入框中的内容不为空,应先提示“是否保存”
            if (inputInfo.Text != string.Empty)
            {
                // 若用户选择“是”,应弹出保存文件的对话框
                if (MessageBox.Show("是否保存当前文件?", "温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information)
                    == DialogResult.Yes)
                {
                    // 保存文件
                    Save();
                    Text = "新建-超级记事本";
                    inputInfo.Text = "";
                }
                  else if (MessageBox.Show("是否保存当前文件?", "温馨提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information)
                    == DialogResult.No)
                {
                    // 用户选择不保存时将输入框中的内容清除
                    inputInfo.Text = "";
                }
            }
        }

        private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!txtFileName.Equals(""))
            {
                SaveFileDialog saveFile = new SaveFileDialog();
                // 默认保存格式
                saveFile.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
                StreamWriter sw = new StreamWriter(filePath, false);
                sw.Write(inputInfo.Text);
                sw.Close();

                MessageBox.Show("文件保存成功!", "温馨提示");
                filePath = saveFile.FileName;
                // 把标题改为打开的文件的名称
                Text = txtFileName + "记事本";
            }
            else
            {
                // 成员变量为“”,说明文件第一次保存,执行“另存为”操作
                HoldFile();
            }
        }

        private void 颜色设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {


            if (colordig.ShowDialog() == DialogResult.OK)
            {
                inputInfo.ForeColor = colordig.Color;
            }
        }

        private void 背景颜色BToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (colordig.ShowDialog() == DialogResult.OK)
            {
                inputInfo.BackColor = colordig.Color;
            }
        }

        private void 计算器CToolStripMenuItem_Click(object sender, EventArgs e)
        {
            con.CALC();//计算器
        }

        private void 画图PToolStripMenuItem_Click(object sender, EventArgs e)
        {
            con.MSPAINT();//画图1
        }

        private void 记事本ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            con.notePad();//记事本
        }

        private void 运行RToolStripMenuItem_Click(object sender, EventArgs e)
        {

            con._run();//运行
        }

        private void 控制面板KToolStripMenuItem_Click(object sender, EventArgs e)
        {
            con._control();//控制面板
        }

        private void 组策略ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            con.Write_();//写字板

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Text = DateTime.Now.ToString();
        }








        }
    }

外部类

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 超级记事本
{
   public class MoreTools
    {
       /// <summary>
       /// 控制面板
       /// </summary>
       public void _control()
       {

           System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();
           Info.FileName = "control.exe ";//"calc.exe"为计算器,"notepad.exe"为记事本
            System.Diagnostics.Process.Start(Info); 
       }
       /// <summary>
       /// 命令窗口
       /// </summary>
       public void _run() {
           System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();
           Info.FileName = "cmd.exe ";//"calc.exe"为计算器,"notepad.exe"为记事本
          System.Diagnostics.Process.Start(Info);

       }
       /// <summary>
       /// 记事本
       /// </summary>
       public void notePad() {
           System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();
           Info.FileName = "notepad.exe ";//"calc.exe"为计算器,"notepad.exe"为记事本
           System.Diagnostics.Process.Start(Info);

       }
       /// <summary>
       /// 画图
       /// </summary>
       public void MSPAINT() {

           System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();
           Info.FileName = "mspaint.exe ";//"calc.exe"为计算器,"notepad.exe"为记事本
           System.Diagnostics.Process.Start(Info);
       }
       /// <summary>
       /// 计算器
       /// </summary>
       public void CALC() {


           System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();
           Info.FileName = "calc.exe ";//"calc.exe"为计算器,"notepad.exe"为记事本
          System.Diagnostics.Process.Start(Info);

       }
       public void Write_() {

           System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();
           Info.FileName = "write.exe ";//"calc.exe"为计算器,"notepad.exe"为记事本
         System.Diagnostics.Process.Start(Info);

       }


       }

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值