public partial class Form1 : Form
{
bool IsTextChanged = false;
public Form1()
{
InitializeComponent();
}
private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
{
this.txtText.Text = "" ;
IsTextChanged = false;
}
private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "打开文件";
ofd.Filter = "文本文件|*.txt|所有文件|*.*";
if(ofd.ShowDialog()==DialogResult.OK)
{
//第一步:声明一个文件流
FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
//第二步:创建读写器
StreamReader sr=new StreamReader(fs,Encoding.Default);
//第三步:读操作
//xtText.Text = sr.ReadToEnd(); //读到最后结束
while(sr.EndOfStream==false)
{
string line=sr.ReadLine(); //读取一行
txtText.Text=txtText.Text+line+"\r\n";
}
//第四步:关闭读写器
sr.Close();
//第五步:关闭文件流
fs.Close();
}
IsTextChanged = false;
}
private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveText();
}
protected void SaveText()
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "保存文件";
sfd.Filter = "文本文件|*.txt|所有文件|*.*";
if (sfd.ShowDialog() == DialogResult.OK)
{
//声明一个文件流
FileStream fs = new FileStream(sfd.FileName, FileMode.Create, FileAccess.Write);
//创建写入器
StreamWriter sw = new StreamWriter(fs, Encoding.Default);
//写入操作
sw.Write(txtText.Text);
//关闭写入器
sw.Close();
//关闭文件流
fs.Close();
}
}
private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void txtText_TextChanged(object sender, EventArgs e)
{
IsTextChanged = true;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (IsTextChanged == true)
{
DialogResult dr=MessageBox.Show("文本内容已经改变,是否保存?", "询问", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
//保存文件
SaveText();
}
else if (dr == DialogResult.No)
{
//取消保存
}
else
{
e.Cancel = true;
}
}
//记事本关闭时保存窗体的位置及大小,再次打开时记忆上次关闭时的状态
int x = this.Location.X;
int y = this.Location.Y;
int w = this.Size.Width;
int h = this.Size.Height;
//规定一个配置文件叫 app.dll,,其实是一个文本文档
StreamWriter sw = new StreamWriter(Application.StartupPath + "app.dll");
sw.WriteLine(x.ToString());
sw.WriteLine(y.ToString());
sw.WriteLine(w.ToString());
sw.WriteLine(h.ToString());
sw.WriteLine(txtText.ForeColor.R);
sw.WriteLine(txtText.ForeColor.G); //把关闭时的字体颜色也保存下来
sw.WriteLine(txtText.ForeColor.B);
sw.Close();
}
private void 剪切SToolStripMenuItem_Click(object sender, EventArgs e)
{
txtText.Cut();
}
private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
{
txtText.Copy();
}
private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
{
txtText.Paste();
}
private void 查找杏仁ToolStripMenuItem_Click(object sender, EventArgs e)
{
int currPos = txtText.SelectionStart;
int findPos = txtText.Text.IndexOf("杏仁", currPos);
if (findPos != -1)
{
//找到杏仁了
txtText.Select(findPos, 2);
}
}
private void 字体FToolStripMenuItem_Click(object sender, EventArgs e)
{
FontDialog fd = new FontDialog();
fd.Font = txtText.Font;
if (fd.ShowDialog() == DialogResult.OK)
{
txtText.Font = fd.Font;
}
}
private void 字体颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog();
cd.Color = txtText.ForeColor;
if (cd.ShowDialog() == DialogResult.OK)
{
txtText.ForeColor = cd.Color;
}
}
private void 背景颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog();
cd.Color = txtText.BackColor;
if (cd.ShowDialog() == DialogResult.OK)
{
txtText.BackColor = cd.Color;
}
}
private void Form1_Load(object sender, EventArgs e)
{
string appPath = Application.StartupPath + "app.dll";
if (File.Exists(appPath))
{
StreamReader sr = new StreamReader(appPath);
int x = Convert.ToInt32(sr.ReadLine());
int y = Convert.ToInt32(sr.ReadLine());
int w = Convert.ToInt32(sr.ReadLine());
int h = Convert.ToInt32(sr.ReadLine());
int r = Convert.ToInt32(sr.ReadLine());
int g = Convert.ToInt32(sr.ReadLine());
int b = Convert.ToInt32(sr.ReadLine());
this.Location = new Point(x, y);
this.Size = new Size(w, h);
this.txtText.ForeColor = Color.FromArgb(r, g, b);
sr.Close();
}
}
}
自己写的记事本程序(功能不完善)
最新推荐文章于 2021-11-16 15:57:17 发布