一、实验目的

  1. 掌握文件类的使用;
  2. 掌握文件流的操作;
  3. 掌握二进制数据、文本数据的读写;
  4. 继续应用WPF技术进行界面编程。

二、实验内容

  1. 写一个记事本程序:

(1)设计界面,向窗体添加下拉式菜单、多格式文本框(RichTextBox)。

(2)依次为“文件”下的“新建”、“打开”、“保存”菜单项的Click事件添加事件处理函数。可以使用路由命令。

(3)添加“格式”工具栏,可以设置所有文字的粗体、斜体、大小、颜色等样式。

(4)实现文本文件的打开、编辑和保存功能;

提示

1、窗口可用DockPanel进行布局,让菜单和工具栏都位于顶部,即:

DockPanel.Dock="Top"

2、文本文件的编辑可以使用TextBox控件。

3、使用命令绑定,让菜单项和工具栏同时与一个操作相关联。

在MainWindow.xaml的Window标签下加:

    <Window.CommandBindings><CommandBinding Command="ApplicationCommands.New" Executed="NewCommand_Executed"/><CommandBinding Command="ApplicationCommands.Open" Executed="OpenCommand_Executed"/><CommandBinding Command="ApplicationCommands.Save" Executed="SaveCommand_Executed"/></Window.CommandBindings>

在菜单项添加:

        <MenuItem Header="新建(_N)" Command="New"/>

在工具栏添加:

            <Button Content="新建" Command="New"/>

就可绑定命令。同时Ctrl+O等键盘组合也默认与Open命令相绑定。

其中NewCommand_Executed需要作为一个事件响应方法来实现。

4、添加bool类型_saved字段,标记当前内容是否已保存。(文本格式不属于文件内容,修改格式不会导致_saved字段的改变)

5、打开文件时,弹出打开文件对话框,操作代码如下:

           OpenFileDialog dlg = new OpenFileDialog();dlg.DefaultExt = "*.txt";dlg.Filter = "Text Files (*.txt)|*.txt";bool? result = dlg.ShowDialog();if (result == true){string fileName = dlg.FileName;}

自此可对该文件名进行操作。

6、保存文件时,实际可实现“另存为”功能。弹出保存文件对话框,操作代码如下:

            SaveFileDialog saveFileDialog = new SaveFileDialog();saveFileDialog.Filter = "文本文件|*.txt|所有文件|*.*";saveFileDialog.FilterIndex = 0;bool? result = saveFileDialog.ShowDialog();if (result == true){string strFile = saveFileDialog.FileName;}

自此可对该文件名进行操作。

7、可以根据自己的想法,添加更加丰富的功能。

源代码

XAML

