当前位置:文档之家› 综合大作业(记事本)可视化程序设计

综合大作业(记事本)可视化程序设计

郑州轻工业学院实验报告实验课程:可视化程序设计基础实验名称: Windows窗体设计班级:计算机科学与技术01—1班学号: 541007010157 姓名:郑峰弓实验日期: 2011年11月24日一、实验目的1.掌握窗体设计步骤、窗体调用。

2.根据自己所做的实验内容可适当增加条目。

3.熟悉本学期学习的各种控件,上交作业。

二、实验内容2. 仿MS word (或Windows 系统记事本),实现一个文本编辑系统:读取的文件类型可以是txt、rtf等自定义格式,不一定用doc格式实现文本字体、颜色等格式的设置实现文件保存、打开、新建等功能实现有关菜单的设计、工具栏的设计和状态栏的设计(状态栏显示光标所在行、列等)三、程序开发操作步骤1. 启动Visual Studio 2008 。

2. 创建项目。

设置项目名为“记事本可视化程序设计”。

3. 添加控件。

创建过,出现设计界面,此时会出现一窗体Form1,此时窗体时空的,不包含任何控件的。

单击“工具箱”的公共控件选项卡,选择MenuStrip控件,将其放在窗口上,创建两个MenuStrip1,MenuStri2,再选择RichTextBox 控件,将其放在窗口上,调整大小及其位置。

再工具箱中的所有Window窗体中选择OpenFileDialog,SaveFileDialog,ColorDialog,FontDialog,StatusStrip控件,将其放在窗口上。

4. 设置控件属性。

点击窗口,在属性中的text设置为‘记事本’,将Startposition 设置成Centerscreen。

点击MenuStrip1,在窗体上加菜单栏,分别为文件,格式,查看,编辑,在文件,格式,查看,编辑,下又建立子菜单,如下图所示。

在子菜单下各自设置各自的热键和快捷键,在MenuStri2上添加8个button,分别将他们的text设置为空,分别找到其属性Imagel,分别添加图片,此栏为工具栏按钮,如下图所示。

5. 编写程序代码。

点窗口双击在程序框中写入相应的代码,分别点击. 窗体上的各个控件,并找到其click,在程序框中写入相应的代码,代码编辑如下。

