文本编辑器实例
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
8using System.IO;
9using System.Drawing.Printing;
10
11namespace WindowsApplication1
12{
13 public partial class TextEditor : Form
14 {
15 private string filename;
16 public TextEditor(WindowsApplication1.Main parent)
17 {
18 this.MdiParent = parent;
19 InitializeComponent();
20 }
21
22 private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
23 {
24
25 if (MessageBox.Show("你确定要退出么?", "退出", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)==DialogResult.Yes )
26 this.Close();
27
28
29}
30
31 private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
32 {
33
34 openFileDialog1.Title = "打开";
35 //openFileDialog1.InitialDirectory=@"d:\program files"; //初始目录,注意不要使用硬编码的目录字符串,可能该目录不存在
36 openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); //打开系统定义的目录
37
38 openFileDialog1.Filter = "文本文件 (*.txt)|*.txt|VB程序文件 (*.vb)|*.vb |所有文件 (*.*)|*.*|C#类型文件 (*.cs;*.cd)|*.cs;*.cd";
39 openFileDialog1.FilterIndex = 3; //默认选择过滤类型 从1开始
40
41 if (openFileDialog1.ShowDialog() == DialogResult.OK)
42 {
43 filename=openFileDialog1.FileName;
44 // listBox1.Items.Add(openFileDialog1.FileName);
45 //foreach (string fl in openFileDialog1.FileNames)
46 //{
47 // listBox1.Items.Add(fl);
48 //}
49
50 OpenFile();
51
52 GetFileName();
53
54 }
55 }
56
57 private void OpenFile()
58 {
59 try
60 {
61
62 textBox1.Clear();
63 textBox1.Text = File.ReadAllText(filename, Encoding.Default);
64 //textBox1.Text = File.ReadAllText(filename);
65 //StringBuilder strb = new StringBuilder();
66
67 //using (StreamReader sr = new StreamReader(filename))
68 //{
69 // while (sr.Peek() >= 0)
70 // {
71 // //Console.WriteLine(sr.ReadLine());
72 // strb.Append(sr.ReadLine() );
73 // }
74 //}
75 //textBox1.Text = strb.ToString();
76 }
77 catch (IOException ex)
78 {
79 MessageBox.Show(ex.Message, "TextEditor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
80 }
81
82 }
83
84 private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
85 {
86 if (!(filename == null))
87 SaveFile();
88 else
89 {
90
91 if (saveFileDialog1.ShowDialog() == DialogResult.OK)
92 {
93 filename = saveFileDialog1.FileName;
94 SaveFile();
95 }
96 }
97 }
98
99 private void SaveFile()
100 {
101 try
102 {
103 File.WriteAllText(filename, textBox1.Text);
104 }
105 catch (IOException ex)
106 {
107 MessageBox.Show(ex.Message, "TextEditor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
108 }
109 }
110
111 private void TextEditor_Load(object sender, EventArgs e)
112 {
113 //textBox1.Clear();
114 //filename = "未命名.txt";
115 }
116
117 private void toolStripMenuItem1_Click(object sender, EventArgs e)
118 {
119 if (saveFileDialog1.ShowDialog() == DialogResult.OK)
120 {
121 filename = saveFileDialog1.FileName;
122 SaveFile();
123 }
124 }
125
126 private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
127 {
128
129 textBox1.Clear();
130 filename = null;
131 this.Text = "TextEditor";
132
133 }
134
135 private void GetFileName()
136 {
137 FileInfo fi = new FileInfo(filename);
138 this.Text = fi.Name + " -TextEditor";
139 }
140
141 private string[] lines;
142 private int linesPrinted;
143
144 private void OnPrintPage(object sender, PrintPageEventArgs e)
145 {
146 int x = e.MarginBounds.Left ;
147 int y = e.MarginBounds.Top ;
148
149 while (linesPrinted < lines.Length)
150 {
151 e.Graphics.DrawString(lines[linesPrinted++], new Font("Arial", 10), Brushes.Black, x, y);
152 y += 15;
153 if (y >= e.PageBounds.Bottom )
154 {
155 e.HasMorePages = true;
156 return;
157 }
158 }
159
160 linesPrinted = 0;
161 e.HasMorePages = false;
162 }
163
164
165
166 private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
167 {
168 try
169 {
170 if (printDialog1.ShowDialog() == DialogResult.OK)
171 {
172 printDocument1.Print();
173 }
174 }
175 catch (InvalidPrinterException ex)
176 {
177 MessageBox.Show(ex.Message, "Text Editor", MessageBoxButtons.OK, MessageBoxIcon.Error);
178 }
179
180 }
181
182 private void OnBeginPrint(object sender, PrintEventArgs e)
183 {
184 char[] param ={ '\n' };
185 lines = textBox1.Text.Split(param);
186 int i = 0;
187 char[] trimParam ={ '\r' };
188 foreach (string s in lines)
189 {
190 lines[i++] = s.TrimEnd(trimParam);
191 }
192 }
193
194 private void OnEndPrint(object sender, PrintEventArgs e)
195 {
196 lines = null;
197 }
198
199 private void 页面设置UToolStripMenuItem_Click(object sender, EventArgs e)
200 {
201 pageSetupDialog1.ShowDialog();
202 }
203
204 private void 打印预览ToolStripMenuItem_Click(object sender, EventArgs e)
205 {
206 printPreviewDialog1.ShowDialog();
207
208 }
209
210 }
211}
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
8using System.IO;
9using System.Drawing.Printing;
10
11namespace WindowsApplication1
12{
13 public partial class TextEditor : Form
14 {
15 private string filename;
16 public TextEditor(WindowsApplication1.Main parent)
17 {
18 this.MdiParent = parent;
19 InitializeComponent();
20 }
21
22 private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
23 {
24
25 if (MessageBox.Show("你确定要退出么?", "退出", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)==DialogResult.Yes )
26 this.Close();
27
28
29}
30
31 private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
32 {
33
34 openFileDialog1.Title = "打开";
35 //openFileDialog1.InitialDirectory=@"d:\program files"; //初始目录,注意不要使用硬编码的目录字符串,可能该目录不存在
36 openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); //打开系统定义的目录
37
38 openFileDialog1.Filter = "文本文件 (*.txt)|*.txt|VB程序文件 (*.vb)|*.vb |所有文件 (*.*)|*.*|C#类型文件 (*.cs;*.cd)|*.cs;*.cd";
39 openFileDialog1.FilterIndex = 3; //默认选择过滤类型 从1开始
40
41 if (openFileDialog1.ShowDialog() == DialogResult.OK)
42 {
43 filename=openFileDialog1.FileName;
44 // listBox1.Items.Add(openFileDialog1.FileName);
45 //foreach (string fl in openFileDialog1.FileNames)
46 //{
47 // listBox1.Items.Add(fl);
48 //}
49
50 OpenFile();
51
52 GetFileName();
53
54 }
55 }
56
57 private void OpenFile()
58 {
59 try
60 {
61
62 textBox1.Clear();
63 textBox1.Text = File.ReadAllText(filename, Encoding.Default);
64 //textBox1.Text = File.ReadAllText(filename);
65 //StringBuilder strb = new StringBuilder();
66
67 //using (StreamReader sr = new StreamReader(filename))
68 //{
69 // while (sr.Peek() >= 0)
70 // {
71 // //Console.WriteLine(sr.ReadLine());
72 // strb.Append(sr.ReadLine() );
73 // }
74 //}
75 //textBox1.Text = strb.ToString();
76 }
77 catch (IOException ex)
78 {
79 MessageBox.Show(ex.Message, "TextEditor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
80 }
81
82 }
83
84 private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
85 {
86 if (!(filename == null))
87 SaveFile();
88 else
89 {
90
91 if (saveFileDialog1.ShowDialog() == DialogResult.OK)
92 {
93 filename = saveFileDialog1.FileName;
94 SaveFile();
95 }
96 }
97 }
98
99 private void SaveFile()
100 {
101 try
102 {
103 File.WriteAllText(filename, textBox1.Text);
104 }
105 catch (IOException ex)
106 {
107 MessageBox.Show(ex.Message, "TextEditor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
108 }
109 }
110
111 private void TextEditor_Load(object sender, EventArgs e)
112 {
113 //textBox1.Clear();
114 //filename = "未命名.txt";
115 }
116
117 private void toolStripMenuItem1_Click(object sender, EventArgs e)
118 {
119 if (saveFileDialog1.ShowDialog() == DialogResult.OK)
120 {
121 filename = saveFileDialog1.FileName;
122 SaveFile();
123 }
124 }
125
126 private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
127 {
128
129 textBox1.Clear();
130 filename = null;
131 this.Text = "TextEditor";
132
133 }
134
135 private void GetFileName()
136 {
137 FileInfo fi = new FileInfo(filename);
138 this.Text = fi.Name + " -TextEditor";
139 }
140
141 private string[] lines;
142 private int linesPrinted;
143
144 private void OnPrintPage(object sender, PrintPageEventArgs e)
145 {
146 int x = e.MarginBounds.Left ;
147 int y = e.MarginBounds.Top ;
148
149 while (linesPrinted < lines.Length)
150 {
151 e.Graphics.DrawString(lines[linesPrinted++], new Font("Arial", 10), Brushes.Black, x, y);
152 y += 15;
153 if (y >= e.PageBounds.Bottom )
154 {
155 e.HasMorePages = true;
156 return;
157 }
158 }
159
160 linesPrinted = 0;
161 e.HasMorePages = false;
162 }
163
164
165
166 private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
167 {
168 try
169 {
170 if (printDialog1.ShowDialog() == DialogResult.OK)
171 {
172 printDocument1.Print();
173 }
174 }
175 catch (InvalidPrinterException ex)
176 {
177 MessageBox.Show(ex.Message, "Text Editor", MessageBoxButtons.OK, MessageBoxIcon.Error);
178 }
179
180 }
181
182 private void OnBeginPrint(object sender, PrintEventArgs e)
183 {
184 char[] param ={ '\n' };
185 lines = textBox1.Text.Split(param);
186 int i = 0;
187 char[] trimParam ={ '\r' };
188 foreach (string s in lines)
189 {
190 lines[i++] = s.TrimEnd(trimParam);
191 }
192 }
193
194 private void OnEndPrint(object sender, PrintEventArgs e)
195 {
196 lines = null;
197 }
198
199 private void 页面设置UToolStripMenuItem_Click(object sender, EventArgs e)
200 {
201 pageSetupDialog1.ShowDialog();
202 }
203
204 private void 打印预览ToolStripMenuItem_Click(object sender, EventArgs e)
205 {
206 printPreviewDialog1.ShowDialog();
207
208 }
209
210 }
211}