c#记事本实验报告.doc

上传人:飞****9 文档编号:137141616 上传时间:2020-07-05 格式:DOC 页数:12 大小:183KB
返回 下载 相关 举报
c#记事本实验报告.doc_第1页
第1页 / 共12页
c#记事本实验报告.doc_第2页
第2页 / 共12页
c#记事本实验报告.doc_第3页
第3页 / 共12页
c#记事本实验报告.doc_第4页
第4页 / 共12页
c#记事本实验报告.doc_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《c#记事本实验报告.doc》由会员分享,可在线阅读,更多相关《c#记事本实验报告.doc(12页珍藏版)》请在金锄头文库上搜索。

1、目 录一、系统概要设计1二、系统详细设计1三、系统实现与测试(或调试)5四、分析与总结6参考文献6一、系统概要设计本实验的目的是建立一个文本编译器,同时介绍控件的用法。首先使用RichTeextBox控件可用来输入和编辑文本。其次是实现剪切板功能。通过剪切板可以完成数据的剪切,复制,粘贴等功能。然后实现保存打开功能。在文件下弹出,新建,打开,关闭,保存。修改字体使用的是字体对话框FontDialog,然后选择指定字体就行了。最后是实现打印功能,PrintDocument组件是用于完成打印的类。最后代码实现所有功能,这就是本实验的设计思路。二、系统详细设计1、新建项目。放置RichTextBox

2、控件到窗体。属性Name=richTextBox1,Dock=Fill,Text=”2、放置MenuStrip控件到窗体。为菜单增加顶级菜单:编辑,Name为mainMenuEdit,为其弹出菜单增加菜单项:剪切、复制、粘贴、撤销,属性Name分别为:menuItemEditCut、menuItemEditCopy、menuItemEditPaste、menuItemEditUndo。为各个菜单项增加单击事件处理函数如下:private void menuItemEditCut_Click(object sender, EventArgs e) richTextBox1.Cut(); priv

3、ate 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、实现存取文件功能:

4、顶级菜单的弹出菜单中一般包括如下菜单项:新建、打开、关闭、保存等。首先放置OpenFileDialog控件和SaveFileDialog控件到窗体中。为菜单增加顶级菜单:文件,为其弹出菜单增加菜单项:新建、打开、保存、退出。修改这些菜单的Name属性分别为:mainMenuFile、menuItemFileNew、menuItemFileOpen、menuItemFileSave、menuItemFileExit。在Form1类中定义变量:string s_FileName=”,记录当前编辑的文件名,如果字符串为空,表示还未记录文件名,编辑的文件没有名字,当单击“保存”保存文件时,要请用户输入

5、文件名。为文件“新建”菜单增加单击事件处理函数: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(op

6、enFileDialog1.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(saveFileDialog

7、1.FileName, RichTextBoxStreamType.PlainText); 文件“退出”菜单增加事件处理函数:private void menuItemFileExit_Click(object sender, EventArgs e) Close(); 修改字体,放置FontDialog控件到窗体,属性Name=fontDialog1.为菜单增加顶级菜单:格式,属性Name为mainMenuModel,为其弹出菜单增加子菜单:字体,属性Name为menuItemModelFont,为“字体”菜单增加单击菜单处理函数如下:private void menuItemModelFo

8、nt_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类中增加变量:String

9、Reader 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(richT

10、extBox1.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.GetHeigh

11、t(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.HasMor

12、ePages = 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 = ;

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 总结/报告

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号