文档编辑器
<Window x:Class="e3_1.Window1"
http://schemas.microsoft.com/winfx/2006/xaml/presentation"
http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="350" Width="525" KeyDown="Window_KeyDown" Loaded="Window_Loaded" Closing="Window_Closing">
<DockPanel LastChildFill="True">
<Menu Height="22" Name="menul" DockPanel.Dock="Top">
<MenuItem Header="文件(_F)">
<MenuItem Header="新建(_N)" Click="New_MenuItem_Click_1" Name="New_MenuItem">
</MenuItem>
<MenuItem Header="打开..." Name="FileOpen_MenuItem" Click="FileOpen_MenuItem_Click"/>
<MenuItem Header="保存..." Name="Filesave_MenuItem" Click="FileOpen_MenuItem_Click"/>
<MenuItem Header="另存为..." Name="FileSaveAs_MenuItem" Click="FileSaveAs_MenuItem_Click"/>
<Separator />
<MenuItem Header="退出(_X)" Name="Exit_MenuItem" Click="Exit_MenuItem_Click"/>
<Separator />
</MenuItem>
<MenuItem Header="编辑" Name="Edit">
<MenuItem Command="ApplicationCommands.Undo" />
<MenuItem Command="ApplicationCommands.Redo" />
<Separator />
<MenuItem Command="ApplicationCommands.SelectAll" />
<Separator />
<MenuItem Command="ApplicationCommands.Cut" />
<MenuItem Command="ApplicationCommands.Copy" />
<MenuItem Command="ApplicationCommands.Paste" />
</MenuItem>
</Menu>
<ToolBar Height="30" Name="toolBarl" DockPanel.Dock="Top">
<Button Width="30" Height="30" FontSize="20" Command="EditingCommands.ToggleBold" ToolTip="加粗">
<TextBlock FontWeight="Bold">B</TextBlock>
</Button>
<Button Width="30" Height="30" FontSize="20" Command="EditingCommands.ToggleItalic" ToolTip="斜倾">
<TextBlock FontStyle ="Italic" FontWeight="Bold" >I</TextBlock>
</Button>
<Button Width="30" Height="30" FontSize="20" Command="EditingCommands.ToggleUnderline" ToolTip="下划线">
<TextBlock TextDecorations="Underline " FontWeight="Bold">U</TextBlock>
</Button>
<Button Width="30" Height="30" Command="EditingCommands.IncreaseFontSize" ToolTip="字符加宽">
加宽
</Button>
<Button Width="30" Height="30" Command="EditingCommands.DecreaseFontSize" ToolTip="字符变窄" Content="变窄"></Button>
<Button Width="30" Height="30" Command="EditingCommands.ToggleBullets" ToolTip="项目符号">
项目符号
</Button>
<Button Width="30" Height="30" Command="EditingCommands.ToggleNumbering" ToolTip="编号">
编号
</Button>
<Button Width="30" Height="30" Command="EditingCommands.AlignLeft" ToolTip="左对齐">
左对齐
</Button>
<Button Width="30" Height="30" Command="EditingCommands.AlignCenter" ToolTip="中间对齐">
中间对齐
</Button>
<Button Width="30" Height="30" Command="EditingCommands.AlignRight" ToolTip="右对齐">
右对齐
</Button>
<Button Width="30" Height="30" Command="EditingCommands.AlignJustify" ToolTip="两边对齐">
两边对齐
</Button>
<Button Width="30" Height="30" Command="EditingCommands.IncreaseIndentation" ToolTip="增加缩进量">
增加缩进量
</Button>
<Button Width="30" Height="30" Command="EditingCommands.DecreaseIndentation" ToolTip="减少缩进量">
减少缩进量
</Button>
</ToolBar>
<RichTextBox Name="richTextBox1" AcceptsTab="True" TextChanged="richTextBox1_TextChanged" />
</DockPanel>
</Window>
cs.
namespace e3_1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
string s_FileName = "";
TextPointer postion = null;
bool bSave = false;
public Window1()
{
InitializeComponent();
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if ((Keyboard.GetKeyStates(Key.LeftCtrl) & KeyStates.Down) > 0)
{
if ((Keyboard.GetKeyStates(Key.N) & KeyStates.Down) > 0)
New_MenuItem_Click(sender, e);
}
}
private void New_MenuItem_Click(object sender, RoutedEventArgs e)
{
richTextBox1.Document.Blocks.Clear();
s_FileName = "";
bSave = false;
}
private void Exit_MenuItem_Click(Object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
private void FileOpen_MenuItem_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialogl = new OpenFileDialog();
openFileDialogl.Filter = "Rtf文件(*.Rtf|*.rtf|所有文件(*.*)|*.*";
if (openFileDialogl.ShowDialog().Value)
{
s_FileName=openFileDialogl.FileName;
string fileExtension=
System.IO.Path.GetExtension(s_FileName).ToUpper();
if (fileExtension==".RTF")
{using (FileStream fileStream=File.OpenRead(s_FileName))
{
TextRange textRange = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd);
if(textRange.CanLoad(DataFormats.Rtf))
textRange.Load(fileStream, DataFormats.Rtf);
}
}
}
}
private void FileSave_MenuItem_Click(object sender, RoutedEventArgs e)
{
if (s_FileName.Length != 0)
{
using (FileStream fileStream = File.Create(s_FileName))
{
TextRange textRange = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd);
textRange.Save(fileStream, DataFormats.Rtf);
bSave = false;
}
}
else
FileSave_MenuItem_Click(sender, e);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
public bool IfSaveOldFile()
{
bool ReturnValue=true;
if(bSave)
{
MessageBoxResult dr;
dr=MessageBox.Show("要保存好当前更改吗?","保存更改吗",MessageBoxButton.YesNoCancel,MessageBoxImage.Question);
switch(dr)
{
case MessageBoxResult.Yes:
FileSave_MenuItem_Click(null,null);
ReturnValue=true;
bSave=false;
break;
case
MessageBoxResult.No:
bSave=false;
ReturnValue=true;
break;case MessageBoxResult.Cancel:
ReturnValue=false;
break;
}
}
return ReturnValue;
}
public void richTextBox1_TextChanged(object sender,TextChangedEventArgs e)
{
bSave=true;
}
public void Window_Closing(object sender,System.ComponentModel.CancelEventArgs e)
{
if(!IfSaveOldFile())
e.Cancel=true;
}
private void FileSaveAs_MenuItem_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog saveFileDialog1=new SaveFileDialog();
saveFileDialog1.Filter="Rtf文件(*.Rtf|*.rtf|所有文件(*.*)|*.*";
if(saveFileDialog1.ShowDialog().Value)
{
s_FileName=saveFileDialog1.FileName;
using(FileStream fileStream=File.Create(s_FileName))
{
TextRange textRange=new TextRange(richTextBox1.Document.ContentStart,richTextBox1.Document.ContentEnd);
textRange.Save(fileStream,DataFormats.Rtf);
bSave=false;
}
}
}
private void New_MenuItem_Click_1(object sender, RoutedEventArgs e)
{
}
}
}