记事本代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
//引用类的申明
namespace WindowsApplication1//名称空间的申明
{
public partial class MainForm : Form//主窗口
{
public MainForm()
{
InitializeComponent();
}
//
//人机交互是通过操作人员对事件的操作来实现的,以下为事件的实现代码
//
//新建文本
private void MenuItem7_Click(object sender, EventArgs e)
{
//如果当前文本内容处于修改状态,则提示保存
if (textBox1.Modified == true)
{
DialogResult re = MessageBox.Show("文件" + Text + "的内容已改变,需要保存吗?", "保存文件", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
switch (re)
{
case DialogResult.Yes:
this.MenuItem9_Click(sender, e);
textBox1.Clear();
stsavename = "";
this.Text = "新建-记事本";
break;
case DialogResult.No:
textBox1.Clear();
stsavename = "";
this.Text = "新建-记事本";
break;
case DialogResult.Cancel:
break;
}
}
else
{
textBox1.Clear();
stsavename = "";
this.Text = "新建-记事本";
textBox1.Modified = false;
}
}
//打开文件
private void MenuItem8_Click(object sender, EventArgs e)
{
try//捕获异常事件
{
//如果当前文本内容被修改,则提示保存
if (textBox1.Modified == true)
{
//提示保存对话框
DialogResult re = MessageBox.Show("文件" + this.Text + "的内容已经改变,是否需要保存?", "保存文件", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (re == DialogResult.Cancel)
{
return;
}
if (re == DialogResult.OK)
{
this.MenuItem9_Click(sender, e);
}
}
openFileDialog1.Title = "打开";
openFileDialog1.FileName = "";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//打开选定路径上的文件
StreamReader sr = new StreamReader(openFileDialog1.FileName, Encoding.Default);
stsavename = openFileDialog1.FileName;
this.Text = Path.GetFileNameWithoutExtension(stsavename) + "-记事本";
textBox1.Text = sr.ReadToEnd();
sr.Close();
}
}
//处理异常事件
catch (Exception error)
{
MessageBox.Show(error.Message.ToString());
}
}
//保存文件
private void MenuItem9_Click(object sender, EventArgs e)
{
//捕获异常事件
try
{
if (stsavename == "")
{
saveFileDialog1.Title = "保存";
saveFileDialog1.FileName = "*.txt";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
//保存的实现
stsavename = saveFileDialog1.FileName;
using (StreamWriter sw = new StreamWriter(saveFileDialog1.FileName))
{
sw.Write(textBox1);
sw.Close();
textBox1.Modified = false;
this.Text = Path.GetFileNameWithoutExtension(saveFileDialog1.FileName) + "-记事本";
}
}
}
else
{
textBox1.SaveFile(stsavename, RichTextBoxStreamType.PlainText);
textBox1.Modified = false;
}
}
//处理异常事件
catch (Exception er)
{
MessageBox.Show(er.Message.ToString());
}
}
//另存为:
private void MenuItem10_Click(object sender, EventArgs e)
{
try
{
saveFileDialog1.Title = "另存为";
saveFileDialog1.FileName = "*.txt";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = new StreamWriter(saveFileDialog1.FileName);
sw.Write(textBox1);
textBox1.Modified = false;
stsavename = saveFileDialog1.FileName;
sw.Close();
this.Text = Path.GetFileNameWithoutExtension(saveFileDialog1.FileName) + "-记事本";
}
}
catch (Exception er)
{
MessageBox.Show(er.Message.ToString());
}
}
//页面设置
private void MenuItem11_Click(object sender, EventArgs e)
{
try
{
if (pageSetupDial
og1.ShowDialog() == DialogResult.OK)
{
pageSetupDialog1.ShowDialog();
}
}
catch (Exception err)
{
MessageBox.Show(err.Message.ToString());
}
}
//打印
private void MenuItem12_Click(object sender, EventArgs e)
{
try
{
if (printDialog1.ShowDialog() == DialogResult.OK)
{
printDialog1.ShowDialog();
}
}
catch (Exception err)
{
MessageBox.Show(err.Message.ToString());
}
}
//退出
private void MenuItem14_Click(object sender, EventArgs e)
{
this.Close();
}
//编辑-撤消
private void MenuItem15_Click(object sender, EventArgs e)
{
if (textBox1.CanUndo == true)
{
textBox1.Undo();
textBox1.ClearUndo();
}
}
//编辑-剪切
private void MenuItem17_Click(object sender, EventArgs e)
{
if (textBox1.SelectedText != "")
{
textBox1.Cut();
}
}
//编辑-复制
private void MenuItem18_Click(object sender, EventArgs e)
{
textBox1.Copy();
}
//编辑-粘贴
private void MenuItem19_Click(object sender, EventArgs e)
{
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true)
{
textBox1.Paste();
}
}
//编辑-删除
private void MenuItem20_Click(object sender, EventArgs e)
{
if (textBox1.SelectedText != "")
{
textBox1.SelectedText = "";
}
}
//编辑-全选
private void MenuItem27_Click(object sender, EventArgs e)
{
textBox1.SelectAll();
}
//查找
private void MenuItem22_Click(object sender, EventArgs e)
{
new Find(textBox1).Show();
}
//查找下一个,与查找相同的调用方法
private void MenuItem23_Click(object sender, EventArgs e)
{
new Find(textBox1).Show();
}
//覆盖
private void MenuItem24_Click(object sender, EventArgs e)
{
new Replace(textBox1).Show();
}
//跳转
private void MenuItem25_Click(object sender, EventArgs e)
{
new Goto(textBox1).Show();
}
//时间/日期,加到文本最后
private void MenuItem28_Click(object sender, EventArgs e)
{
textBox1.AppendText(DateTime.Now.ToString());
}
//字型设置
private void MenuItem30_Click(object sender, EventArgs e)
{
if (fontDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Font = fontDialog1.Font;
textBox1.ForeColor = fontDialog1.Color;
}
}
//显示帮助文件
private void MenuItem32_Click(object sender, EventArgs e)
{
Help.ShowHelp(this, "notepad.chm");
}
//关于本记事本制作申明
private void MenuItem33_Click(object sender, EventArgs e)
{
new About().ShowDialog();
}
//字体颜色设置
private void MenuItem34_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.ForeColor = colorDialog1.Color;
}
}
//背景颜色设置
private void MenuItem35_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.BackColor = colorDialog1.Color;
}
}
//上下文菜单-撤消
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
this.MenuItem15_Click(sender, e);
}
//上下文菜单-剪切
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
this.MenuItem17_Click(sender, e);
}
//上下问菜单-复制
private void toolStripMenuItem3_Click(object sender, EventArgs e)
{
this.MenuItem18_Click(sender, e);
}
//上下文菜单-粘贴
private void toolStripMenuItem4_Click(object sender, EventArgs e)
{
this.MenuItem19_Click(sender, e);
}
//上下文菜单-删除
private void toolStripMenuItem5_Click(object sender, EventArgs e)
{
this.MenuItem20_Click(sender, e);
}
//上下文菜单-全选
private void toolStripMenuItem6_Click(object sender, EventArgs e)
{
this.MenuItem27_Click(sender, e);
}
//编辑菜单项下拉事件发生时更新其菜单项属性
private void MenuItem3_DropDownOpened(object sender, EventArgs e)
{
//如果剪切板内容不为空,则粘贴项可用,否则不可用
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
{
this.MenuItem19.Enabled = true;
}
else
this.MenuItem19.Enabled = false;
//根据选择区域不为空,则复制,剪切,删除可用;否则不可用
if (textBox1.SelectedText.Length > 0)
{
MenuItem17.Enabled = true;
MenuItem18.En
abled = true;
MenuItem20.Enabled = true;
}
else
{
MenuItem17.Enabled = false;
MenuItem18.Enabled = false;
MenuItem20.Enabled = false;
}
//设置撤消
if (textBox1.CanUndo == true)
{
MenuItem15.Enabled = true;
}
else
{
MenuItem15.Enabled = false;
}
//如果记事本文本内容不为空,则全选可用;否则不可用
if (textBox1.Text.Length > 0)
{
MenuItem27.Enabled = true;
MenuItem22.Enabled = true;
MenuItem23.Enabled = true;
}
else
{
MenuItem27.Enabled = false;
MenuItem22.Enabled = false;
MenuItem23.Enabled = false;
}
}
//点击右键时设置上下文菜单属性
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
//如果剪切板内容不为空,则粘贴项可用,否则不可用
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
{
this.toolStripMenuItem4.Enabled = true;
}
else
this.toolStripMenuItem4.Enabled = false;
//根据选择区域不为空,则复制,剪切,删除可用;否则不可用
if (textBox1.SelectedText.Length > 0)
{
toolStripMenuItem2.Enabled = true;
toolStripMenuItem3.Enabled = true;
toolStripMenuItem5.Enabled = true;
}
else
{
toolStripMenuItem2.Enabled = false;
toolStripMenuItem3.Enabled = false;
toolStripMenuItem5.Enabled = false;
}
//设置撤消
if (textBox1.CanUndo == true)
{
toolStripMenuItem1.Enabled = true;
}
else
{
toolStripMenuItem1.Enabled = false;
}
//如果记事本文本内容不为空,则全选可用;否则不可用
if (textBox1.Text.Length > 0)
{
toolStripMenuItem6.Enabled = true;
}
else
{
toolStripMenuItem6.Enabled = false;
}
}
//关闭窗体前提示是否需要保存
private void Form_Close(object sender, ponentModel.CancelEventArgs err)
{
if (textBox1.Modified == true)
{
DialogResult re = MessageBox.Show(this.Text + "内容已更改,是否需要保存?", "保存提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Asterisk);
swi
tch (re)
{
case DialogResult.Yes:
this.MenuItem9_Click(sender, err);
if (textBox1.Modified == true)
{
err.Cancel = true;
}
else
{
Application.Exit();
}
break;
case DialogResult.No:
Application.Exit();
break;
case DialogResult.Cancel:
err.Cancel = true;
break;
}
}
}
//状态栏
private void MenuItem31_Click(object sender, EventArgs e)
{
if (MenuItem31.Checked == true )
{
MenuItem31.Checked = false;
toolStripStatusLabel1.Text = "";
}
else
{
MenuItem31.Checked = true;
status(sender, e);
}
}
//选择区域发生变化时(即光标位置变化时,动态更新显示其位置)
private void textBox1_SelectionChanged(object sender, EventArgs e)
{
if (MenuItem31.Enabled == true)
{
if (MenuItem31.Checked == true)
{
status(sender, e);
}
else
{
toolStripStatusLabel1.Text = "";
}
}
}
//设置或取消自动换行
private void MenuItem29_CheckedChanged(object sender, EventArgs e)
{
//修改texBox1的相关属性
if (this.MenuItem29.Checked == false)
{
if (MenuItem31.Checked == true)
{
status(sender, e);
}
MenuItem25.Enabled = true;
textBox1.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.ForcedBoth;
textBox1.WordWrap = false;
}
else
{
MenuItem25.Enabled = false;
textBox1.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.ForcedVertical;
textBox1.WordWrap = true;
}
}
//当自动换行改变时,设置状态栏可用与否
private void MenuItem29_Click(object sender, EventArgs e)
{
if (MenuItem29.Checked == true)
{
MenuItem29.Checked = false;
MenuItem31.Enabled =true;
}
else
{
MenuItem29.Checked = true;
MenuItem31.Enabled = false ;
toolStripStatusLabel1.Text = "";
}
}
//显示光标所在行数与列数
private void status(object sender, EventArgs e)
{
int line = this.textBox1.GetLineFromCharIndex(textBox1.SelectionStart);
int col;
int start = 0;
int cursor = textBox1.SelectionStart;
while (start < cursor)
{
if (line == this.textBox1.GetLineFromCharIndex(start))
{
break;
}
else
start++;
}
col = cursor - start;
line++;
col++;
toolStripStatusLabel1.Text = "line " + line + " col " + col;
}
}
}