<Window x:Class="Homework11.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:Homework11"mc:Ignorable="d"Title="记事本" Height="450" Width="800"><Window.CommandBindings><CommandBinding Command="ApplicationCommands.New" Executed="NewCommand_Executed"/><CommandBinding Command="ApplicationCommands.Open" Executed="OpenCommand_Executed"/><CommandBinding Command="ApplicationCommands.Save" Executed="SaveCommand_Executed"/><CommandBinding Command="ApplicationCommands.Print" Executed="PrintCommand_Executed"/></Window.CommandBindings><DockPanel><DockPanel.Resources><Style TargetType="{x:Type Button}" x:Key="formatTextStyle"><Setter Property="FontFamily" Value="Palatino Linotype"></Setter><Setter Property="Width" Value="30"></Setter><Setter Property="FontSize" Value ="14"></Setter><Setter Property="CommandTarget" Value="{Binding ElementName=MainTextBox}"></Setter></Style><Style TargetType="{x:Type Button}" x:Key="formatImageStyle"><Setter Property="Width" Value="30"></Setter><Setter Property="CommandTarget" Value="{Binding ElementName=MainTextBox}"></Setter></Style></DockPanel.Resources><Menu x:Name="MainMenu" DockPanel.Dock="Top"><MenuItem Header="文件"><MenuItem Header="新建(_N)" Command="New"/><MenuItem Header="打开(_O)" Command="Open"/><MenuItem Header="保存(_S)" Command="Save"/><MenuItem Header="打印(_P)" Command="Print"/></MenuItem><MenuItem Header="格式"><MenuItem Header="自动换行(_W)"/><MenuItem Header="字体(_F)"/></MenuItem></Menu><ToolBar x:Name="MainToolBar" DockPanel.Dock="Top"><Button Style="{StaticResource formatImageStyle}" Command="New" ><Image Source="Images/New.png"></Image></Button><Button Style="{StaticResource formatImageStyle}"  Command="Open"><Image Source="Images/Open.png"></Image></Button><Button Style="{StaticResource formatImageStyle}" Command="Save"><Image Source="Images/Save.png"></Image></Button><Button Style="{StaticResource formatImageStyle}" Command="Print"><Image Source="Images/Print.png"></Image></Button></ToolBar><!-- This tool bar contains all the editing buttons. --><ToolBar Name="mainToolBar" Height="30" DockPanel.Dock="Top"><Button Style="{StaticResource formatImageStyle}" Command="ApplicationCommands.Cut" ToolTip="Cut"><Image Source="Images/EditCut.png"></Image></Button><Button Style="{StaticResource formatImageStyle}" Command="ApplicationCommands.Copy" ToolTip="Copy"><Image Source="Images/EditCopy.png"></Image></Button><Button Style="{StaticResource formatImageStyle}" Command="ApplicationCommands.Paste" ToolTip="Paste"><Image Source="Images/EditPaste.png"></Image></Button><Button Style="{StaticResource formatImageStyle}" Command="ApplicationCommands.Undo" ToolTip="Undo"><Image Source="Images/EditUndo.png"></Image></Button><Button Style="{StaticResource formatImageStyle}" Command="ApplicationCommands.Redo" ToolTip="Redo"><Image Source="Images/EditRedo.png"></Image></Button><Button Style="{StaticResource formatTextStyle}" Command="EditingCommands.ToggleBold" ToolTip="Bold"><Image Source="Images/EditBold.png"></Image></Button><Button Style="{StaticResource formatTextStyle}" Command="EditingCommands.ToggleItalic" ToolTip="Italic"><Image Source="Images/EditItalic.png"></Image></Button><Button Style="{StaticResource formatTextStyle}" Command="EditingCommands.ToggleUnderline" ToolTip="Underline"><Image Source="Images/EditUnderline.png"></Image></Button><Button Style="{StaticResource formatImageStyle}" Command="EditingCommands.IncreaseFontSize" ToolTip="Grow Font"><Image Source="Images\CharacterGrowFont.png"></Image></Button><Button Style="{StaticResource formatImageStyle}" Command="EditingCommands.DecreaseFontSize" ToolTip="Shrink Font"><Image Source="Images\CharacterShrinkFont.png"></Image></Button><Button Style="{StaticResource formatImageStyle}" Command="EditingCommands.ToggleBullets" ToolTip="Bullets"><Image Source="Images\ListBullets.png"></Image></Button><Button Style="{StaticResource formatImageStyle}" Command="EditingCommands.ToggleNumbering" ToolTip="Numbering"><Image Source="Images/ListNumbering.png"></Image></Button><Button Style="{StaticResource formatImageStyle}" Command="EditingCommands.AlignLeft" ToolTip="Align Left"><Image Source="Images\ParagraphLeftJustify.png"></Image></Button><Button Style="{StaticResource formatImageStyle}" Command="EditingCommands.AlignCenter" ToolTip="Align Center"><Image Source="Images\ParagraphCenterJustify.png"></Image></Button><Button Style="{StaticResource formatImageStyle}" Command="EditingCommands.AlignRight" ToolTip="Align Right"><Image Source="Images\ParagraphRightJustify.png"></Image></Button><Button Style="{StaticResource formatImageStyle}" Command="EditingCommands.AlignJustify" ToolTip="Align Justify"><Image Source="Images\ParagraphFullJustify.png"></Image></Button><Button Style="{StaticResource formatImageStyle}" Command="EditingCommands.IncreaseIndentation" ToolTip="Increase Indent"><Image Source="Images\ParagraphIncreaseIndentation.png"></Image></Button><Button Style="{StaticResource formatImageStyle}" Command="EditingCommands.DecreaseIndentation" ToolTip="Decrease Indent"><Image Source="Images\ParagraphDecreaseIndentation.png"></Image></Button></ToolBar><RichTextBox x:Name="MainTextBox" DockPanel.Dock="Top" Height="300" TextChanged="MainTextBox_TextChanged"  AcceptsTab="True"><FlowDocument><Paragraph><Run></Run></Paragraph></FlowDocument></RichTextBox><StatusBar DockPanel.Dock="Bottom"><TextBlock x:Name="MainStatusBar"></TextBlock></StatusBar></DockPanel></Window>

