文本编辑器实例

  1 None.gif using  System;
  2 None.gif using  System.Collections.Generic;
  3 None.gif using  System.ComponentModel;
  4 None.gif using  System.Data;
  5 None.gif using  System.Drawing;
  6 None.gif using  System.Text;
  7 None.gif using  System.Windows.Forms;
  8 None.gif using  System.IO;
  9 None.gif using  System.Drawing.Printing;
 10 None.gif
 11 None.gif namespace  WindowsApplication1
 12 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 13InBlock.gif    public partial class TextEditor : Form
 14ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 15InBlock.gif        private string filename;
 16InBlock.gif        public TextEditor(WindowsApplication1.Main parent)
 17ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 18InBlock.gif            this.MdiParent = parent;
 19InBlock.gif            InitializeComponent();
 20ExpandedSubBlockEnd.gif        }

 21InBlock.gif
 22InBlock.gif        private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
 23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 24InBlock.gif
 25InBlock.gif           if (MessageBox.Show("你确定要退出么?""退出", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)==DialogResult.Yes )
 26InBlock.gif               this.Close();
 27InBlock.gif
 28InBlock.gif
 29ExpandedSubBlockEnd.gif}

 30InBlock.gif        
 31InBlock.gif        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
 32ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 33InBlock.gif           
 34InBlock.gif             openFileDialog1.Title = "打开";
 35InBlock.gif            //openFileDialog1.InitialDirectory=@"d:\program files"; //初始目录,注意不要使用硬编码的目录字符串,可能该目录不存在
 36InBlock.gif            openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); //打开系统定义的目录
 37InBlock.gif
 38InBlock.gif            openFileDialog1.Filter = "文本文件 (*.txt)|*.txt|VB程序文件 (*.vb)|*.vb |所有文件 (*.*)|*.*|C#类型文件 (*.cs;*.cd)|*.cs;*.cd";
 39InBlock.gif            openFileDialog1.FilterIndex = 3//默认选择过滤类型 从1开始
 40InBlock.gif
 41InBlock.gif            if (openFileDialog1.ShowDialog() == DialogResult.OK)
 42ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 43InBlock.gif                filename=openFileDialog1.FileName;
 44InBlock.gif                //    listBox1.Items.Add(openFileDialog1.FileName);
 45InBlock.gif                //foreach (string fl in openFileDialog1.FileNames)
 46InBlock.gif                //{
 47InBlock.gif                //    listBox1.Items.Add(fl);
 48InBlock.gif                //}
 49InBlock.gif
 50InBlock.gif                OpenFile();
 51InBlock.gif
 52InBlock.gif                GetFileName();
 53InBlock.gif                
 54ExpandedSubBlockEnd.gif            }

 55ExpandedSubBlockEnd.gif        }

 56InBlock.gif
 57InBlock.gif        private void OpenFile()
 58ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 59InBlock.gif            try
 60ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 61InBlock.gif                
 62InBlock.gif                textBox1.Clear();
 63InBlock.gif                textBox1.Text = File.ReadAllText(filename, Encoding.Default);
 64InBlock.gif                //textBox1.Text = File.ReadAllText(filename);
 65InBlock.gif                //StringBuilder strb = new StringBuilder();
 66InBlock.gif
 67InBlock.gif                //using (StreamReader sr = new StreamReader(filename))
 68InBlock.gif                //{
 69InBlock.gif                //    while (sr.Peek() >= 0)
 70InBlock.gif                //    {
 71InBlock.gif                //        //Console.WriteLine(sr.ReadLine());
 72InBlock.gif                //        strb.Append(sr.ReadLine() );
 73InBlock.gif                //    }
 74InBlock.gif                //}
 75InBlock.gif                //textBox1.Text = strb.ToString();
 76ExpandedSubBlockEnd.gif            }

 77InBlock.gif            catch (IOException ex)
 78ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 79InBlock.gif                MessageBox.Show(ex.Message, "TextEditor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
 80ExpandedSubBlockEnd.gif            }

 81InBlock.gif           
 82ExpandedSubBlockEnd.gif        }

 83InBlock.gif
 84InBlock.gif        private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
 85ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 86InBlock.gif            if (!(filename == null))
 87InBlock.gif                SaveFile();
 88InBlock.gif            else
 89ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 90InBlock.gif
 91InBlock.gif                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
 92ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 93InBlock.gif                    filename = saveFileDialog1.FileName;
 94InBlock.gif                    SaveFile();
 95ExpandedSubBlockEnd.gif                }

 96ExpandedSubBlockEnd.gif            }

 97ExpandedSubBlockEnd.gif        }

 98InBlock.gif
 99InBlock.gif        private void SaveFile()
100ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
101InBlock.gif             try
102ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
103InBlock.gif                File.WriteAllText(filename, textBox1.Text);
104ExpandedSubBlockEnd.gif            }

