实例介绍
【实例截图】
【核心代码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Drawing.Printing;
namespace Mynotepad
{
public partial class Form1 : Form
{
string fileAddress;
bool istextChanged = false;
bool isopenFile = false;
public static Form1 notepad;
private Form3 fm3;
public Form1()
{
InitializeComponent();
fm3 = new Form3();
fm3.Owner = this;
}
[STAThread]
static void Main()
{
notepad = new Form1();
Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false);
Application.Run(notepad);
}
private void new_Click(object sender, EventArgs e)
{
if (this.richTextBox1.Text != "")
{
// DialogResult strYesNoCancel = MessageBox.Show("文件的内容已改变,是否保存", "记事本", MessageBoxButtons.YesNoCancel);
DialogResult strYesNoCancel = MessageBox.Show("文件" Text "的内容已改变,需要保存吗?", "保存文件",
MessageBoxButtons.YesNoCancel);
if (strYesNoCancel.ToString() == "Yes")
{
saveFileDialog1.Reset();
saveFileDialog1.Filter = "文本文件(*.txt)|*.txt";
saveFileDialog1.Title = "另存为";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
fileAddress = saveFileDialog1.FileName;
}
}
if (strYesNoCancel.ToString() == "No")
{
this.richTextBox1.Clear();
this.Text = "新建文本" "-记事本";
}
if (strYesNoCancel.ToString() == "Cancel")
{
return;
}
}
else
{
this.richTextBox1.Clear();
this.Text = "新建文本" "-记事本";
}
}
private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.Reset();
openFileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件|*";
openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;
openFileDialog1.Multiselect = false;
openFileDialog1.Title = "打开文件";
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.PlainText);
isopenFile = true;
istextChanged = false;
this.Text = Path.GetFileName(openFileDialog1.FileName) "-记事本";
fileAddress = openFileDialog1.FileName;
}
}
private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.Reset();
saveFileDialog1.Filter = "文本文件(*.txt)|*.txt";
saveFileDialog1.Title = "另存为";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
fileAddress = saveFileDialog1.FileName;
}
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
toolStripButton6.Enabled = true;
撤消UToolStripMenuItem.Enabled = true;
撤消ToolStripMenuItem.Enabled = true;
istextChanged = true;
}
private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
{
if (isopenFile)
{
if (istextChanged)
{
richTextBox1.SaveFile(fileAddress, RichTextBoxStreamType.PlainText);
}
}
else
{
if (istextChanged)
{
saveFileDialog1.Reset();
saveFileDialog1.Filter = "文本文件(*.txt)|*.txt";
saveFileDialog1.Title = "另存为";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
fileAddress = saveFileDialog1.FileName;
}
}
}
}
private void 字体FToolStripMenuItem_Click(object sender, EventArgs e)
{
fontDialog1.ShowColor = true;
fontDialog1.ShowApply = false;
fontDialog1.ShowEffects = true;
if (fontDialog1.ShowDialog(this) == DialogResult.OK)
{
richTextBox1.Font = fontDialog1.Font;
richTextBox1.ForeColor = fontDialog1.Color;
}
}
private void toolStripButton16_Click(object sender, EventArgs e)
{
colorDialog1.AnyColor = true;
colorDialog1.AllowFullOpen = true;
colorDialog1.FullOpen = true;
if (colorDialog1.ShowDialog(this) == DialogResult.OK)
{
richTextBox1.ForeColor = colorDialog1.Color;
}
}
private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.CanUndo == true)
{
richTextBox1.Undo();
toolStripButton7.Enabled = true;
toolStripMenuItem1.Enabled = true;
toolStripButton6.Enabled = false;
撤消UToolStripMenuItem.Enabled = false;
撤消ToolStripMenuItem.Enabled = false ;
//textBox1.ClearUndo();
}
}
private void 重做toolStripMenuItem1_Click(object sender, EventArgs e)
{
if (richTextBox1.CanRedo == true)
{
richTextBox1.Redo();
toolStripButton7.Enabled = false;
toolStripMenuItem1.Enabled = false;
}
}
private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.SelectedText != "")
{
richTextBox1.Cut();
}
}
private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.SelectedText != "")
{
richTextBox1.Copy();
}
}
private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
{
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true)
{
richTextBox1.Paste();
}
}
private void 删除LToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.SelectedText != "")
{
richTextBox1.SelectedText = "";
}
}
private void 时间日期DToolStripMenuItem_Click(object sender, EventArgs e)
{
// richTextBox1.AppendText(DateTime.Now.ToString());
richTextBox1.SelectedText = DateTime.Now.ToString();
}
private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.SelectAll();
}
private void toolStripButton17_Click(object sender, EventArgs e)
{
ColorDialog colorDialog1 = new ColorDialog();
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.BackColor = colorDialog1.Color;
}
}
private void 状态ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (状态ToolStripMenuItem.Checked == true)
{
statusStrip1.Visible = false;
状态ToolStripMenuItem.Checked = false;
}
else
{
statusStrip1.Visible = true;
状态ToolStripMenuItem.Checked = true;
}
}
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
if (richTextBox1.SelectionLength == 0)
{
toolStripButton5.Enabled = false;
toolStripButton8.Enabled = false;
toolStripButton10.Enabled = false;
剪切TToolStripMenuItem.Enabled = false;
复制CToolStripMenuItem.Enabled = false;
删除LToolStripMenuItem.Enabled = false;
剪切ToolStripMenuItem.Enabled = false;
复制ToolStripMenuItem.Enabled = false;
删除ToolStripMenuItem.Enabled = false;
toolStripButton14.Enabled = false;
toolStripButton15.Enabled = false;
toolStripButton20.Enabled = false;
}
else
{
toolStripButton5.Enabled = true;
toolStripButton8.Enabled = true;
toolStripButton10.Enabled = true;
剪切TToolStripMenuItem.Enabled = true;
复制CToolStripMenuItem.Enabled = true;
删除LToolStripMenuItem.Enabled = true;
剪切ToolStripMenuItem.Enabled = true;
复制ToolStripMenuItem.Enabled = true;
删除ToolStripMenuItem.Enabled = true;
toolStripButton14.Enabled = true;
toolStripButton15.Enabled = true;
toolStripButton20.Enabled = true;
}
if (状态ToolStripMenuItem.Enabled == true)
{
if (状态ToolStripMenuItem.Checked == true)
{
status(sender, e);
}
else
{
toolStripStatusLabel1.Text = "";
}
}
}
private void status(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 "列" col ;
}
private void 自动换行WToolStripMenuItem_Click(object sender, EventArgs e)
{
if (自动换行WToolStripMenuItem.Checked == true)
{
自动换行WToolStripMenuItem.Checked = false;
toolStripMenuItem2.Enabled = true;
richTextBox1.ScrollBars = RichTextBoxScrollBars.ForcedBoth;
richTextBox1.WordWrap = false;
}
else
{
toolStripMenuItem2.Enabled = false;
自动换行WToolStripMenuItem.Checked = true;
richTextBox1.ScrollBars = RichTextBoxScrollBars.ForcedVertical;
richTextBox1.WordWrap = true;
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "无标题" "-记事本";
this.TopMost = false;
}
private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
{
About about = new About();
about.Show();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (this.richTextBox1.Text != "")
{
e.Cancel = true;
DialogResult strYesNoCancel = MessageBox.Show("文件" Text "的内容已改变,需要保存吗?", "保存文件",
MessageBoxButtons.YesNoCancel);
if (strYesNoCancel.ToString() == "Yes")
{
saveFileDialog1.Reset();
saveFileDialog1.Filter = "文本文件(*.txt)|*.txt";
saveFileDialog1.Title = "另存为";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
e.Cancel = false;
}
}
if (strYesNoCancel.ToString() == "No")
{
e.Cancel = false;
}
if (strYesNoCancel.ToString() == "Cancel")
{
return;
}
}
}
private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.richTextBox1.Text != "")
{
DialogResult strYesNoCancel = MessageBox.Show("文件" Text "的内容已改变,需要保存吗?", "保存文件",
MessageBoxButtons.YesNoCancel);
if (strYesNoCancel.ToString() == "Yes")
{
saveFileDialog1.Reset();
saveFileDialog1.Filter = "文本文件(*.txt)|*.txt";
saveFileDialog1.Title = "另存为";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
this.Dispose();
}
}
if (strYesNoCancel.ToString() == "No")
{
this.Dispose();
}
if (strYesNoCancel.ToString() == "Cancel")
{
return;
}
}
else
{
this.Dispose();
}
}
private void toolStripButton14_Click(object sender, EventArgs e) //粗体
{
if (richTextBox1.SelectionFont != null)
{
System.Drawing.Font currentFont = richTextBox1.SelectionFont;
System.Drawing.FontStyle newFontStyle;
if (richTextBox1.SelectionFont.Bold == true)
{
newFontStyle = FontStyle.Regular;
}
else
{
newFontStyle = FontStyle.Bold;
}
richTextBox1.SelectionFont = new Font(
currentFont.FontFamily,
currentFont.Size,
newFontStyle
);
}
}
private void toolStripButton15_Click(object sender, EventArgs e) //斜体
{
if (richTextBox1.SelectionFont != null)
{
System.Drawing.Font currentFont = richTextBox1.SelectionFont;
System.Drawing.FontStyle newFontStyle;
if (richTextBox1.SelectionFont.Italic == true)
{
newFontStyle = FontStyle.Regular;
}
else
{
newFontStyle = FontStyle.Italic;
}
richTextBox1.SelectionFont = new Font(
currentFont.FontFamily,
currentFont.Size,
newFontStyle
);
}
}
private void toolStripButton20_Click(object sender, EventArgs e) //下滑线
{
if (richTextBox1.SelectionFont != null)
{
System.Drawing.Font currentFont = richTextBox1.SelectionFont;
System.Drawing.FontStyle newFontStyle;
if (richTextBox1.SelectionFont.Underline == true)
{
newFontStyle = FontStyle.Regular;
}
else
{
newFontStyle = FontStyle.Underline;
}
richTextBox1.SelectionFont = new Font(
currentFont.FontFamily,
currentFont.Size,
newFontStyle
);
}
}
private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
{
System.Diagnostics.Process.Start(e.LinkText);
}
private void richTextBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, true);
int i;
for (i = 0; i < s.Length; i )
fileAddress = s[i];
richTextBox1.LoadFile((string)fileAddress, RichTextBoxStreamType.PlainText);
isopenFile = true;
}
public static void gto(int i)
{
//光标的焦点的行数
int ln = 0;
for (int count = 0; count < i; count )
{
foreach (char ch in notepad.richTextBox1.Lines[count])
{
ch.ToString();
ln ;
}
ln = 1; //表示一个换行符 占二个字符
}
notepad.richTextBox1.SelectionStart = ln;
notepad.richTextBox1.ScrollToCaret();
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
int line = this.richTextBox1.Lines.Length;
if (line == 0)
{
line = line 1;
}
Goto go = new Goto(line);
go.ShowDialog();
}
private void toolStripButton11_Click(object sender, EventArgs e)
{
if (this.TopMost == true)
{
this.TopMost = false;
return;
}
else
{
this.TopMost = true;
return;
}
}
private void toolStripMenuItem3_Click(object sender, EventArgs e)
{
if (fm3.IsDisposed)
{
fm3 = new Form3();
fm3.Owner = this;
fm3.Show();
}
else
{
fm3.Show();
}
}
private void 帮助HToolStripMenuItem1_Click(object sender, EventArgs e)
{
Form4 form4 = new Form4();
form4.Show();
}
}
}
标签: 记事本
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


支持(0) 盖楼(回复)