作业真是一次比一次难! 这次作业估摸写了8、9个小时。

作业要求: 高仿Windows自带记事本;

1. 菜单栏;

2. 编码;

3. 图标;

4. 右键菜单栏;

5. 快捷键;

6. 状态栏;

7. ……

下载地址:Notepad

上图:

主要代码:

Form1.cs

    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 Notepad
    {
    public partial class Form1 : Form
    {
    private string textFileName;
    private string filePath;
    public string findWord;
    public bool checkbox;
    public bool radiobutton;
    public int iFound;
    public Form1()
    {
    InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
    checkbox = false;
    radiobutton = false;
    textFileName = "";
    filePath = "";
    findWord = "";
    撤销UToolStripMenuItem.Enabled = richTextBox1.CanUndo;
    if (richTextBox1.SelectedText.Length == 0)
    {
    剪切ToolStripMenuItem.Enabled = false;
    复制CToolStripMenuItem.Enabled = false;
    }
    else
    {
    复制CToolStripMenuItem.Enabled = true;
    剪切ToolStripMenuItem.Enabled = true;
    }
    粘贴PToolStripMenuItem.Enabled = true;
    if (richTextBox1.SelectedText.Length == 0)
    {
    删除LToolStripMenuItem.Enabled = false;
    }
    else
    {
    删除LToolStripMenuItem.Enabled = true;
    }
    撤销UToolStripMenuItem1.Enabled = richTextBox1.CanUndo;
    if (richTextBox1.SelectedText.Length == 0)
    {
    剪切TToolStripMenuItem.Enabled = false;
    复制CToolStripMenuItem1.Enabled = false;
    }
    else
    {
    复制CToolStripMenuItem1.Enabled = true;
    剪切TToolStripMenuItem.Enabled = true;
    }
    粘贴PToolStripMenuItem1.Enabled = true;
    if (richTextBox1.SelectedText.Length == 0)
    {
    删除DToolStripMenuItem.Enabled = false;
    }
    else
    {
    删除DToolStripMenuItem.Enabled = true;
    }
    textFileName = "无标题";
    Text = textFileName + " - 记事本";
    自动换行ToolStripMenuItem.Checked = true;
    state(sender, e);
    状态栏SToolStripMenuItem.Checked = true;
    statusStrip1.Show();
    }
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
    if (Text[0] == '*')
    {
    DialogResult dr = MessageBox.Show("是否将更改保存到 " + textFileName + "?", "记事本", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
    if (dr == DialogResult.Yes)
    {
    保存SToolStripMenuItem_Click(sender, e);
    }
    if (dr == DialogResult.Cancel)
    {
    e.Cancel = true;
    }
    }
    }
    private void richTextBox1_TextChanged(object sender, EventArgs e)
    {
    if (Text[0] != '*')
    {
    Text = "* " + Text;
    }
    撤销UToolStripMenuItem.Enabled = richTextBox1.CanUndo;
    if (richTextBox1.SelectedText.Length == 0)
    {
    剪切ToolStripMenuItem.Enabled = false;
    复制CToolStripMenuItem.Enabled = false;
    }
    else
    {
    复制CToolStripMenuItem.Enabled = true;
    剪切ToolStripMenuItem.Enabled = true;
    }
    粘贴PToolStripMenuItem.Enabled = true;
    if (richTextBox1.SelectedText.Length == 0)
    {
    删除LToolStripMenuItem.Enabled = false;
    }
    else
    {
    删除LToolStripMenuItem.Enabled = true;
    }
    撤销UToolStripMenuItem1.Enabled = richTextBox1.CanUndo;
    if (richTextBox1.SelectedText.Length == 0)
    {
    剪切TToolStripMenuItem.Enabled = false;
    复制CToolStripMenuItem1.Enabled = false;
    }
    else
    {
    复制CToolStripMenuItem1.Enabled = true;
    剪切TToolStripMenuItem.Enabled = true;
    }
    粘贴PToolStripMenuItem1.Enabled = true;
    if (richTextBox1.SelectedText.Length == 0)
    {
    删除DToolStripMenuItem.Enabled = false;
    }
    else
    {
    删除DToolStripMenuItem.Enabled = true;
    }
    }
    private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)
    {
    撤销UToolStripMenuItem.Enabled = richTextBox1.CanUndo;
    if (richTextBox1.SelectedText.Length == 0)
    {
    剪切ToolStripMenuItem.Enabled = false;
    复制CToolStripMenuItem.Enabled = false;
    }
    else
    {
    复制CToolStripMenuItem.Enabled = true;
    剪切ToolStripMenuItem.Enabled = true;
    }
    粘贴PToolStripMenuItem.Enabled = true;
    if (richTextBox1.SelectedText.Length == 0)
    {
    删除LToolStripMenuItem.Enabled = false;
    }
    else
    {
    删除LToolStripMenuItem.Enabled = true;
    }
    撤销UToolStripMenuItem1.Enabled = richTextBox1.CanUndo;
    if (richTextBox1.SelectedText.Length == 0)
    {
    剪切TToolStripMenuItem.Enabled = false;
    复制CToolStripMenuItem1.Enabled = false;
    }
    else
    {
    复制CToolStripMenuItem1.Enabled = true;
    剪切TToolStripMenuItem.Enabled = true;
    }
    粘贴PToolStripMenuItem1.Enabled = true;
    if (richTextBox1.SelectedText.Length == 0)
    {
    删除DToolStripMenuItem.Enabled = false;
    }
    else
    {
    删除DToolStripMenuItem.Enabled = true;
    }
    }
    private void 关于作者MToolStripMenuItem_Click(object sender, EventArgs e)
    {
    MessageBox.Show("林海鸿\n学号:13055712\n邮箱:haipzm@gmail.com\n网站:http://haipz.com/", "作者", MessageBoxButtons.OK, MessageBoxIcon.None);
    }
    #region 文件
    private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
    {
    if (Text[0] == '*')
    {
    DialogResult dr = MessageBox.Show("是否将更改保存到 " + textFileName + "?", "记事本", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
    if (dr == DialogResult.Yes)
    {
    保存SToolStripMenuItem_Click(sender, e);
    richTextBox1.Text = "";
    }
    if (dr == DialogResult.No)
    {
    richTextBox1.Text = "";
    }
    if (dr != DialogResult.Cancel)
    {
    textFileName = "无标题";
    Text = textFileName + " - 记事本";
    filePath = "";
    }
    }
    }
    private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
    {
    if (!filePath.Equals(""))
    {
    richTextBox1.SaveFile(filePath, RichTextBoxStreamType.PlainText);
    Text = textFileName + " - 记事本";
    }
    else
    另存为ToolStripMenuItem_Click(sender, e);
    }
    private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
    {
    SaveFileDialog saveFile = new SaveFileDialog();
    saveFile.Filter = "文本文件(*.txt)|*.txt";
    if (saveFile.ShowDialog() == DialogResult.OK)
    {
    filePath = saveFile.FileName;
    FileInfo fileInfo = new FileInfo(saveFile.FileName);
    textFileName = fileInfo.Name;
    Text = textFileName + " - 记事本";
    richTextBox1.SaveFile(filePath, RichTextBoxStreamType.PlainText);
    }
    }
    private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
    {
    OpenFileDialog openFile = new OpenFileDialog();
    openFile.Filter = "文本文件(*.txt)|*.txt";
    if (openFile.ShowDialog() == DialogResult.OK)
    {
    filePath = openFile.FileName;
    FileInfo fileInfo = new FileInfo(openFile.FileName);
    textFileName = fileInfo.Name;
    Text = textFileName + " - 记事本";
    richTextBox1.LoadFile(filePath, RichTextBoxStreamType.PlainText);
    }
    }
    private void 页面设置UToolStripMenuItem_Click(object sender, EventArgs e)
    {
    pageSetupDialog1.Document = printDocument1;
    pageSetupDialog1.ShowDialog();
    }
    private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
    {
    printDialog1.Document = printDocument1;
    printDialog1.PrinterSettings = pageSetupDialog1.PrinterSettings;
    if (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)
    {
    Application.Exit();
    }
    #endregion
    #region 编辑
    private void 撤销UToolStripMenuItem_Click(object sender, EventArgs e)
    {
    richTextBox1.Undo();
    }
    private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
    {
    richTextBox1.Cut();
    }
    private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
    {
    richTextBox1.Copy();
    }
    private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
    {
    richTextBox1.Paste();
    }
    private void 删除LToolStripMenuItem_Click(object sender, EventArgs e)
    {
    richTextBox1.SelectedText = "";
    }
    private void 查找FToolStripMenuItem_Click(object sender, EventArgs e)
    {
    Form2 f2 = new Form2();
    f2.Owner = this;
    f2.Show();
    }
    private void 查找下一个NToolStripMenuItem_Click(object sender, EventArgs e)
    {
    if (findWord == "")
    {
    Form2 f2 = new Form2();
    f2.Owner = this;
    f2.Show();
    }
    else
    {
    if (radiobutton)
    {
    if (iFound == -1) iFound = richTextBox1.TextLength + 1;
    if (checkbox)
    {
    iFound = richTextBox1.Find(findWord, 0, iFound, RichTextBoxFinds.Reverse & RichTextBoxFinds.MatchCase);
    }
    else
    {
    iFound = richTextBox1.Find(findWord, 0, iFound, RichTextBoxFinds.Reverse);
    }
    }
    else
    {
    if (checkbox)
    {
    iFound = richTextBox1.Find(findWord, iFound + 1, richTextBox1.TextLength, RichTextBoxFinds.MatchCase);
    }
    else
    {
    iFound = richTextBox1.Find(findWord, iFound + 1, richTextBox1.TextLength, RichTextBoxFinds.None);
    }
    }
    if (iFound == -1)
    {
    MessageBox.Show("找不到 \"" + findWord + " \"", "查找", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    }
    }
    private void 替换RToolStripMenuItem_Click(object sender, EventArgs e)
    {
    Form2 f4 = new Form2();
    f4.Owner = this;
    f4.Show();
    }
    private void 转到GToolStripMenuItem_Click(object sender, EventArgs e)
    {
    Form2 f4 = new Form2();
    f4.Owner = this;
    f4.Show();
    }
    private void 时间日期DToolStripMenuItem_Click(object sender, EventArgs e)
    {
    DateTime dt = DateTime.Now;
    richTextBox1.AppendText(dt.Date.ToShortDateString() + " " + dt.TimeOfDay.ToString(@"hh\:mm\:ss"));
    }
    private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
    {
    richTextBox1.SelectAll();
    }
    #endregion
    #region 格式
    private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
    {
    if (自动换行ToolStripMenuItem.Checked == false)
    {
    自动换行ToolStripMenuItem.Checked = true;
    }
    else
    {
    自动换行ToolStripMenuItem.Checked = false;
    }
    richTextBox1.WordWrap = 自动换行ToolStripMenuItem.Checked;
    }
    private void 字体FToolStripMenuItem_Click(object sender, EventArgs e)
    {
    if (fontDialog1.ShowDialog() == DialogResult.OK)
    {
    richTextBox1.Font = fontDialog1.Font;
    }
    }
    #endregion
    #region 查看
    private void 状态栏SToolStripMenuItem_Click(object sender, EventArgs e)
    {
    if (状态栏SToolStripMenuItem.Checked == false)
    {
    状态栏SToolStripMenuItem.Checked = true;
    }
    else
    {
    状态栏SToolStripMenuItem.Checked = false;
    }
    if (状态栏SToolStripMenuItem.Checked)
    {
    statusStrip1.Visible = true;
    }
    else
    {
    statusStrip1.Visible = false;
    }
    }
    private void richTextBox1_SelectionChanged(object sender, EventArgs e)
    {
    state(sender, e);
    }
    private void state(object sender, EventArgs e)
    {
    int line = this.richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart);
    int col;
    int start = 0;
    int cursor = richTextBox1.SelectionStart;
    while (start < cursor)
    {
    if (line == this.richTextBox1.GetLineFromCharIndex(start))
    {
    break;
    }
    else
    start++;
    }
    col = cursor - start;
    line++;
    col++;
    toolStripStatusLabel1.Text = "行 " + line;
    toolStripStatusLabel2.Text = "列 " + col;
    }
    #endregion
    #region 帮助
    private void 查看帮助ToolStripMenuItem_Click(object sender, EventArgs e)
    {
    System.Diagnostics.Process.Start("winhlp32");
    }
    private void 关于记事本AToolStripMenuItem_Click(object sender, EventArgs e)
    {
    System.Diagnostics.Process.Start("winver");
    }
    #endregion
    #region 右键
    private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
    {
    撤销UToolStripMenuItem.Enabled = richTextBox1.CanUndo;
    if (richTextBox1.SelectedText.Length == 0)
    {
    剪切ToolStripMenuItem.Enabled = false;
    复制CToolStripMenuItem.Enabled = false;
    }
    else
    {
    复制CToolStripMenuItem.Enabled = true;
    剪切ToolStripMenuItem.Enabled = true;
    }
    粘贴PToolStripMenuItem.Enabled = true;
    if (richTextBox1.SelectedText.Length == 0)
    {
    删除LToolStripMenuItem.Enabled = false;
    }
    else
    {
    删除LToolStripMenuItem.Enabled = true;
    }
    撤销UToolStripMenuItem1.Enabled = richTextBox1.CanUndo;
    if (richTextBox1.SelectedText.Length == 0)
    {
    剪切TToolStripMenuItem.Enabled = false;
    复制CToolStripMenuItem1.Enabled = false;
    }
    else
    {
    复制CToolStripMenuItem1.Enabled = true;
    剪切TToolStripMenuItem.Enabled = true;
    }
    粘贴PToolStripMenuItem1.Enabled = true;
    if (richTextBox1.SelectedText.Length == 0)
    {
    删除DToolStripMenuItem.Enabled = false;
    }
    else
    {
    删除DToolStripMenuItem.Enabled = true;
    }
    }
    private void 撤销UToolStripMenuItem1_Click(object sender, EventArgs e)
    {
    撤销UToolStripMenuItem_Click(sender, e);
    }
    private void 剪切TToolStripMenuItem_Click_1(object sender, EventArgs e)
    {
    剪切TToolStripMenuItem_Click(sender, e);
    }
    private void 复制CToolStripMenuItem1_Click(object sender, EventArgs e)
    {
    复制CToolStripMenuItem_Click(sender, e);
    }
    private void 粘贴PToolStripMenuItem1_Click(object sender, EventArgs e)
    {
    粘贴PToolStripMenuItem_Click(sender, e);
    }
    private void 删除DToolStripMenuItem_Click(object sender, EventArgs e)
    {
    删除DToolStripMenuItem_Click(sender, e);
    }
    private void 全选AToolStripMenuItem1_Click(object sender, EventArgs e)
    {
    全选AToolStripMenuItem_Click(sender, e);
    }
    #endregion
    }
    }

Form2.cs

    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;
    namespace Notepad
    {
    public partial class Form2 : Form
    {
    Form1 f1;
    public Form2()
    {
    InitializeComponent();
    }
    private void Form2_Load(object sender, EventArgs e)
    {
    f1 = (Form1)Owner;
    button1.Enabled = false;
    f1.iFound = -1;
    }
    private void button2_Click(object sender, EventArgs e)
    {
    this.Close();
    }
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
    if (textBox1.TextLength == 0)
    {
    button1.Enabled = false;
    }
    else
    {
    button1.Enabled = true;
    }
    }
    public void button1_Click(object sender, EventArgs e)
    {
    f1.findWord = textBox1.Text;
    f1.checkbox = checkBox1.Checked;
    f1.radiobutton = radioButton1.Checked;
    if (radioButton1.Checked)
    {
    if (f1.iFound == -1) f1.iFound = f1.richTextBox1.TextLength + 1;
    if (checkBox1.Checked)
    {
    f1.iFound = f1.richTextBox1.Find(f1.findWord, 0, f1.iFound, RichTextBoxFinds.Reverse & RichTextBoxFinds.MatchCase);
    }
    else
    {
    f1.iFound = f1.richTextBox1.Find(f1.findWord, 0, f1.iFound, RichTextBoxFinds.Reverse);
    }
    }
    else
    {
    if (checkBox1.Checked)
    {
    f1.iFound = f1.richTextBox1.Find(f1.findWord, f1.iFound + 1, f1.richTextBox1.TextLength, RichTextBoxFinds.MatchCase);
    }
    else
    {
    f1.iFound = f1.richTextBox1.Find(f1.findWord, f1.iFound + 1, f1.richTextBox1.TextLength, RichTextBoxFinds.None);
    }
    }
    if (f1.iFound == -1)
    {
    MessageBox.Show("找不到 \"" + f1.findWord + " \"", "查找", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    f1.Focus();
    }
    }
    }

Form3.cs

    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;
    namespace Notepad
    {
    public partial class Form3 : Form
    {
    int iFound;
    Form1 f1;
    public Form3()
    {
    InitializeComponent();
    }
    private void Form3_Load(object sender, EventArgs e)
    {
    f1 = (Form1)Owner;
    iFound = -1;
    button1.Enabled = false;
    button2.Enabled = false;
    button3.Enabled = false;
    }
    private void button1_Click(object sender, EventArgs e)
    {
    string findWord = textBox1.Text;
    if (checkBox1.Checked)
    {
    iFound = f1.richTextBox1.Find(findWord, iFound + 1, f1.richTextBox1.TextLength, RichTextBoxFinds.MatchCase);
    }
    else
    {
    iFound = f1.richTextBox1.Find(findWord, iFound + 1, f1.richTextBox1.TextLength, RichTextBoxFinds.None);
    }
    if (iFound == -1)
    {
    MessageBox.Show("找不到 \"" + findWord + " \"", "查找", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    f1.Focus();
    }
    private void button2_Click(object sender, EventArgs e)
    {
    string changeWord = textBox2.Text;
    f1.richTextBox1.SelectedText = changeWord;
    }
    private void button3_Click(object sender, EventArgs e)
    {
    if (iFound == -1) button1_Click(sender, e);
    string findWord = textBox1.Text;
    while (iFound != -1)
    {
    button2_Click(sender, e);
    if (checkBox1.Checked)
    {
    iFound = f1.richTextBox1.Find(findWord, iFound + 1, f1.richTextBox1.TextLength, RichTextBoxFinds.MatchCase);
    }
    else
    {
    iFound = f1.richTextBox1.Find(findWord, iFound + 1, f1.richTextBox1.TextLength, RichTextBoxFinds.None);
    }
    }
    }
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
    if (textBox1.TextLength == 0)
    {
    button1.Enabled = false;
    button2.Enabled = false;
    button3.Enabled = false;
    }
    else
    {
    button1.Enabled = true;
    button2.Enabled = true;
    button3.Enabled = true;
    }
    }
    private void button4_Click(object sender, EventArgs e)
    {
    this.Close();
    }
    }
    }

Form4.cs

    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;
    namespace Notepad
    {
    public partial class Form4 : Form
    {
    Form1 f1;
    int allline;
    public Form4()
    {
    InitializeComponent();
    }
    private void Form4_Load(object sender, EventArgs e)
    {
    f1 = (Form1)Owner;
    textBox1.Focus();
    }
    private void button2_Click(object sender, EventArgs e)
    {
    this.Close();
    }
    private void button1_Click(object sender, EventArgs e)
    {
    int line = Convert.ToInt32(textBox1.Text);
    if (line < 1 || line > allline)
    {
    MessageBox.Show("行数超过总行数", "记事本 - 跳行", MessageBoxButtons.OK, MessageBoxIcon.Information);
    Form4_Activated(sender, e);
    return;
    }
    f1.richTextBox1.SelectionStart = f1.richTextBox1.GetFirstCharIndexFromLine(line - 1);
    f1.richTextBox1.SelectionLength = 0;
    this.Hide();
    }
    private void textBox1_KeyPress_1(object sender, KeyPressEventArgs e)
    {
    if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8)
    e.Handled = true;
    }
    private void Form4_Activated(object sender, EventArgs e)
    {
    allline = f1.richTextBox1.GetLineFromCharIndex(f1.richTextBox1.SelectionStart);
    int start = 0;
    int cursor = f1.richTextBox1.SelectionStart;
    while (start < cursor)
    {
    if (allline == f1.richTextBox1.GetLineFromCharIndex(start))
    {
    break;
    }
    else
    start++;
    }
    allline++;
    textBox1.Text = allline.ToString();
    textBox1.SelectAll();
    textBox1.Focus();
    }
    }
    }
转载保留版权:http://haipz.com/blog/i/1040 - 海胖博客