105InBlock.gif            catch (IOException ex)
106ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
107InBlock.gif                MessageBox.Show(ex.Message, "TextEditor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
108ExpandedSubBlockEnd.gif            }

109ExpandedSubBlockEnd.gif        }

110InBlock.gif
111InBlock.gif        private void TextEditor_Load(object sender, EventArgs e)
112ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
113InBlock.gif            //textBox1.Clear();
114InBlock.gif            //filename = "未命名.txt";
115ExpandedSubBlockEnd.gif        }

116InBlock.gif
117InBlock.gif        private void toolStripMenuItem1_Click(object sender, EventArgs e)
118ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
119InBlock.gif            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
120ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
121InBlock.gif                filename = saveFileDialog1.FileName;
122InBlock.gif                SaveFile();
123ExpandedSubBlockEnd.gif            }

124ExpandedSubBlockEnd.gif        }

125InBlock.gif
126InBlock.gif        private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
127ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
128InBlock.gif           
129InBlock.gif                    textBox1.Clear();
130InBlock.gif                    filename = null;
131InBlock.gif                    this.Text = "TextEditor";
132InBlock.gif               
133ExpandedSubBlockEnd.gif        }

134InBlock.gif
135InBlock.gif        private void GetFileName()
136ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
137InBlock.gif            FileInfo fi = new FileInfo(filename);
138InBlock.gif            this.Text = fi.Name + " -TextEditor";
139ExpandedSubBlockEnd.gif        }

140InBlock.gif
141InBlock.gif        private string[] lines;
142InBlock.gif        private int linesPrinted;
143InBlock.gif
144InBlock.gif        private void OnPrintPage(object sender, PrintPageEventArgs e)
145ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
146InBlock.gif            int x = e.MarginBounds.Left ;
147InBlock.gif            int y = e.MarginBounds.Top ;
148InBlock.gif
149InBlock.gif            while (linesPrinted < lines.Length)
150ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
151InBlock.gif                e.Graphics.DrawString(lines[linesPrinted++], new Font("Arial"10), Brushes.Black, x, y);
152InBlock.gif                y += 15;
153InBlock.gif                if (y >= e.PageBounds.Bottom )
154ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
155InBlock.gif                    e.HasMorePages = true;
156InBlock.gif                    return;
157ExpandedSubBlockEnd.gif                }

158ExpandedSubBlockEnd.gif            }

159InBlock.gif
160InBlock.gif            linesPrinted = 0;
161InBlock.gif            e.HasMorePages = false;
162ExpandedSubBlockEnd.gif        }

163InBlock.gif
164InBlock.gif     
165InBlock.gif
166InBlock.gif        private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
167ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
168InBlock.gif            try
169ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
170InBlock.gif                if (printDialog1.ShowDialog() == DialogResult.OK)
171ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
172InBlock.gif                    printDocument1.Print();
173ExpandedSubBlockEnd.gif                }

174ExpandedSubBlockEnd.gif            }

175InBlock.gif            catch (InvalidPrinterException ex)
176ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
177InBlock.gif                MessageBox.Show(ex.Message, "Text Editor", MessageBoxButtons.OK, MessageBoxIcon.Error);
178ExpandedSubBlockEnd.gif            }

179InBlock.gif
180ExpandedSubBlockEnd.gif        }

181InBlock.gif
182InBlock.gif        private void OnBeginPrint(object sender, PrintEventArgs e)
183ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
184ExpandedSubBlockStart.gifContractedSubBlock.gif            char[] param =dot.gif'\n' };
185InBlock.gif            lines = textBox1.Text.Split(param);
186InBlock.gif            int i = 0;
187ExpandedSubBlockStart.gifContractedSubBlock.gif            char[] trimParam =dot.gif'\r' };
188InBlock.gif            foreach (string s in lines)
189ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
190InBlock.gif                lines[i++= s.TrimEnd(trimParam);
191ExpandedSubBlockEnd.gif            }

192ExpandedSubBlockEnd.gif        }

193InBlock.gif
194InBlock.gif        private void OnEndPrint(object sender, PrintEventArgs e)
195ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
196InBlock.gif            lines = null;
197ExpandedSubBlockEnd.gif        }

198InBlock.gif
199InBlock.gif        private void 页面设置UToolStripMenuItem_Click(object sender, EventArgs e)
200ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
201InBlock.gif            pageSetupDialog1.ShowDialog();
202ExpandedSubBlockEnd.gif        }

203InBlock.gif
204InBlock.gif        private void 打印预览ToolStripMenuItem_Click(object sender, EventArgs e)
205ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
206InBlock.gif            printPreviewDialog1.ShowDialog();
207InBlock.gif           
208ExpandedSubBlockEnd.gif        }

209InBlock.gif       
210ExpandedSubBlockEnd.gif    }

211ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/tiasys/archive/2006/12/27/605236.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值