c#第二题

文档编辑器
<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)
    {

    }
    }
     
    
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 作业要求 乘法计算器 程序 namespace Expl_02{public partial class Form...
    鲸落_79f1阅读 1,135评论 0 0
  • 程序不可能只有一个界面,再建一个界面。在项目上右键,选择添加一个窗体。
    1990黑四哥阅读 4,093评论 0 0
  • 1.C#中的“Main”函数:(1)一个小游戏说明程序是怎么运行的 无论游戏怎么玩,都会通过第一个按钮“开始”,这...
    技术美术阅读 3,157评论 0 0
  • 实现以下要求: static void Main(string[] args){string num1;strin...
    亻尔亻也阅读 1,040评论 0 0
  • 1.内存,栈,堆 (1)windows使用一个虚拟的寻址系统,该系统把程序可用的内存地址映射到硬件内存中的实际地址...
    Louis_yi阅读 1,382评论 0 0

友情链接更多精彩内容