-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
208 lines (182 loc) · 7.97 KB
/
Form1.cs
File metadata and controls
208 lines (182 loc) · 7.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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;
using System.Drawing.Printing;
namespace notepad
{
public partial class Form1 : Form
{
private int findPostion = 0;
private string s_FileName="";
private object s;
private EventArgs ea;
private StringReader streamToPrint = null;
private Font printFont;
public Form1()
{
InitializeComponent();
新建ToolStripMenuItem_Click(s,ea);
}
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Text = "";//或richTextBox1.Clear();
s_FileName = "";//新建文件没有文件名。s_FileName为字段成员。
}
private void 文件ToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
s_FileName = openFileDialog1.FileName;
richTextBox1.LoadFile(openFileDialog1.FileName,
RichTextBoxStreamType.PlainText);
}
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (s_FileName.Length != 0)
richTextBox1.SaveFile(s_FileName, RichTextBoxStreamType.PlainText);
else
menuItemFileSaveAs_Click(sender, e);//调用另存为菜单项事件处理函数
}
private void menuItemFileSaveAs_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
s_FileName = saveFileDialog1.FileName;
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
this.Text = s_FileName;
}
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
Application.Exit();
}
private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Cut();
}
private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Copy();
}
private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Paste();
}
private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Undo();
}
private void 恢复ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Redo();
}
public void FindRichTextBoxString(string FindString)
{
if (findPostion >= richTextBox1.Text.Length)//已查到文本底部
{
MessageBox.Show("已到文本底部,再次查找将从文本开始处查找","提示", MessageBoxButtons.OK);
findPostion = 0;
return;
}//下边语句进行查找,返回找到的位置,返回-1,表示未找到,参数1是要找的字符串
//参数2是查找的开始位置,参数3是查找的一些选项,如大小写是否匹配,查找方向等
findPostion = richTextBox1.Find(FindString,
findPostion, RichTextBoxFinds.MatchCase);
if (findPostion == -1)//如果未找到
{
MessageBox.Show("已到文本底部,再次查找将从文本开始处查找","提示", MessageBoxButtons.OK);
findPostion = 0;//下次查找的开始位置
}
else//已找到
{
richTextBox1.Focus();//主窗体获得焦点
findPostion += FindString.Length;
}//下次查找的开始位置在此次找到字符串之后
}
public void ReplaceRichTextBoxString(string ReplaceString)
{
if (richTextBox1.SelectedText.Length != 0)//如果选取了字符串
richTextBox1.SelectedText = ReplaceString;//替换被选的字符串
}
private void 查找和替换ToolStripMenuItem_Click(object sender, EventArgs e)
{
findPostion = 0;
formFindReplace FindReplaceDialog = new formFindReplace(this);//注意this
FindReplaceDialog.Show();//打开非模式对话框使用Show()方法
}
private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (fontDialog1.ShowDialog() == DialogResult.OK)
richTextBox1.SelectionFont = fontDialog1.Font;
}
private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (fontDialog1.ShowDialog() == DialogResult.OK)
richTextBox1.SelectionColor = colorDialog1.Color;
}
private void printDocument1_BeginPrint(object sender,System.Drawing.Printing.PrintEventArgs e)
{
printFont = richTextBox1.Font;//打印使用的字体
streamToPrint = new StringReader(richTextBox1.Text);//打印richTextBox1.Text
}//如预览文件改为:streamToPrint=new StreamReader("文件的路径及文件名");
private void printDocument1_PrintPage(object sender,System.Drawing.Printing.PrintPageEventArgs e)
{
float linesPerPage = 0;//记录每页最大行数
float yPos = 0;//记录将要打印的一行数据在垂直方向的位置
int count = 0;//记录每页已打印行数
float leftMargin = e.MarginBounds.Left;//左边距
float topMargin = e.MarginBounds.Top;//顶边距
string line = null;//从RichTextBox中读取一段字符将存到line中
//每页最大行数=一页纸打印区域的高度/一行字符的高度
linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics);
//如果当前页已打印行数小于每页最大行数而且读出数据不为null,继续打印
while (count < linesPerPage && ((line = streamToPrint.ReadLine()) != null))
{ //yPos为要打印的当前行在垂直方向上的位置
yPos = topMargin + (count * printFont.GetHeight(e.Graphics));
e.Graphics.DrawString(line, printFont, Brushes.Black,leftMargin, yPos, new StringFormat());//打印,参见第五章
count++;//已打印行数加1
}
if (line != null)//是否需要打印下一页
e.HasMorePages = true;//需要打印下一页
else
e.HasMorePages = false;//不需要打印下一页
}
private void printDocument1_EndPrint(object sender,System.Drawing.Printing.PrintEventArgs e)
{
if (streamToPrint != null)
streamToPrint.Close();//释放不用的资源
}
private void 页面设置ToolStripMenuItem_Click(object sender, EventArgs e)
{
pageSetupDialog1.Document = printDocument1;
pageSetupDialog1.ShowDialog();
}
private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)
{
printDialog1.Document = printDocument1;
if (printDialog1.ShowDialog(this) == DialogResult.OK)
printDocument1.Print();
}
private void 打印预览ToolStripMenuItem_Click(object sender, EventArgs e)
{
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
}
private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
{
formAbout formAboutDialig = new formAbout();//注意this
formAboutDialig.Show();//打开非模式对话框使用Show()方法
}
}
}