Posted on 2009-04-22 23:56
火山工作室 阅读(595)
评论(0) 编辑 收藏 引用 网摘 所属分类:
高职IT教育
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
8
9namespace TextWriter
10{
11 public partial class MainForm : Form
12 {
13 public int k = 1;//子窗口的序号
14 RichTextBox contents = null;//获取活动窗体中的文本框控件
15 public System.Windows.Forms.ToolStripMenuItem menuWrap;
16 public MainForm()
17 {
18 InitializeComponent();
19 }
20 private bool ISGetContents()
21 {
22 contents = (RichTextBox)this.ActiveMdiChild.ActiveControl;
23 if (contents != null)
24 return true;
25 else
26 return false;
27 }
28 private void menuNew_Click(object sender, EventArgs e)
29 {
30 Form2 f = new Form2();
31 k++;
32 f.Text = "Form" + k;
33 f.MdiParent = this;
34 f.Show();
35 contents = (RichTextBox)f.ActiveControl;
36 }
37
38 private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
39 {
40 this.openFileDialog1.Filter = "Text.File(*.txt)|*.txt|Rtf File(*.rtf)|*.rtf|All File(*.*)|*.*";
41 if (ISGetContents() && openFileDialog1.ShowDialog() == DialogResult.OK)
42 if (openFileDialog1.FilterIndex == 1)
43 contents.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.PlainText);
44 else if (openFileDialog1.FilterIndex == 2)
45 contents.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.RichText);
46 else
47 contents.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.UnicodePlainText);
48 this.ActiveMdiChild.Text = openFileDialog1.FileName;
49
50 }
51
52 private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
53 {
54 this.saveFileDialog1.DefaultExt = "*.txt"; //默认的扩展名
55 saveFileDialog1.Filter = "txt Files|x.txt"; //添加选择文件类型
56 if (saveFileDialog1.ShowDialog() == DialogResult.OK && saveFileDialog1.FileName.Length > 0) //判断是否单击“打开”,是否选择文件。
57 if (ISGetContents())
58 contents.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
59
60 }
61
62 private void 关闭XToolStripMenuItem_Click(object sender, EventArgs e)
63 {
64 Application.Exit();
65 }
66
67 private void 撤消ToolStripMenuItem_Click(object sender, EventArgs e)
68 {
69 if (ISGetContents())
70 contents.Undo();
71 }
72
73 private void 恢复ToolStripMenuItem_Click(object sender, EventArgs e)
74 {
75 if (ISGetContents())
76 contents.Redo();
77 }
78
79 private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
80 {
81 if (ISGetContents())
82 contents.Cut();
83 }
84
85 private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
86 {
87 if (ISGetContents())
88 contents.Copy();
89 }
90
91 private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
92 {
93 if (ISGetContents())
94 contents.SelectAll();
95 }
96
97 private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
98 {
99 if (ISGetContents())
100 contents.Paste();
101 }
102
103 private void 查找ToolStripMenuItem_Click(object sender, EventArgs e)
104 {
105 if (ISGetContents())
106 {
107 contents.Focus(); //设置焦点
108 search se = new search();
109 se.ShowDialog(); //打开查找窗口
110 if (se.DialogResult == DialogResult.OK)
111 {
112 contents.Find(se.Searchtext); //查找
113 }
114 }
115 }
116
117 private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
118 {
119 this.fontDialog1.ShowDialog();
120 if (ISGetContents())
121 contents.SelectionFont = fontDialog1.Font;
122 }
123
124 private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
125 {
126 this.colorDialog1.ShowDialog();
127 if (ISGetContents())
128 contents.SelectionColor = colorDialog1.Color;
129 }
130
131 private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
132 {
133 if (ISGetContents())
134 {
135
136 if (menuWrap.Checked == true)
137 {
138 contents.WordWrap = false;
139 }
140 else
141 {
142 contents.WordWrap = true;
143 }
144 }
145 }
146
147 private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
148 {
149 help h = new help();
150 h.ShowDialog();
151 }
152
153 private void MainForm_Load(object sender, EventArgs e)
154 {
155
156 Form2 f = new Form2();
157 f.Text = "Form" + k;
158 f.MdiParent = this;
159 f.Show();
160 contents = (RichTextBox)f.ActiveControl;
161 contents.WordWrap = false;
162 }
163
164 private void 水平排列ToolStripMenuItem_Click(object sender, EventArgs e)
165 {
166 this.LayoutMdi(MdiLayout.TileHorizontal);
167 }
168
169 private void 重叠ToolStripMenuItem_Click(object sender, EventArgs e)
170 {
171 this.LayoutMdi(MdiLayout.Cascade);
172 }
173
174 private void 垂直排列ToolStripMenuItem_Click(object sender, EventArgs e)
175 {
176 this.LayoutMdi(MdiLayout.TileVertical);
177 }
178
179 private void toolStripButton1_Click(object sender, EventArgs e)
180 {
181 this.menuNew_Click(sender, e);
182 }
183
184 private void toolStripButton2_Click(object sender, EventArgs e)
185 {
186 this.打开OToolStripMenuItem_Click(sender, e);
187 }
188
189 private void toolStripButton3_Click(object sender, EventArgs e)
190 {
191 this.字体ToolStripMenuItem_Click(sender, e);
192 }
193
194 private void toolStripButton4_Click(object sender, EventArgs e)
195 {
196 this.颜色ToolStripMenuItem_Click(sender, e);
197 }
198
199 private void toolStripButton5_Click(object sender, EventArgs e)
200 {
201 this.关于ToolStripMenuItem_Click(sender, e);
202 }
203
204 }
205}