实例介绍
【实例截图】
【核心代码】
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 DotNetNotePad
{
public partial class frmMain : Form
{
/* 定义变量 */
public string filePath = null; // 储存文件路径
public string fileDataPath = "D:\\备注\\ssss.dat"; // 数据文件路径
public bool IsTextChanged = false; // 表示文本框是否被改变
// 打开文件格式
public const string FileType = "文本文件(*.txt)|*.txt|配置文件(*.ini)|*.ini|脚本文件(*.js *.vbs *.lua *.cfg)|*.js;*.vbs;*.lua;*.cfg|源码文件(*.h *.cpp *.cs *.vbs *.frm *.mdl *.cls)|*.h;*.cpp;*.cs;*.vbs;*.frm;*.mdl;*.cls|所有文件(*.*)|*.*";
public frmMain()
{
InitializeComponent();
}
private void 退出EToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void txtTextBox_TextChanged(object sender, EventArgs e)
{
stripSet_Tick(0, EventArgs.Empty);
IsTextChanged = true;
}
private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
{
// 粘贴
txtTextBox.Paste();
}
private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
{
// 剪切
txtTextBox.Cut();
}
private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
{
// 复制
txtTextBox.Copy();
}
private void 背景色BToolStripMenuItem_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog(); // 实例化颜色选择框
cd.Color = txtTextBox.ForeColor; // 将文本框默认颜色改变为文本字体颜色
if (cd.ShowDialog() == DialogResult.OK)
{
txtTextBox.ForeColor = cd.Color; // 设置文本字体颜色
}
}
private void 自动换行WToolStripMenuItem_Click(object sender, EventArgs e)
{
// 自动换行
if (!自动换行WToolStripMenuItem.Checked) { txtTextBox.ScrollBars = ScrollBars.Both; txtTextBox.WordWrap = false; }
else { txtTextBox.ScrollBars = ScrollBars.Vertical; txtTextBox.WordWrap = true; }
}
private void 字体FToolStripMenuItem_Click(object sender, EventArgs e)
{
FontDialog fd = new FontDialog(); // 实例化字体选择框
fd.ShowColor = true; // 启动文字选择框的颜色功能
fd.Font = txtTextBox.Font; // 将字体框默认字体改变为文本字体
fd.Color = txtTextBox.ForeColor; // 将字体框默认颜色改变为文本字体颜色
if (fd.ShowDialog() == DialogResult.OK)
{
// 设置字体
txtTextBox.Font = fd.Font;
txtTextBox.ForeColor = fd.Color;
}
}
//Downloads By http://www.veryhuo.com
private void 背景色BToolStripMenuItem1_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog();
cd.Color = txtTextBox.BackColor;
if (cd.ShowDialog() == DialogResult.OK)
{
txtTextBox.BackColor = cd.Color;
}
}
private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
{
QuestSaveText();
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "打开";
ofd.Filter = FileType;
if (ofd.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new StreamReader(ofd.FileName,Encoding.Default);
filePath = ofd.FileName;
this.Text = "备注" "[读取中]";
Application.DoEvents();
txtTextBox.Text = sr.ReadToEnd();
this.Text = "备注";
sr.Close();
IsTextChanged = false;
}
}
private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "另存为";
sfd.Filter = "文本文件(*.txt)|*.txt|配置文件(*.ini)|*.ini|所有文件(*.*)|*.*";
if (sfd.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream(sfd.FileName, FileMode.Create);
StreamWriter sw = new StreamWriter(fs,Encoding.Default);
sw.Write(txtTextBox.Text);
sw.Close();
fs.Close();
IsTextChanged = false;
}
}
private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
{
if (File.Exists(filePath) == true)
{
FileStream fs = new FileStream(filePath, FileMode.Create);
StreamWriter sw = new StreamWriter(fs, Encoding.Default);
sw.Write(txtTextBox.Text);
sw.Close();
fs.Close();
IsTextChanged = false;
}
else
{
MessageBox.Show("保存失败, 路径为空!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
{
QuestSaveText();
this.Text = "备注";
txtTextBox.Text = null;
filePath = null;
IsTextChanged = false;
autosave();
}
private void QuestSaveText()
{
if (IsTextChanged == true && filePath != null)
{
// if (MessageBox.Show("是否保存该程序", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
//{
保存SToolStripMenuItem_Click(1, EventArgs.Empty);
//}
}
else if (IsTextChanged == true)
{
// if (IsTextChanged == true && MessageBox.Show("是否保存该程序", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
//{
另存为AToolStripMenuItem_Click(1, EventArgs.Empty);
//}
}
}
private void 常用ToolStripMenuItem_Click(object sender, EventArgs e)
{
txtTextBox.SelectAll();
}
private void 状态栏SToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!状态栏SToolStripMenuItem.Checked) { statusStrip.Visible = false; } else { statusStrip.Visible = true; }
}
private void stripSet_Tick(object sender, EventArgs e)
{
int index = txtTextBox.GetFirstCharIndexOfCurrentLine();
int line = txtTextBox.GetLineFromCharIndex(index) 1;
int col = txtTextBox.SelectionStart - index;
toolStripStatusLabel2.Text = "时间: " DateTime.Now.ToString("HH:mm:ss");
toolStripStatusLabel3.Text = "行: " txtTextBox.GetLineFromCharIndex(txtTextBox.Text.Length 1) " 列: " col;
toolStripStatusLabel5.Text = "字数: " txtTextBox.Text.Length;
}
private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
{
QuestSaveText();
IsTextChanged = false;
SaveSetting();
}
private void 撤销ZToolStripMenuItem_Click(object sender, EventArgs e)
{
txtTextBox.Undo();
txtTextBox.ClearUndo();
}
private void 删除LToolStripMenuItem_Click(object sender, EventArgs e)
{
txtTextBox.Paste("");
}
private void menuStrip_MouseEnter(object sender, EventArgs e)
{
if (filePath != null) { 保存SToolStripMenuItem.Enabled = true; } else { 保存SToolStripMenuItem.Enabled = false; }
if (txtTextBox.CanUndo == true) { 撤销ZToolStripMenuItem.Enabled = true; } else { 撤销ZToolStripMenuItem.Enabled = false; }
if (txtTextBox.SelectionLength == 0)
{
剪切TToolStripMenuItem.Enabled = false;
复制CToolStripMenuItem.Enabled = false;
删除LToolStripMenuItem.Enabled = false;
}
else
{
剪切TToolStripMenuItem.Enabled = true;
复制CToolStripMenuItem.Enabled = true;
删除LToolStripMenuItem.Enabled = true;
}
if (Clipboard.GetText() != null) { 粘贴PToolStripMenuItem.Enabled = true; } else { 粘贴PToolStripMenuItem.Enabled = false; }
}
private void 菜单MToolStripMenuItem_Click(object sender, EventArgs e)
{
menuStrip.Visible = !menuStrip.Visible;
}
private void 查找FToolStripMenuItem_Click(object sender, EventArgs e)
{
frmFindText fft = new frmFindText();
fft.tb = txtTextBox;
fft.Show(this);
}
private void 查找下一个NToolStripMenuItem_Click(object sender, EventArgs e)
{
frmFindText fft = new frmFindText();
fft.tb = txtTextBox;
fft.Show(this);
}
private void 关于备注AToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutBox ab = new AboutBox();
ab.ShowDialog(this);
}
private void 替换RToolStripMenuItem_Click(object sender, EventArgs e)
{
frmReplace fr = new frmReplace();
fr.tb = txtTextBox;
fr.Show(this);
}
private void autosave()
{
if (autoSave.Enabled == true && filePath == null)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "选择自动保存文件的路径";
sfd.Filter = "文本文件(*.txt)|*.txt|配置文件(*.ini)|*.ini|所有文件(*.*)|*.*";
if (sfd.ShowDialog() == DialogResult.OK)
{
filePath = sfd.FileName;
this.Text = "备注 - " sfd.FileName.Substring(sfd.FileName.LastIndexOf('\\') 1);
}
else
{
MessageBox.Show("你没有选择自动保存的路径, 自动保存将被停止!","警告",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
自动保存AToolStripMenuItem.Checked = false;
autoSave.Enabled = false;
}
}
}
private void autoSave_Tick(object sender, EventArgs e)
{
if (filePath != null)
{
FileStream fs = new FileStream(filePath, FileMode.Create);
StreamWriter sw = new StreamWriter(fs, Encoding.Default);
sw.Write(txtTextBox.Text);
sw.Close();
fs.Close();
toolStripStatusLabel6.Text = string.Format("[自动保存: {0}]", DateTime.Now.ToString("HH:mm:ss"));
}
}
private void txtTextBox_MouseDown(object sender, MouseEventArgs e)
{
frmFindText.startIndex = 0;
}
private void SaveSetting()
{
if (保存设置SToolStripMenuItem.Checked == true)
{
StreamWriter sw = new StreamWriter(fileDataPath, false);
sw.Close();
}
if (File.Exists(fileDataPath))
{
StreamWriter sw = new StreamWriter(fileDataPath,true,Encoding.Default);
// 存坐标和宽度
sw.WriteLine(this.Location.X.ToString());
sw.WriteLine(this.Location.Y.ToString());
sw.WriteLine(this.Size.Width.ToString());
sw.WriteLine(this.Size.Height.ToString());
// 存字体样式
sw.WriteLine(txtTextBox.Font.Name.ToString());
sw.WriteLine(txtTextBox.Font.Size.ToString());
sw.WriteLine(txtTextBox.Font.Bold.ToString());
sw.WriteLine(txtTextBox.Font.Italic.ToString());
sw.WriteLine(txtTextBox.Font.Strikeout.ToString());
sw.WriteLine(txtTextBox.Font.Underline.ToString());
// 存字体颜色
sw.WriteLine(txtTextBox.ForeColor.R);
sw.WriteLine(txtTextBox.ForeColor.G);
sw.WriteLine(txtTextBox.ForeColor.B);
// 存背景颜色
sw.WriteLine(txtTextBox.BackColor.R);
sw.WriteLine(txtTextBox.BackColor.G);
sw.WriteLine(txtTextBox.BackColor.B);
sw.Close();
}
}
private void LoadSetting()
{
if (File.Exists(fileDataPath))
{
// 数据文件存在,开始读取
StreamReader sr = new StreamReader(fileDataPath,Encoding.Default);
try
{
// 读坐标和宽度
int X = Convert.ToInt32(sr.ReadLine());
int Y = Convert.ToInt32(sr.ReadLine());
int W = Convert.ToInt32(sr.ReadLine());
int H = Convert.ToInt32(sr.ReadLine());
// 读字体样式
string fn = sr.ReadLine().ToString();
float fs = Convert.ToSingle(sr.ReadLine().ToString());
string isBold = sr.ReadLine().ToString();
string isItalic = sr.ReadLine().ToString();
string isStrikeout = sr.ReadLine().ToString();
string isUnderline = sr.ReadLine().ToString();
// 读字体颜色
int R = Convert.ToInt32(sr.ReadLine());
int G = Convert.ToInt32(sr.ReadLine());
int B = Convert.ToInt32(sr.ReadLine());
// 读背景颜色
int BR = Convert.ToInt32(sr.ReadLine());
int BG = Convert.ToInt32(sr.ReadLine());
int BB = Convert.ToInt32(sr.ReadLine());
// 开始应用设置
sr.Close();
保存设置SToolStripMenuItem.Checked = true;
this.Location = new Point(X, Y);
this.Size = new Size(W, H);
txtTextBox.Font = new Font(fn, fs);
if (isBold == "True") { txtTextBox.Font = new Font(txtTextBox.Font, txtTextBox.Font.Style | FontStyle.Bold); }
if (isItalic == "True") { txtTextBox.Font = new Font(txtTextBox.Font, txtTextBox.Font.Style | FontStyle.Italic); }
if (isStrikeout == "True") { txtTextBox.Font = new Font(txtTextBox.Font, txtTextBox.Font.Style | FontStyle.Strikeout); }
if (isUnderline == "True") { txtTextBox.Font = new Font(txtTextBox.Font, txtTextBox.Font.Style | FontStyle.Underline); }
txtTextBox.BackColor = Color.FromArgb(BR, BG, BB);
txtTextBox.ForeColor = Color.FromArgb(R, G, B);
}
catch
{
/* 当文件读取失败,提示错误,并重置数据文件 */
sr.Close();
MessageBox.Show("数据文件读取失败, 所有已保存的设置将会被删除!", "备注", MessageBoxButtons.OK, MessageBoxIcon.Error);
StreamWriter sw = new StreamWriter(fileDataPath, false);
sw.Close();
}
}
}
private void frmMain_Load(object sender, EventArgs e)
{
LoadSetting();
}
private void CommandLoadFile()
{
/* 从命令行参数中获取路径并载入 */
string comPath = TextHandle.ComPath();
if (comPath != "NULL")
{
StreamReader sr = new StreamReader(comPath, Encoding.Default);
filePath = comPath;
this.Text = "书签" "[读取中]";
Application.DoEvents();
txtTextBox.Text = sr.ReadToEnd();
this.Text = "书签";
IsTextChanged = false;
sr.Close();
}
}
private void 保存设置SToolStripMenuItem_Click(object sender, EventArgs e)
{
if (保存设置SToolStripMenuItem.Checked == true)
{
StreamWriter sw = new StreamWriter(fileDataPath, false);
sw.Close();
SaveSetting();
}
else
{
if (File.Exists(fileDataPath)) { File.Delete(fileDataPath); }
}
}
private void frmMain_Shown(object sender, EventArgs e)
{
if (File.Exists(fileDataPath)) { 保存设置SToolStripMenuItem.Checked = true; } else { 保存设置SToolStripMenuItem.Checked = false; }
CommandLoadFile();
}
private void 自动保存AToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!自动保存AToolStripMenuItem.Checked) { autoSave.Enabled = false; } else { autoSave.Enabled = true; autosave(); }
}
private void 秒ToolStripMenuItem2_Click(object sender, EventArgs e)
{
秒ToolStripMenuItem2.Checked = true;
秒ToolStripMenuItem3.Checked = false;
分钟ToolStripMenuItem3.Checked = false;
分钟ToolStripMenuItem4.Checked = false;
分钟ToolStripMenuItem5.Checked = false;
自定义ToolStripMenuItem.Checked = false;
autoSave.Interval = 15000; // 15秒
}
private void 秒ToolStripMenuItem3_Click(object sender, EventArgs e)
{
秒ToolStripMenuItem2.Checked = false;
秒ToolStripMenuItem3.Checked = true;
分钟ToolStripMenuItem3.Checked = false;
分钟ToolStripMenuItem4.Checked = false;
分钟ToolStripMenuItem5.Checked = false;
自定义ToolStripMenuItem.Checked = false;
autoSave.Interval = 30000; // 30秒
}
private void 分钟ToolStripMenuItem3_Click(object sender, EventArgs e)
{
秒ToolStripMenuItem2.Checked = false;
秒ToolStripMenuItem3.Checked = false;
分钟ToolStripMenuItem3.Checked = true;
分钟ToolStripMenuItem4.Checked = false;
分钟ToolStripMenuItem5.Checked = false;
自定义ToolStripMenuItem.Checked = false;
autoSave.Interval = 600000; // 1分钟
}
private void 分钟ToolStripMenuItem4_Click(object sender, EventArgs e)
{
秒ToolStripMenuItem2.Checked = false;
秒ToolStripMenuItem3.Checked = false;
分钟ToolStripMenuItem3.Checked = false;
分钟ToolStripMenuItem4.Checked = true;
分钟ToolStripMenuItem5.Checked = false;
自定义ToolStripMenuItem.Checked = false;
autoSave.Interval = 120000; // 2分钟
}
private void 分钟ToolStripMenuItem5_Click(object sender, EventArgs e)
{
秒ToolStripMenuItem2.Checked = false;
秒ToolStripMenuItem3.Checked = false;
分钟ToolStripMenuItem3.Checked = false;
分钟ToolStripMenuItem4.Checked = false;
分钟ToolStripMenuItem5.Checked = true;
自定义ToolStripMenuItem.Checked = false;
autoSave.Interval = 300000; // 5分钟
}
private void 自定义ToolStripMenuItem_Click(object sender, EventArgs e)
{
秒ToolStripMenuItem2.Checked = false;
秒ToolStripMenuItem3.Checked = false;
分钟ToolStripMenuItem3.Checked = false;
分钟ToolStripMenuItem4.Checked = false;
分钟ToolStripMenuItem5.Checked = false;
自定义ToolStripMenuItem.Checked = true;
SetAutoSaveTimer sast = new SetAutoSaveTimer();
sast.cmTime = autoSave;
sast.ShowDialog(this);
}
private void 时间ToolStripMenuItem1_Click(object sender, EventArgs e)
{
txtTextBox.SelectedText = DateTime.Now.ToString("HH:mm:ss");
}
private void 日期ToolStripMenuItem1_Click(object sender, EventArgs e)
{
txtTextBox.SelectedText = DateTime.Now.ToString("yyyy-MM-dd");
}
private void 星期ToolStripMenuItem1_Click(object sender, EventArgs e)
{
/* 判断当前的星期并插入到光标后 */
txtTextBox.SelectedText = TextHandle.TodayWeek();
}
private void 时间日期ToolStripMenuItem1_Click(object sender, EventArgs e)
{
txtTextBox.SelectedText = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
private void 年ToolStripMenuItem1_Click(object sender, EventArgs e)
{
txtTextBox.SelectedText = DateTime.Now.ToString("yyyy");
}
private void 月ToolStripMenuItem1_Click(object sender, EventArgs e)
{
txtTextBox.SelectedText = DateTime.Now.ToString("MM");
}
private void 日ToolStripMenuItem1_Click(object sender, EventArgs e)
{
txtTextBox.SelectedText = DateTime.Now.ToString("dd");
}
private void 过滤非法字符ToolStripMenuItem_Click(object sender, EventArgs e)
{
txtTextBox.Text = TextHandle.SensitiveShield(txtTextBox.Text);
}
private void menuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
}
}
标签:
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论