image.png

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Threading.Tasks;
    9. using System.Windows.Forms;
    10. using System.IO;
    11. namespace _118_记事本应用程序
    12. {
    13. public partial class Form1 : Form
    14. {
    15. public Form1()
    16. {
    17. InitializeComponent();
    18. }
    19. private void Form1_Load(object sender, EventArgs e)
    20. {
    21. //加载程序的时候隐藏panel
    22. panel1.Visible = false;
    23. //取消文本框的自动换行
    24. textBox1.WordWrap = false;
    25. }
    26. /// <summary>
    27. /// 点击按钮的时候,隐藏panel
    28. /// </summary>
    29. /// <param name="sender"></param>
    30. /// <param name="e"></param>
    31. private void button1_Click(object sender, EventArgs e)
    32. {
    33. panel1.Visible = false;
    34. }
    35. private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
    36. {
    37. panel1.Visible = true;
    38. }
    39. private void 隐藏ToolStripMenuItem_Click(object sender, EventArgs e)
    40. {
    41. panel1.Visible = false;
    42. }
    43. List<string> list = new List<string>();
    44. /// <summary>
    45. /// 打开对话框
    46. /// </summary>
    47. /// <param name="sender"></param>
    48. /// <param name="e"></param>
    49. private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
    50. {
    51. OpenFileDialog ofd = new OpenFileDialog();
    52. //设置对话框标题
    53. ofd.Title = "客官,请选择要打开的文件";
    54. //设置对话框可以多选
    55. ofd.Multiselect = true;
    56. //设置对话框的初始目录
    57. ofd.InitialDirectory = @"C:\Users\46124\Desktop";
    58. //设置对话框可筛选的文件类型的内容
    59. ofd.Filter = "文本文件|*.txt|所有文件|*.*";
    60. ofd.ShowDialog();
    61. //获得在打开对话框中选中的路径
    62. string path = ofd.FileName;
    63. //将文件的全路径存储到泛型集合中
    64. list.Add(path);
    65. //获得了用户打开的文件名
    66. string fileName = Path.GetFileName(path);
    67. //将文件名放入listBox中
    68. listBox1.Items.Add(fileName);
    69. ReadFile(path);
    70. //MessageBox.Show("打开成功");
    71. }
    72. /// <summary>
    73. /// 保存用户在textBox中写入的内容
    74. /// </summary>
    75. /// <param name="sender"></param>
    76. /// <param name="e"></param>
    77. private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
    78. {
    79. SaveFileDialog sfd = new SaveFileDialog();
    80. sfd.Title = "客官,请选择你要保存的路径~";
    81. //设置对话框可筛选的文件类型的内容
    82. sfd.Filter = "文本文件|*.txt|所有文件|*.*";
    83. sfd.InitialDirectory = @"C:\Users\46124\Desktop";
    84. sfd.ShowDialog();
    85. //设置对话框的初始目录
    86. //获取用户要保存文件的路径
    87. string path = sfd.FileName;
    88. WriteFile(path);
    89. MessageBox.Show("保存成功");
    90. }
    91. /// <summary>
    92. /// 用户点击后换行,再次点击取消换行
    93. /// </summary>
    94. /// <param name="sender"></param>
    95. /// <param name="e"></param>
    96. private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
    97. {
    98. if (自动换行ToolStripMenuItem.Text == "☆自动换行")
    99. {
    100. textBox1.WordWrap = true;
    101. 自动换行ToolStripMenuItem.Text = "★取消自动换行";
    102. }
    103. else if (自动换行ToolStripMenuItem.Text == "★取消自动换行")
    104. {
    105. textBox1.WordWrap = false;
    106. 自动换行ToolStripMenuItem.Text = "☆自动换行";
    107. }
    108. }
    109. /// <summary>
    110. /// 换字体样式
    111. /// </summary>
    112. /// <param name="sender"></param>
    113. /// <param name="e"></param>
    114. private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
    115. {
    116. FontDialog fd = new FontDialog();
    117. fd.ShowDialog();
    118. textBox1.Font = fd.Font;
    119. }
    120. /// <summary>
    121. /// 更改背景色
    122. /// </summary>
    123. /// <param name="sender"></param>
    124. /// <param name="e"></param>
    125. private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
    126. {
    127. ColorDialog cd = new ColorDialog();
    128. cd.ShowDialog();
    129. textBox1.BackColor = cd.Color;
    130. }
    131. /// <summary>
    132. /// 双击历史记录打开文件
    133. /// </summary>
    134. /// <param name="sender"></param>
    135. /// <param name="e"></param>
    136. private void listBox1_DoubleClick(object sender, EventArgs e)
    137. {
    138. //获得双击文件所对应的全路径
    139. string path = list[listBox1.SelectedIndex];
    140. ReadFile(path);
    141. }
    142. /// <summary>
    143. /// 读取文件
    144. /// </summary>
    145. /// <param name="path"></param>
    146. public void ReadFile(string path)
    147. {
    148. //如果路径为空,则立即跳出函数
    149. if (path == "")
    150. {
    151. return;
    152. }
    153. using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
    154. {
    155. byte[] buffer = new byte[1024 * 1024 * 5];
    156. int readbyte = fsRead.Read(buffer, 0, buffer.Length);
    157. textBox1.Text = Encoding.UTF8.GetString(buffer, 0, readbyte);
    158. }
    159. }
    160. /// <summary>
    161. /// 写入文件
    162. /// </summary>
    163. /// <param name="path"></param>
    164. public void WriteFile(string path)
    165. {
    166. //如果路径为空,则立即跳出函数
    167. if (path == "")
    168. {
    169. return;
    170. }
    171. using (FileStream fsWrite = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
    172. {
    173. byte[] buffer = Encoding.UTF8.GetBytes(textBox1.Text);
    174. fsWrite.Write(buffer, 0, buffer.Length);
    175. }
    176. }
    177. }
    178. }