使用C#语言,仿照电脑上的记事本,设计一个电子笔记本,具有基本的打开、保存、另存,以及编辑字体、更改格式、对齐换行、显示日期等相关操作。
需要用到的控件有:
menuStrip | 制作菜单栏 |
openFileDialog | 打开文件 |
saveFileDialog | 保存文件 |
colorDialog | 用于更改字体颜色 |
richTextBox | 用于文本的显示 |
功能图:
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.Windows.Forms;
9. using System.IO;
10.
11. namespace WindowsFormsApplication1
12. {
13. public partial class 简易记事本 : Form
14. {
15.
16. public 简易记事本()
17. {
18. InitializeComponent();
19. }
20.
21. private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
22. {
23. richTextBox1.Clear();
24. OpenFileDialog openFileDialog1 = new OpenFileDialog();
25. openFileDialog1.InitialDirectory = "";
26.
27. openFileDialog1.Filter = "文本文件(*.txt)|*.txt";
28. openFileDialog1.RestoreDirectory = true;
29. if (openFileDialog1.ShowDialog() == DialogResult.OK)
30. {
31. StreamReader st = new StreamReader(openFileDialog1.FileName, Encoding.GetEncoding("utf-8"));
32. string str = st.ReadLine();
33. while (str != null)
34. {
35. richTextBox1.AppendText(str);
36. richTextBox1.AppendText("\n");
37. str = st.ReadLine();
38. }
39. }
40. }
41.
42. private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
43. {
44. saveFileDialog1.Filter = "文本文件(*.txt)|*.txt";
45.
46. this.Text=saveFileDialog1.FileName;
47. }
48.
49. private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
50. {
51. saveFileDialog1.FileName = "";
52. saveFileDialog1.Filter = "文本文件(*.txt)|*.txt";
53.
54. saveFileDialog1.InitialDirectory = "";
55. saveFileDialog1.Title = "另存为";
56. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
57. {
58. StreamWriter wt = new StreamWriter(saveFileDialog1.FileName);
59. wt.Write(richTextBox1.Text);
60. wt.Close();
61. }
62. this.Text = saveFileDialog1.FileName;
63. }
64.
65. private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
66. {
67. this.Close();
68. }
69.
70. private void richTextBox1_TextChanged(object sender, EventArgs e)
71. {
72. //richboxTextHasChanged = true;
73. }
74.
75. private void 简易记事本_Load(object sender, EventArgs e)
76. {
77. //label1.Text = DateTime.Now.ToLongTimeString().ToString();
78.
79. }
80.
81.
82. private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
83. {
84. richTextBox1.Cut();
85. }
86.
87. private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
88. {
89. richTextBox1.Copy();
90. }
91.
92. private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
93. {
94. richTextBox1.Paste();
95. }
96.
97. private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
98. {
99. richTextBox1.SelectAll();
100. }
101.
102. private void 时间日期DToolStripMenuItem_Click(object sender, EventArgs e)
103. {
104. richTextBox1.AppendText("\r\n"+System.DateTime.Now.ToString());
105. }
106.
107. private void 自动换行WToolStripMenuItem_Click(object sender, EventArgs e)
108. {
109. //自动换行
110. if (this.自动换行WToolStripMenuItem.Checked)
111. {
112. richTextBox1.WordWrap = true;
113. }
114. else
115. {
116. richTextBox1.WordWrap = false;
117. }
118. }
119.
120. private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
121. {
122. FontDialog fd = new FontDialog();
123. fd.ShowDialog();
124. richTextBox1.Font = fd.Font;
125. }
126.
127. private int GetStringLen(string s)
128. {
129. if (!string.IsNullOrEmpty(s))
130. {
131. int len = s.Length;
132. for (int i = 0; i < s.Length; i++)
133. {
134. if (s[i] > 255)
135. len++;
136. }
137. return len;
138. }
139. return 0;
140. }
141.
142. private void 状态栏ToolStripMenuItem_Click(object sender, EventArgs e)
143. {
144. int row = richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart) + 1;
145. int start = richTextBox1.GetFirstCharIndexOfCurrentLine();
146. string s = richTextBox1.Text.Substring(start, richTextBox1.SelectionStart - start);
147. int col = GetStringLen(s) + 1;
148. label2.Text = "第 " + row + " 行, 第 " + col + " 列";
149. }
150.
151. private void 查看帮助HToolStripMenuItem_Click(object sender, EventArgs e)
152. {
153. System.Diagnostics.Process.Start("https://answers.microsoft.com/en-us/windows/forum/apps_windows_10");
154.
155. }
156.
157. private void 关于记事本AToolStripMenuItem_Click(object sender, EventArgs e)
158. {
159. AboutBox about = new AboutBox();
160. about.ShowDialog();
161. }
162.
163. private void 撤销RToolStripMenuItem_Click(object sender, EventArgs e)
164. {
165. richTextBox1.Undo();
166. }
167.
168. private void 左对齐ToolStripMenuItem_Click(object sender, EventArgs e)
169. {
170. richTextBox1.SelectionAlignment = HorizontalAlignment.Left;
171. }
172.
173. private void 居中ToolStripMenuItem_Click(object sender, EventArgs e)
174. {
175.
176. richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
177.
178. }
179.
180. private void 右对齐ToolStripMenuItem_Click(object sender, EventArgs e)
181. {
182. {
183. richTextBox1.SelectionAlignment = HorizontalAlignment.Right;
184. }
185.
186. }
187.
188. private void 字体颜色CToolStripMenuItem_Click(object sender, EventArgs e)
189. {
190. colorDialog1.ShowDialog();
191. richTextBox1.SelectionColor = colorDialog1.Color;
192.
193. }
194.
195. private void 背景BToolStripMenuItem_Click(object sender, EventArgs e)
196. {
197. colorDialog1.ShowDialog();
198. richTextBox1.BackColor = colorDialog1.Color;
199. }
200. }
201. }