代码:using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace记事本可视化程序设计{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){剪切ToolStripMenuItem.Enabled = false ;复制ToolStripMenuItem.Enabled = false;删除ToolStripMenuItem.Enabled = false;粘贴ToolStripMenuItem.Enabled = true;}private void新建ToolStripMenuItem_Click(object sender, EventArgs e){if (richTextBox1.Text == string.Empty)richTextBox1.Text = "";else {DialogResult result = MessageBox.Show("是否将更改保存?", "记事本提示", MessageBoxButtons.YesNoCancel, rmation);if (result == DialogResult.Yes){Savefile();Close();richTextBox1.Text = "";}else if (result == DialogResult.No){richTextBox1.Text = "";}}}private void打开ToolStripMenuItem_Click(object sender, EventArgs e){openFileDialog1.FileName = "";openFileDialog1.Filter = "RTF File(*.rtf)|*.RTF|TXT FILE(*.txt)|*.txt";openFileDialog1.ShowDialog();if (openFileDialog1.FileName != "")switch (openFileDialog1.FilterIndex){case 1: //选择的是.rtf类型richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.RichText);break;case 2: //选择的是.txt类型richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.PlainText);break;}}private void保存ToolStripMenuItem_Click(object sender, EventArgs e){Savefile();DialogResult result = MessageBox.Show("是否将更改保存?", "记事本提示", MessageBoxButtons.YesNoCancel, rmation);if (result == DialogResult.Cancel || result == DialogResult.No)return;else{MessageBox.Show("文件保存成功!", "记事本提示");}}private void Savefile(){saveFileDialog1.Filter = "RTF File(*.rtf)|*.RTF|TXT FILE(*.txt)|*.txt";if (saveFileDialog1.ShowDialog() == DialogResult.OK)switch (openFileDialog1.FilterIndex){case 1: //选择的是.rtf类型richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.RichText);break;case 2: //选择的是.txt类型richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);break;}}private void退出ToolStripMenuItem_Click(object sender, EventArgs e){if (richTextBox1.Text != string.Empty){DialogResult result = MessageBox.Show("是否将更改保存?", "记事本提示", MessageBoxButtons.YesNoCancel, rmation);if (result == DialogResult.Yes){Savefile();Application.Exit();}else if (result == DialogResult.No){Application.Exit();}}elseApplication.Exit();}private void字体ToolStripMenuItem_Click(object sender, EventArgs e){fontDialog1.AllowVectorFonts = true;//设置用户可以选择矢量字体fontDialog1.AllowVerticalFonts = true;//设置字体对话框既显示水平字体,也显示垂直字体fontDialog1.FixedPitchOnly = false;//设置用户可以选择不固定间距的字体fontDialog1.MaxSize = 100;//设置可选择的最大字fontDialog1.MinSize = 5;//设置可选择的最小字if (fontDialog1.ShowDialog() == DialogResult.OK)//判断是否选择了字体{if (richTextBox1.SelectedText == "")//判断是否选择了文本richTextBox1.SelectAll();//全选文本richTextBox1.SelectionFont = fontDialog1.Font;//设置选中的文本字体}}private void颜色ToolStripMenuItem_Click(object sender, EventArgs e){colorDialog1.AllowFullOpen = true;//设置允许用户自定义颜色colorDialog1.AnyColor = true;//设置颜色对话框中显示所有颜色colorDialog1.SolidColorOnly = false;//设置用户可以在颜色对话框中选择复杂颜色if (colorDialog1.ShowDialog() == DialogResult.OK)//判断是否选择了颜色{if (richTextBox1.SelectedText == "")//判断是否选择了文本richTextBox1.SelectAll();//全选文本richTextBox1.SelectionColor = colorDialog1.Color;//将选定的文本颜色设置为颜色对话框中选择的颜色}}private void状态栏ToolStripMenuItem_Click(object sender, EventArgs e){int index = richTextBox1.GetFirstCharIndexOfCurrentLine();int line = richTextBox1.GetLineFromCharIndex(index) + 1;int Col = richTextBox1.SelectionStart - index + 1;toolStripStatusLabel1.Text = line + "行";toolStripStatusLabel2.Text = Col + "列";}private void Form1_FormClosing(object sender, FormClosingEventArgs e){if (richTextBox1.Text != string.Empty){DialogResult result = MessageBox.Show("是否将更改保存?", "记事本提示", MessageBoxButtons.YesNoCancel, rmation);if (result == DialogResult.Yes){Savefile();Application.Exit();}else if (result == DialogResult.No){Application.Exit();}else if (result == DialogResult.Cancel){return;}}elseApplication.Exit();}private void剪切ToolStripMenuItem_Click(object sender, EventArgs e){Clipboard.SetDataObject(richTextBox1.SelectedText);richTextBox1.SelectedText = "";}private void复制ToolStripMenuItem_Click(object sender, EventArgs e){richTextBox1.Copy();}private void删除ToolStripMenuItem_Click(object sender, EventArgs e){richTextBox1.SelectedText = "";}private void粘贴ToolStripMenuItem_Click(object sender, EventArgs e){richTextBox1.Paste();}private void编辑ToolStripMenuItem_Click(object sender, EventArgs e){if ( richTextBox1.SelectedText != string.Empty){剪切ToolStripMenuItem.Enabled = true;复制ToolStripMenuItem.Enabled = true;删除ToolStripMenuItem.Enabled = true;}}private void button1_Click(object sender, EventArgs e){if (richTextBox1.Text == string.Empty)richTextBox1.Text = "";else{DialogResult result = MessageBox.Show("是否将更改保存?", "记事本提示", MessageBoxButtons.YesNoCancel, rmation);if (result == DialogResult.Yes){Savefile();Close();richTextBox1.Text = "";}else if (result == DialogResult.No){richTextBox1.Text = "";}}}//一下是工具栏代码设计private void button2_Click(object sender, EventArgs e){openFileDialog1.FileName = "";openFileDialog1.Filter = "RTF File(*.rtf)|*.RTF|TXT FILE(*.txt)|*.txt"; openFileDialog1.ShowDialog();if (openFileDialog1.FileName != "")switch (openFileDialog1.FilterIndex){case 1: //选择的是.rtf类型richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.RichText);break;case 2: //选择的是.txt类型richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.PlainText);break;}}private void button5_Click(object sender, EventArgs e){Savefile();DialogResult result = MessageBox.Show("是否将更改保存?", "记事本提示", MessageBoxButtons.YesNoCancel, rmation);if (result == DialogResult.Cancel || result == DialogResult.No)return;else{MessageBox.Show("文件保存成功!", "记事本提示");}}private void button4_Click(object sender, EventArgs e){int index = richTextBox1.GetFirstCharIndexOfCurrentLine();int line = richTextBox1.GetLineFromCharIndex(index) + 1;int Col = richTextBox1.SelectionStart - index + 1;toolStripStatusLabel1.Text = line + "行";toolStripStatusLabel2.Text = Col + "列";}private void button3_Click(object sender, EventArgs e){Clipboard.SetDataObject(richTextBox1.SelectedText);richTextBox1.SelectedText = "";}private void button6_Click(object sender, EventArgs e){richTextBox1.Copy();}private void button8_Click(object sender, EventArgs e){richTextBox1.SelectedText = "";}private void button7_Click(object sender, EventArgs e){richTextBox1.Paste();}private void hScrollBar1_Scroll(object sender, ScrollEventArgs e) {richTextBox1.Text = (hScrollBar1.Value * 0.5).ToString();}private void vScrollBar1_Scroll(object sender, ScrollEventArgs e) {richTextBox1.Text = (vScrollBar1.Value * 0.5).ToString();}}}6. 调试程序。

相关主题