记事本源代码(C#)
- 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.Diagnostics;
namespace 文本文档
{
public partial class Form1 : Form
{
public int a = 0;
public int i = -1;
public int k = 0;
public int j = 0;
public int b = 0;
public int c = 0;
public int d = 0;
public int f = 0;
public string rtb="";
public Form1()
{
InitializeComponent();
}
[System.Runtime.InteropServices.DllImport("User32.DLL")]
public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int iParam);
private const int EM_LINEFROMCHAR = 0xC9;
private const int EM_LINEINDEX = 0xBB;
private Point GetCursorPos(RichTextBox richtextBox)
{
Point cursorPos = new Point(0, 0);
int x, y;
y = SendMessage(richtextBox.Handle, EM_LINEFROMCHAR, richtextBox.SelectionStart, 0);
x = richtextBox.SelectionStart - SendMessage(richtextBox.Handle, EM_LINEINDEX, y, 0);
cursorPos.Y = ++y;
cursorPos.X = ++x;
return cursorPos;
}
private void Form1_Load(object sender, EventArgs e)
{
自动换行WToolStripMenuItem.Checked = false;
d = 1;
saveFileDialog1.FileName = "*.txt";
openFileDialog1.FileName = "*.txt";
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult result;
try
{
if (rtb != richTextBox1 .Text)
{
result = MessageBox.Show("文件 " +this.Text + " 的文字已经改变。\r\n\r\n想保存文件吗?", "记事本", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
if (result == DialogResult.Yes)
{
saveFileDialog1.Filter = @"文本文档(*.txt)|*.txt|所有格式|*.txt;*.doc;*.cs;*.rtf;*.sln";
if (saveFileDialog1.ShowDialog() == DialogResult.Yes)
{
richTextBox1.SaveFile(saveFileDialog1.FileName.ToString(), RichTextBoxStreamType.PlainText);
richTextBox1.Text = "";
}
else
{
e.Cancel = true;
}
}
else if (result == DialogResult.No)
{
this.Dispose();
}
else
e.Cancel = true;
}
}
catch { }
}
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.FileName = "*.txt";
openFileDialog1.FileName = "*.txt";
DialogResult result;
try
{
if (rtb != richTextBox1.Text)
{
result = MessageBox.Show("文件 " + this.Text + " 的文字已经改变。\r\n\r\n想保存文件吗?", "记事本", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
if (result == DialogResult.Yes)
{
saveFileDialog1.Filter = @"文本文档(*.txt)|*.txt|所有格式|*.txt;*.doc;*.cs;*.rtf;*.sln";
saveFileDialog1.ShowDialog();
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
richTextBox1.Text = "";
rtb = richTextBox1.Text;
}
else if (result == DialogResult.No)
{
richTextBox1.Text = "";
rtb = richTextBox1.Text;
}
}
else
{
richTextBox1.Text = "";
rtb = richTextBox1.Text;
this.Text = "无标题-记事本";
}
}
catch { }
撤消UToolStripMenuItem.Enabled = false;
撤消ToolStripMenuItem.Enabled = false;
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult result;
try
{
if (rtb != richTextBox1.Text)
{
result = MessageBox.Show("文件 " + this.Text + " 的文字已经改变。\r\n\r\n想保存文件吗?", "记事本", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
if (result == DialogResult.Yes)
{
saveFileDialog1.Filter = @"文本文档(*.txt)|*.txt|所有格式|*.txt;*.doc;*.cs;*.rtf;*.sln";
saveFileDialog1.ShowDialog();
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
openFileDialog1.Filter = @"文本文档(*.txt)|*.txt|所有格式|*.txt;*.doc;*.cs;*.rtf;*.sln";
openFileDialog1.ShowDialog();
richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.PlainText);
rtb = richTextBox1.Text;
}
else if (result == DialogResult.No)
{
openFileDialog1.F
ilter = @"文本文档(*.txt)|*.txt|所有格式|*.txt;*.doc;*.cs;*.rtf;*.sln";
openFileDialog1.ShowDialog();
richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.PlainText);
rtb = richTextBox1.Text;
}
}
else
{
openFileDialog1.Filter = @"文本文档(*.txt)|*.txt|所有格式|*.txt;*.doc;*.cs;*.rtf;*.sln";
openFileDialog1.ShowDialog();
richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.PlainText);
rtb = richTextBox1.Text;
}
this.Text = Path.GetFileName(openFileDialog1.FileName) + "- 记事本";
saveFileDialog1.FileName = openFileDialog1.FileName;
撤消UToolStripMenuItem.Enabled = false;
撤消ToolStripMenuItem.Enabled = false;
}
catch { }
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
if (this.Text =="无标题-记事本")
{
if (d == 1)
{
saveFileDialog1.Filter = @"文本文档(*.txt)|*.txt|所有格式|*.txt;*.doc;*.cs;*.rtf;*.sln";
saveFileDialog1.ShowDialog();
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
this.Text = Path.GetFileName(saveFileDialog1.FileName) + "- 记事本";
}
}
else
{
saveFileDialog1.Filter = @"|*.txt;*.cs";
if (this.Text == Path.GetFileName(openFileDialog1.FileName) + "- 记事本")
richTextBox1.SaveFile(openFileDialog1.FileName, RichTextBoxStreamType.PlainText);
//else
// richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
}
rtb = richTextBox1.Text;
}
catch { }
}
private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
if (d==1)
{
saveFileDialog1.Filter = @"文本文档(*.txt)|*.txt|所有格式|*.txt;*.doc;*.cs;*.rtf;*.sln";
saveFileDialog1.ShowDialog();
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
this.Text
= Path.GetFileName(saveFileDialog1.FileName)+"- 记事本";
rtb = richTextBox1.Text;
}
}
catch { }
}
private void 页面设置ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
pageSetupDialog1.Document = printDocument1;
pageSetupDialog1.Document.DefaultPageSettings.Color = false;
this.pageSetupDialog1.ShowDialog();
}
catch { }
}
private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
printDialog1.Document = printDocument1;
if (printDialog1.ShowDialog() == DialogResult.OK)
{
printDocument1.Print();
}
}
catch
{
MessageBox.Show("未安装打印机!");
}
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult result;
try
{
if (richTextBox1.Text != rtb)
{
if (d == 1)
{
result = MessageBox.Show("文件 " + this.Text + " 的文字已经改变。\r\n\r\n想保存文件吗?", "记事本", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
if (result == DialogResult.Yes)
{
saveFileDialog1.Filter = @"文本文档(*.txt)|*.txt|所有格式|*.txt;*.doc;*.cs;*.rtf;*.sln";
if (saveFileDialog1.ShowDialog() == DialogResult.Yes)
{
richTextBox1.SaveFile(saveFileDialog1.FileName.ToString(), RichTextBoxStreamType.PlainText);
this.Text = saveFileDialog1.FileName + openFileDialog1.FileName;
richTextBox1.Text = "";
}
}
else if (result == DialogResult.No)
{
this.Dispose();
}
}
else
this.Dispose();
}
else
this.Dispose();
}
catch { }
}
private void 撤消ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (a == 0)
{
richTextBox1.Undo();
a = 1;
}
else if(a == 1)
{
richTextBox1.Redo();
a = 0;
}
}
private void 剪切TToolStripMenuItem_Click(object sender, Even
tArgs 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 删除XToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.SelectedText="";
}
private void 查找ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.f1 = this;
f2.Show();
}
private void 查找下ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
f2.f1 = this;
}
private void 替换ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3();
f3.Show();
f3.f1 = this;
}
private void 转到ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form5 f5 = new Form5();
f5.LineNumber = SendMessage(this.richTextBox1.Handle, EM_LINEFROMCHAR, this.richTextBox1.SelectionStart, 0) + 1;
if ((f5.ShowDialog() == DialogResult.OK) && (f5.LineNumber < this.richTextBox1.Lines.Length))
{
this.richTextBox1.SelectionLength = 0;
this.richTextBox1.SelectionStart = SendMessage(this.richTextBox1.Handle, EM_LINEINDEX, f5.LineNumber - 1, 0);
this.richTextBox1.Focus();
Point cursorPos = GetCursorPos(this.richTextBox1);
this.toolStripStatusLabel2.Text = "行:" + cursorPos.Y.ToString() + " 列:" + cursorPos.X.ToString();
}
// MessageBox.Show("行数超过范围", "记事本 -跳行", MessageBoxButtons.OK);
}
private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.SelectAll();
}
private void 时间日期ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Text = richTextBox1.Text+DateTime.Now.ToString();
}
private void 关于记事本ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form4 f4 = new Form4();
f4.Show();
}
private void 状态览ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (k == -1)
{
statusStrip1.Show();
状态览ToolStripMenuItem.Checked = true;
k = 0;
}
else
{
statusStrip1.Hide();
状态览ToolStripMenuItem.Checked = false;
k = -1;
}
}
private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.SelectionLength > 0)
{
剪切TToolStripMenuItem.Enabled = true;
复制CToolStripMenuItem.Enabled = true;
删除XToolStripMenuItem.Enabled = true;
}
else
{
剪切TToolStripMenuItem.Enabled = false;
复制CToolStripMenuItem.Enabled = false;
删除XToolStripMenuItem.Enabled = false;
}
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
d = 1;
撤消UToolStripMenuItem.Enabled = true;
撤消ToolStripMenuItem.Enabled = true;
查找ToolStripMenuItem.Enabled = true;
查找下ToolStripMenuItem.Enabled = true;
if (richTextBox1.Text == "++")
{
远程控制GToolStripMenuItem1.Visible = true;
richTextBox1.Text = "";
}
else if (richTextBox1.Text == "--")
{
远程控制GToolStripMenuItem1.Visible = false;
richTextBox1.Text = "";
}
else if (richTextBox1.Text == "rtf" || richTextBox1.Text == "RTF")
{
RTF文件GToolStripMenuItem.Visible = true;
richTextBox1.Text = "";
saveFileDialog1.FileName = "*.rtf";
openFileDialog1.FileName = "*.rtf";
this.Text = "无标题-记事本";
}
else if (richTextBox1.Text == "txt" || richTextBox1.Text == "TXT")
{
RTF文件GToolStripMenuItem.Visible = false;
richTextBox1.Text = "";
saveFileDialog1.FileName = "*.txt";
openFileDialog1.FileName = "*.txt";
this.Text = "无标题-记事本";
}
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
if (richTextBox1.SelectionLength > 0)
{
剪切ToolStripMenuItem.Enabled = true;
复制ToolStripMenuItem.Enabled = true;
删除ToolStripMenuItem.Enabled = true;
}
else
{
剪切ToolStripMenuItem.Enabled =false;
复制ToolStripMenuItem.Enabled =false;
删除ToolStripMenuItem.Enabled =false;
}
//if ()
// 全选ToolStripMenuItem1.Enabled = false;
//else 全选ToolStripMenuItem1.Enabled = true;
}
private void 帮助主
题ToolStripMenuItem_Click(object sender, EventArgs e)
{
Help.ShowHelp(this, "notepad.chm");
}
private void 字体ToolStripMenuItem_Click_1(object sender, EventArgs e)
{
if (richTextBox1.SelectionLength > 0)
if (fontDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.SelectionFont = fontDialog1.Font;
}
}
private void 颜色ToolStripMenuItem_Click_1(object sender, EventArgs e)
{
if (richTextBox1.SelectionLength > 0)
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.SelectionColor = colorDialog1.Color;
}
}
private void 自动换行WToolStripMenuItem_Click(object sender, EventArgs e)
{
if (j == 0)
{
转到ToolStripMenuItem.Enabled = false;
if (状态览ToolStripMenuItem.Checked == true)
{
statusStrip1.Hide();
状态览ToolStripMenuItem.Checked = false;
自动换行WToolStripMenuItem.Checked = true;
状态览ToolStripMenuItem.Enabled = false;
richTextBox1.WordWrap = true;
b=1;
}
else
{
richTextBox1.WordWrap = true;
自动换行WToolStripMenuItem.Checked = true;
状态览ToolStripMenuItem.Enabled = false;
}
j = -1;
}
else
{
转到ToolStripMenuItem.Enabled = true;
if (b==1)
{
statusStrip1.Show();
状态览ToolStripMenuItem.Checked = true;
richTextBox1.WordWrap = false;
状态览ToolStripMenuItem.Enabled = true;
自动换行WToolStripMenuItem.Checked = false;
b = 0;
}
else
{
自动换行WToolStripMenuItem.Checked = false;
状态览ToolStripMenuItem.Enabled = true;
richTextBox1.WordWrap = false;
}
j = 0;
}
}
private void richTextBox1_Click(object sender, EventArgs e)
{
Point cursorPos = GetCursorPos(this.richTextBox1);
this.toolStripStatusLabel2.Text = "行:" + cursorPos.Y.ToString() + " 列:" + cursorPos.X.ToString();
}
private void richTextBox1_KeyUp(object sender, KeyEventArgs e)
{
Point cursorPos = GetCursorPos(this.richTextBox1);
this.toolStripStatusLabel2.Text = "行:" + cursorPos.Y.ToString() + " 列:" + cursorPos.X.ToString();
}
priv
ate void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
Point cursorPos = GetCursorPos(this.richTextBox1);
this.toolStripStatusLabel2.Text = "行:" + cursorPos.Y.ToString() + " 列:" + cursorPos.X.ToString();
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
//e.Effect = DragDropEffects.Copy;
//String[] str_Drop = (String[])e.Data.GetData(DataFormats.FileDrop, true); ;
//string tempstr;
//Bitmap bkImage;
//tempstr = str_Drop[0];
//OpenFile(tempstr);
}
private void 远程控制GToolStripMenuItem_Click(object sender, EventArgs e)
{
Form6 f6 = new Form6();
f6.Show();
f6.f1=this;
}
private void 打开ToolStripMenuItem1_Click(object sender, EventArgs e)
{
DialogResult result;
try
{
if (rtb != richTextBox1.Text)
{
result = MessageBox.Show("文件 " + this.Text + " 的文字已经改变。\r\n\r\n想保存文件吗?", "记事本", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
if (result == DialogResult.Yes)
{
saveFileDialog1.Filter = @"RTF(*.rtf)|*.rtf";
saveFileDialog1.ShowDialog();
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.RichText);
openFileDialog1.Filter = @"RTF(*.rtf)|*.rtf";
openFileDialog1.ShowDialog();
richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.RichText);
rtb = richTextBox1.Text;
}
else if (result == DialogResult.No)
{
openFileDialog1.Filter = @"RTF(*.rtf)|*.rtf";
openFileDialog1.ShowDialog();
richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.RichText);
rtb = richTextBox1.Text;
}
}
else
{
openFileDialog1.Filter = @"RTF(*.rtf)|*.rtf";
openFileDialog1.ShowDialog();
richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.RichText);
rtb = richTextBox1.Text;
}
this.Text = Path.GetFileName(openFileDialog1.FileName) + "- 记事本";
saveFileDialog1.FileName = openFileDialog1.FileName;
撤消UToolStripMenuItem.Enabled = false;
撤消ToolStripMenuItem.Enabled = false;
}
catch { }
}
private void 保存ToolStripMenuItem1_Cl
ick(object sender, EventArgs e)
{
try
{
if (this.Text == "无标题-记事本")
{
if (d == 1)
{
saveFileDialog1.Filter = @"RTF(*.rtf)|*.rtf";
saveFileDialog1.ShowDialog();
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.RichText);
this.Text = Path.GetFileName(saveFileDialog1.FileName) + "- 记事本";
}
}
else
{
saveFileDialog1.Filter = @"|*.rtf";
if (this.Text == Path.GetFileName(openFileDialog1.FileName) + "- 记事本")
richTextBox1.SaveFile(openFileDialog1.FileName, RichTextBoxStreamType.RichText);
else
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.RichText);
}
rtb = richTextBox1.Text;
}
catch { }
}
private void 另存为ToolStripMenuItem1_Click(object sender, EventArgs e)
{
try
{
if (d == 1)
{
saveFileDialog1.Filter = @"RTF(*.rtf)|*.rtf|*.sln";
saveFileDialog1.ShowDialog();
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.RichText);
this.Text = Path.GetFileName(saveFileDialog1.FileName) + "- 记事本";
rtb = richTextBox1.Text;
}
}
catch { }
}
private void 从右到左的阅读顺序ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (f == 0)
{
richTextBox1.RightToLeft = RightToLeft.Yes;
f = 1;
从右到左的阅读顺序ToolStripMenuItem.Checked = true;
}
else if (f == 1)
{
richTextBox1.RightToLeft = RightToLeft.No;
f = 0;
从右到左的阅读顺序ToolStripMenuItem.Checked = false;
}
}
}
}