using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace _118_记事本应用程序
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//加载程序的时候隐藏panel
panel1.Visible = false;
//取消文本框的自动换行
textBox1.WordWrap = false;
}
/// <summary>
/// 点击按钮的时候,隐藏panel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
panel1.Visible = false;
}
private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
{
panel1.Visible = true;
}
private void 隐藏ToolStripMenuItem_Click(object sender, EventArgs e)
{
panel1.Visible = false;
}
List<string> list = new List<string>();
/// <summary>
/// 打开对话框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
//设置对话框标题
ofd.Title = "客官,请选择要打开的文件";
//设置对话框可以多选
ofd.Multiselect = true;
//设置对话框的初始目录
ofd.InitialDirectory = @"C:\Users\46124\Desktop";
//设置对话框可筛选的文件类型的内容
ofd.Filter = "文本文件|*.txt|所有文件|*.*";
ofd.ShowDialog();
//获得在打开对话框中选中的路径
string path = ofd.FileName;
//将文件的全路径存储到泛型集合中
list.Add(path);
//获得了用户打开的文件名
string fileName = Path.GetFileName(path);
//将文件名放入listBox中
listBox1.Items.Add(fileName);
ReadFile(path);
//MessageBox.Show("打开成功");
}
/// <summary>
/// 保存用户在textBox中写入的内容
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "客官,请选择你要保存的路径~";
//设置对话框可筛选的文件类型的内容
sfd.Filter = "文本文件|*.txt|所有文件|*.*";
sfd.InitialDirectory = @"C:\Users\46124\Desktop";
sfd.ShowDialog();
//设置对话框的初始目录
//获取用户要保存文件的路径
string path = sfd.FileName;
WriteFile(path);
MessageBox.Show("保存成功");
}
/// <summary>
/// 用户点击后换行,再次点击取消换行
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (自动换行ToolStripMenuItem.Text == "☆自动换行")
{
textBox1.WordWrap = true;
自动换行ToolStripMenuItem.Text = "★取消自动换行";
}
else if (自动换行ToolStripMenuItem.Text == "★取消自动换行")
{
textBox1.WordWrap = false;
自动换行ToolStripMenuItem.Text = "☆自动换行";
}
}
/// <summary>
/// 换字体样式
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
{
FontDialog fd = new FontDialog();
fd.ShowDialog();
textBox1.Font = fd.Font;
}
/// <summary>
/// 更改背景色
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog();
cd.ShowDialog();
textBox1.BackColor = cd.Color;
}
/// <summary>
/// 双击历史记录打开文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listBox1_DoubleClick(object sender, EventArgs e)
{
//获得双击文件所对应的全路径
string path = list[listBox1.SelectedIndex];
ReadFile(path);
}
/// <summary>
/// 读取文件
/// </summary>
/// <param name="path"></param>
public void ReadFile(string path)
{
//如果路径为空,则立即跳出函数
if (path == "")
{
return;
}
using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
{
byte[] buffer = new byte[1024 * 1024 * 5];
int readbyte = fsRead.Read(buffer, 0, buffer.Length);
textBox1.Text = Encoding.UTF8.GetString(buffer, 0, readbyte);
}
}
/// <summary>
/// 写入文件
/// </summary>
/// <param name="path"></param>
public void WriteFile(string path)
{
//如果路径为空,则立即跳出函数
if (path == "")
{
return;
}
using (FileStream fsWrite = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
{
byte[] buffer = Encoding.UTF8.GetBytes(textBox1.Text);
fsWrite.Write(buffer, 0, buffer.Length);
}
}
}
}