CS

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;namespace Homework11
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{private bool _saved;private string title {set { Title = value; }get { return Title; }}public MainWindow(){InitializeComponent();}private void NewCommand_Executed(object sender, ExecutedRoutedEventArgs e){_saved = false;TextRange range;range = new TextRange(MainTextBox.Document.ContentStart, MainTextBox.Document.ContentEnd);range.Text = "";}private void OpenCommand_Executed(object sender, ExecutedRoutedEventArgs e){OpenFileDialog dlg = new OpenFileDialog();dlg.DefaultExt = "*.txt";dlg.Filter = "Text Files (*.txt)|*.txt";bool? result = dlg.ShowDialog();string str=null;if (result == true){string _fileName = dlg.FileName;TextRange range;FileStream fStream;if (File.Exists(_fileName)){title = _fileName;range = new TextRange(MainTextBox.Document.ContentStart, MainTextBox.Document.ContentEnd);fStream = new FileStream(_fileName, FileMode.OpenOrCreate);range.Load(fStream, DataFormats.XamlPackage);fStream.Close();}}}private void SaveCommand_Executed(object sender, ExecutedRoutedEventArgs e){if (_saved) {return;}SaveFileDialog saveFileDialog = new SaveFileDialog();saveFileDialog.Filter = "文本文件|*.txt|所有文件|*.*";saveFileDialog.FilterIndex = 0;bool? result = saveFileDialog.ShowDialog();if (result == true){string strFile = saveFileDialog.FileName;TextRange range;FileStream fStream;range = new TextRange(MainTextBox.Document.ContentStart, MainTextBox.Document.ContentEnd);fStream = new FileStream(strFile, FileMode.Create);range.Save(fStream, DataFormats.XamlPackage);fStream.Close();_saved = true;MainStatusBar.Text = "保存到" + strFile;}}private void PrintCommand_Executed(object sender, ExecutedRoutedEventArgs e){PrintDialog pd = new PrintDialog();if ((pd.ShowDialog() == true)){//use either one of the belowpd.PrintVisual(MainTextBox as Visual, "printing as visual");pd.PrintDocument((((IDocumentPaginatorSource)MainTextBox.Document).DocumentPaginator), "printing as paginator");}}private void MainTextBox_TextChanged(object sender, TextChangedEventArgs e){_saved = false;}private void LineCommand_Executed(object sender, ExecutedRoutedEventArgs e){}private void FontSelectCommand_Executed(object sender, ExecutedRoutedEventArgs e){}}
}

运行结果

三、实验心得与体会

  1. 掌握文件类的使用;
  2. 掌握文件流的操作;
  3. 掌握二进制数据、文本数据的读写;
  4. 继续应用WPF技术进行界面编程;
  5. 掌握RichTextBox控件的使用;
  6. 掌握对话框的使用。

参考文章

https://docs.microsoft.com/zh-cn/dotnet/framework/wpf/controls/richtextbox-overview#save-load-and-print-richtextbox-content

https://blog.csdn.net/u011389706/article/details/55805476

https://blog.csdn.net/weixin_43272781/article/details/106284772

https://www.jianshu.com/p/9c30b5097a3f

https://www.cnblogs.com/arxive/p/5725570.html

C#——《C#语言程序设计》实验报告——Windows桌面编程文件与流——简易记事本相关推荐

  1. C#——《C#语言程序设计》实验报告——Windows桌面编程——简单的计算器(仿Windows 10计算器)

    一.实验目的 熟悉使用WPF进行界面编程的基本过程: 掌握WPF布局.控件.事件的使用. 二.实验内容 运用WPF技术,模仿Windows 10系统中计算机器(Calculator)程序,开发一个类似 ...

  2. C#——《C#语言程序设计》实验报告——Windows桌面编程

    一.实验目的 熟悉使用WPF进行界面编程的基本过程: 掌握WPF布局.控件.事件的使用. 二.实验内容 使用WPF技术,进行合理布局,设计一个窗体应用程序,完成一些常用度量单位的转换,基本模板如下图: ...

  3. c语言程序设计数组实验报告,(C语言程序设计实验报告数组.doc

    (C语言程序设计实验报告数组 <C语言程序设计 >课程实验报告 实验名称 学 号_ 姓 名 ___ 班 别 实验日期: 年月日 实验报告日期: 年月日 指导老师: 实验地点: 成 绩: 评 ...

  4. c语言程序设计实验报告2,C语言程序设计实验报告2.docx

    C语言程序设计实验报告2.docx 下载提示(请认真阅读)1.请仔细阅读文档,确保文档完整性,对于不预览.不比对内容而直接下载带来的问题本站不予受理. 2.下载的文档,不会出现我们的网址水印. 3.该 ...

  5. c 语言差错编码实验结果,C语言程序设计实验报告(四).doc11111111111111111.doc

    C语言程序设计实验报告(四).doc11111111111111111 C语言程序设计实验报告 姓 名吴文重学 号52系 别数学系班级2班主讲教师徐时芳指导教师徐时芳实验日期2011-11-8专业10 ...

  6. c语言选择循环实验报告,C语言程序设计实验报告选择与循环结构程序设计.doc

    C语言程序设计实验报告选择与循环结构程序设计.doc 下载提示(请认真阅读)1.请仔细阅读文档,确保文档完整性,对于不预览.不比对内容而直接下载带来的问题本站不予受理. 2.下载的文档,不会出现我们的 ...

  7. c语言的简单程序设计实验报告,工程学院C语言程序设计实验报告

    <工程学院C语言程序设计实验报告>由会员分享,可在线阅读,更多相关<工程学院C语言程序设计实验报告(5页珍藏版)>请在人人文库网上搜索. 1.南京工程学院实验报告课程名称C 语 ...

  8. c语言程序设计植树,C语言程序设计实验报告——实验

    C语言程序设计实验报告--实验 下载提示(请认真阅读)1.请仔细阅读文档,确保文档完整性,对于不预览.不比对内容而直接下载带来的问题本站不予受理. 2.下载的文档,不会出现我们的网址水印. 3.该文档 ...

  9. c语言程序设计数组实验报告,c语言程序设计实验报告(数组).doc

    c语言程序设计实验报告(数组).doc 下载提示(请认真阅读)1.请仔细阅读文档,确保文档完整性,对于不预览.不比对内容而直接下载带来的问题本站不予受理. 2.下载的文档,不会出现我们的网址水印. 3 ...

最新文章

  1. layer.open嵌入地址链接
  2. Linux命令整理 - 文件搜索【4】
  3. 从硅谷走出来,它们的成功有迹可循
  4. python queue 调试_python:如何创建用于调试的持久内存结构
  5. 装饰者模式如何拯救了我的一天
  6. Hive的使用之脚本文件
  7. Dubbo常见面试题与答案
  8. 毕业一年的员工跳槽需要理由吗?
  9. MATLAB2016下载地址,包含安装教程
  10. python读取数据库数据,读取出的中文乱码问题
  11. 车辆等红灯时是用N档还是D档呢?
  12. 为什么大多数私人企业无法做大做强?
  13. C 语言指针与汇编地址(一)
  14. 看了一个大牛的博客,发现了一个很好的文章-初学PHP进
  15. 计算机大学生职业规划书word模板,大学生职业生涯规划书模板(附word)
  16. 求车牌号问题(C语言程序设计)
  17. mysql 口令_怎么样为用户设定口令(MYSQL)_MySQL
  18. 一个电商网站设计方案
  19. linux 下的无线网络配置,详解在LINUX环境下怎样设置无线网络配置
  20. 数据结构与算法学习(第一天)

热门文章

  1. cookie版购物车
  2. mysql 字符串解析_MySQL 动态字符串处理详解
  3. redis 哨兵_Redis哨兵的配置和原理
  4. python 对excel文件进行分词并进行词频统计_python 词频分析
  5. 编程判断某个数为素数_【每日编程233期】素数对猜想
  6. mac怎么实现文件读写c语言,使用Sublime Text和Xcode在Mac上进行文件输入/输出。 C语言...
  7. mysql map 键值对获取_mysql map_get function,用于解析map结构数据,根据key返回相对应value...
  8. buffer转int python_C/C++实战014:字符串转换及Python传参数组
  9. 二、Git多人开发:不同人修改了不同文件如何处理
  10. 期末考试前的预习,科目:化工设备与反应器(5)