如何为富文本编辑器提供SAVE(ctrl + S)功能 [英] how to provide SAVE(ctrl +S) fuction to rich text editor

查看:74
本文介绍了如何为富文本编辑器提供SAVE(ctrl + S)功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在wpf中开发一个富文本编辑器.它的另存为功能可以完美地工作,但是我还想提供保存选项,该选项可以将已经打开的文档以相同的名称保存在相同的位置,例如记事本或单词保存选项...
我在这里发布我的源代码,请帮助我.

.Xaml文件

I am developing a rich text editor in wpf.its save as function work perfectly but i also want to provide save option which save the already open document with same name at same location like a notepad or word save option...
i am posting my source code here plz help me..

.Xaml file

<RichTextBox Height="420.724" Margin="6,66,6,5.999" Name="txtcreatelssn" Width="642" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Auto" SpellCheck.IsEnabled="True"/>
                            <ToolBar Height="34" Margin="6,34,6,0" Name="toolBarMenu" VerticalAlignment="Top" >
                                <Button Click="CmdNew" >
                                    <Image Source="/MTDLL_TEACHER;component/Images/filenew.png" >
                                    </Image>
                                </Button>
                                <Button x:Name="Open" Click="Open1"  >
                                    <Image Source="/MTDLL_TEACHER;component/Images/fileopen.png" ></Image>
                                </Button>
                                <Button  x:Name="Save_As" Click="save_as">
                                    <Image Source="/MTDLL_TEACHER;component/Images/filesave.png"></Image>
                                </Button>
                                <Button  x:Name="Save" Click="save">
                                    <Image Source="/MTDLL_TEACHER;component/Images/filesaveas.png"></Image>
                                </Button>
                                <Button  Command="Cut" ToolTip="Cut">
                                    <Image Source="/MTDLL_TEACHER;component/Images/editcut.png"></Image>
                                </Button>
                                <Button  Command="Copy" ToolTip="Copy">
                                    <Image Source="/MTDLL_TEACHER;component/Images/editcopy.png"></Image>
                                </Button>
                                <Button  Command="Paste" ToolTip="Paste">
                                    <Image Source="/MTDLL_TEACHER;component/Images/editpaste.png"></Image>
                                </Button>
                                <Button  Command="Undo" ToolTip="Undo">
                                    <Image Source="/MTDLL_TEACHER;component/Images/editundo.png"></Image>
                                </Button>
                                <Button  Command="Redo" ToolTip="Redo">
                                    <Image Source="/MTDLL_TEACHER;component/Images/editredo.png"></Image>
                                </Button>
                                <Button  Command="EditingCommands.ToggleBold" ToolTip="Bold">
                                    <TextBlock FontWeight="Bold" FontSize="15" Foreground="Black">B</TextBlock>
                                </Button>
                                <Button  Command="EditingCommands.ToggleItalic" ToolTip="Italic">
                                    <TextBlock FontStyle="Italic" FontWeight="Bold" FontSize="15" Foreground="Black">I</TextBlock>
                                </Button>
                                <Button  Command="EditingCommands.ToggleUnderline" ToolTip="Underline">
                                    <TextBlock TextDecorations="Underline" FontWeight="Bold" FontSize="15" Foreground="Black">U</TextBlock>
                                </Button>
                                <Button  Command="EditingCommands.AlignLeft" ToolTip="Align Left">
                                    <Image Source="/MTDLL_TEACHER;component/Images/paragraphleftjustify.png"/>
                                </Button>
                                <Button  Command="EditingCommands.AlignCenter" ToolTip="Align Center">
                                    <Image Source="/MTDLL_TEACHER;component/Images/paragraphcenterjustify.png"/>
                                </Button>
                                <Button  Command="EditingCommands.AlignRight" ToolTip="Align Right">
                                    <Image Source="/MTDLL_TEACHER;component/Images/paragraphrightjustify.png"/>
                                </Button>
                            </ToolBar>



.Xaml.cs文件



.Xaml.cs file

#region wordfunctionality

       void CmdNew(object sender, RoutedEventArgs e)
       {
           txtcreatelssn.Document = new FlowDocument();
       }
       void Open1(object sender, RoutedEventArgs e)
       {
           System.Windows.Forms.OpenFileDialog openFile = new System.Windows.Forms.OpenFileDialog();
           openFile.Filter = "RichText Files (*.rtf)|*.rtf|All Files (*.*)|*.*";

           if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
           {
               // Create a TextRange around the entire document.
               TextRange documentTextRange = new TextRange(txtcreatelssn.Document.ContentStart, txtcreatelssn.Document.ContentEnd);

               using (FileStream fs = File.Open(openFile.FileName, FileMode.Open))
               {
                   if (System.IO.Path.GetExtension(openFile.FileName).ToLower() == ".rtf")
                   {
                       documentTextRange.Load(fs, System.Windows.DataFormats.Rtf);
                   }
                   else
                   {
                       documentTextRange.Load(fs, System.Windows.DataFormats.Xaml);
                   }
               }

           }
       }

       void save_as(object sender, RoutedEventArgs e)
       {
           System.Windows.Forms.SaveFileDialog savefile = new System.Windows.Forms.SaveFileDialog();
           //System.Windows.Forms.OpenFileDialog openFile = new System.Windows.Forms.OpenFileDialog();
           savefile.Filter = "RichText Files (*.rtf)|*.rtf|All Files (*.*)|*.*";

           if (savefile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
           {
               // Create a TextRange around the entire document.
               TextRange documentTextRange = new TextRange(
                   txtcreatelssn.Document.ContentStart, txtcreatelssn.Document.ContentEnd);
               MemoryStream ms = new MemoryStream();
               // If this file exists, it''s overwritten.
               using (FileStream fs = File.Create(savefile.FileName))
               {
                   if (System.IO.Path.GetExtension(savefile.FileName).ToLower() == ".rtf")
                   {
                       documentTextRange.Save(fs,System.Windows.DataFormats.Rtf);
                   }
                   else
                   {
                       documentTextRange.Save(fs, System.Windows.DataFormats.Xaml);


                   }
               }

           }

       }//save_as

       #endregion wordfunctionality

推荐答案

使用命令,因为这些命令可以连接到快捷键.然后可以再次将命令绑定到按钮.在此处阅读有关此内容的信息: http://www.switchonthecode.com/tutorials/wpf-tutorial-command-bindings-and-custom-commands [ ^ ]或此处: http://msdn.microsoft.com/en-us/library/ms752308.aspx [ ^ ]

最好的问候,

-MRB
Use commands as these can be connected to shortcut keys. The commands then again can be bound to the buttons. Read some about it here: http://www.switchonthecode.com/tutorials/wpf-tutorial-command-bindings-and-custom-commands[^] or here: http://msdn.microsoft.com/en-us/library/ms752308.aspx[^]

Best Regards,

-MRB


您是否尝试过将CTRL + S与菜单命令相关联,以使框架为您处理乏味.
Have you tried associating CTRL+S with the menu command, that way the framework deals with the tedium for you.


这篇关于如何为富文本编辑器提供SAVE(ctrl + S)功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