c#记事本实验报告.doc_第1页
c#记事本实验报告.doc_第2页
c#记事本实验报告.doc_第3页
c#记事本实验报告.doc_第4页
c#记事本实验报告.doc_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

题目:文本编译器课程设计报告姓 名: 学 号: 专 业: 实 验 室: 设计时间:2012年12月31日 2013年01月11日目 录一、系统概要设计1二、系统详细设计1三、系统实现与测试(或调试)5四、分析与总结6参考文献6天津科技大学计算机学院面向对象程序设计课程设计报告一、系统概要设计本实验的目的是建立一个文本编译器,同时介绍控件的用法。首先使用RichTeextBox控件可用来输入和编辑文本。其次是实现剪切板功能。通过剪切板可以完成数据的剪切,复制,粘贴等功能。然后实现保存打开功能。在文件下弹出,新建,打开,关闭,保存。修改字体使用的是字体对话框FontDialog,然后选择指定字体就行了。最后是实现打印功能,PrintDocument组件是用于完成打印的类。最后代码实现所有功能,这就是本实验的设计思路。二、系统详细设计1、新建项目。放置RichTextBox控件到窗体。属性Name=richTextBox1,Dock=Fill,Text=”2、放置MenuStrip控件到窗体。为菜单增加顶级菜单:编辑,Name为mainMenuEdit,为其弹出菜单增加菜单项:剪切、复制、粘贴、撤销,属性Name分别为:menuItemEditCut、menuItemEditCopy、menuItemEditPaste、menuItemEditUndo。为各个菜单项增加单击事件处理函数如下:private void menuItemEditCut_Click(object sender, EventArgs e) richTextBox1.Cut(); private void menuItemEditCopy_Click(object sender, EventArgs e) richTextBox1.Copy(); private void menuItemEditPaste_Click(object sender, EventArgs e) richTextBox1.Paste(); private void menuItemUndo_Click(object sender, EventArgs e) richTextBox1.Undo(); 3、运行,输入一些字符后,选中一段实验复制,剪切,粘贴,撤销等功能。首先是复制:4、实现存取文件功能:顶级菜单的弹出菜单中一般包括如下菜单项:新建、打开、关闭、保存等。首先放置OpenFileDialog控件和SaveFileDialog控件到窗体中。为菜单增加顶级菜单:文件,为其弹出菜单增加菜单项:新建、打开、保存、退出。修改这些菜单的Name属性分别为:mainMenuFile、menuItemFileNew、menuItemFileOpen、menuItemFileSave、menuItemFileExit。在Form1类中定义变量:string s_FileName=”,记录当前编辑的文件名,如果字符串为空,表示还未记录文件名,编辑的文件没有名字,当单击“保存”保存文件时,要请用户输入文件名。为文件“新建”菜单增加单击事件处理函数:private void menuItemFileNew_Click(object sender, EventArgs e) richTextBox1.Text = ; s_FileName = ;/新?建文?件t没?有D文?件t名? 为“打开”菜单增加单击事件处理函数:private void menuItemFileOpen_Click(object sender, EventArgs e) if (openFileDialog1.ShowDialog() = DialogResult.OK) richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.PlainText); 为“保存”菜单增加单击事件处理函数:private void menuItemFileSave_Click(object sender, EventArgs e) if (saveFileDialog1.ShowDialog() = DialogResult.OK) saveFileDialog1.FilterIndex = 1; s_FileName = saveFileDialog1.FileName; richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText); 文件“退出”菜单增加事件处理函数:private void menuItemFileExit_Click(object sender, EventArgs e) Close(); 修改字体,放置FontDialog控件到窗体,属性Name=fontDialog1.为菜单增加顶级菜单:格式,属性Name为mainMenuModel,为其弹出菜单增加子菜单:字体,属性Name为menuItemModelFont,为“字体”菜单增加单击菜单处理函数如下:private void menuItemModelFont_Click(object sender, EventArgs e) if (fontDialog1.ShowDialog() = DialogResult.OK) richTextBox1.SelectionFont = fontDialog1.Font; 效果如下:实现打印功能:PrintDocument组件是用于完成打印的类。在主窗体文件Form1.cs中的最后一个using语句之后增加语句:using System.IO; /处理文件必须引入的命名空间using System.Drawing.Printing; /打印必须引用的命名空间 在主窗体Form1类中增加变量:StringReader streamToPrint=null。打印的文件为StringReader streamToPrint=null。在主窗体Form1类中增加打印使用的字体的变量:Font printFont。放置PrintDocument控件到窗体,属性name为printDocument1.打印所使用的代码:private void printDocument1_BeginPrint(object sender, PrintPageEventArgs e) printFont = richTextBox1.Font; streamToPrint = new StringReader(richTextBox1.Text); private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) float linesPerPage = 0; float yPos = 0; int count = 0; float leftMargin = e.MarginBounds.Left; float topMargin = e.MarginBounds.Top; string line = null; linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics); while (count linesPerPage & (line = streamToPrint.ReadLine() != null) yPos = topMargin + (count * printFont.GetHeight(e.Graphics); e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat(); count+; if (line != null) e.HasMorePages = true; else e.HasMorePages = false; private void 打印?ToolStripMenuItem_Click(object sender, EventArgs e) printDialog1.Document = printDocument1; if (printDialog1.ShowDialog(this) = DialogResult.OK) printDocument1.Print(); private void printDocument1_EndPrint(object sender, PrintPageEventArgs e) if (streamToPrint != null) streamToPrint.Close(); 三、系统实现与测试(或调试)实现代码:namespace 记?事?本? public partial class Form1 : Form string s_FileName = ; StringReader streamToPrint = null; System.Drawing.Font printFont; public Form1() InitializeComponent(); private void richTextBox1_TextChanged(object sender, EventArgs e) private void 剪?切DToolStripMenuItem_Click(object sender, EventArgs e) private void menuItemEditCut_Click(object sender, EventArgs e) richTextBox1.Cut(); private void menuItemEditCopy_Click(object sender, EventArgs e) richTextBox1.Copy(); private void menuItemEditPaste_Click(object sender, EventArgs e) richTextBox1.Paste(); private void menuItemUndo_Click(object sender, EventArgs e) richTextBox1.Undo(); private void openFileDialog1_FileOk(object sender, CancelEventArgs e) private void mainMenuFile_Click(object sender, EventArgs e) private void menuItemFileNew_Click(object sender, EventArgs e) richTextBox1.Text = ; s_FileName = ; private void menuItemFileOpen_Click(object sender, EventArgs e) if (openFileDialog1.ShowDialog() = DialogResult.OK) richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.PlainText); private void menuItemFileSave_Click(object sender, EventArgs e) if (saveFileDialog1.ShowDialog() = DialogResult.OK) saveFileDialog1.Filter = 纯?文?本?文?件t(*.txt)|*.txt|所有D文?件t(*.*)?|*.*; saveFileDialog1.FilterIndex = 1; s_FileName = saveFileDialog1.FileName; richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText); private void menuItemFileExit_Click(object sender, EventArgs e) Close(); private void fontDialog1_Apply(object sender, EventArgs e) private void menuItemModelFont_Click(object sender, EventArgs e) if (fontDialog1.ShowDialog() = DialogResult.OK) richTextBox1.SelectionFont = fontDialog1.Font; private void printDocument1_BeginPrint(object sender, PrintPageEventArgs e) printFont = richTextBox1.Font;? streamToPrint = new StringReader(richTextBox1.Text); private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) float linesPerPage = 0; float yPos = 0; int count = 0; float leftMargin = e.MarginBounds.Left; float topMargin = e.MarginBounds.Top; string line = null; linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics); while (count linesPerPage & (line = streamToPrint.ReadLine() != null) yPos = topMargin + (count * printFont.Get

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论