C#——《C#语⾔程序设计》实验报告——Windows桌⾯编程
⽂件与流——简易记事本
⼀、实验⽬的
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需要作为⼀个事件响应⽅法来实现。
其中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="schemas.microsoft/winfx/2006/xaml/presentation"
xmlns:x="schemas.microsoft/winfx/2006/xaml"
xmlns:d="schemas.microsoft/expression/blend/2008"
xmlns:mc="/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"/>
<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>
<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.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;
textstylerange = 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|所有⽂件|*.*";

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。