zoukankan      html  css  js  c++  java
  • c#-RTF文本编辑器

    1“.RTF”什么?

    多信息文本格式 (RTF) 是一种方便于不同的设备、系统查看的文本和图形文档格式。


    RTF 使用美国国内标准协会 (ANSI)、 PC-8、 Macintosh(mac苹果),或 IBM 的 PC 字符设置控制显示形式和打印形式。
    在不同的操作系统下创建的RTF文档能够在多种操作系统和应用程序之间互相传输、查看。
    当前,作为 MS-DOS、 Microsoft Windows、 OS/2、 Macintosh苹果系统,应用程序之间处理文档的特殊翻译软件。


    RTF是Rich Text Format的缩写,意即多文本格式。

    这是一种类似DOC格式(Word文档)的文件,有非常好的兼容性,使用Windows“附件”中的“写字板”就能打开并进行编辑。

    使用“写字板”打开一个RTF格式文件时。将看到文件的内容;假设要查看RTF格式文件的源码,仅仅要使用“记事本”将它打开即可了。这就是说,你全然能够像编辑HTML文件一样,使用“记事本”来编辑RTF格式文件。


    作为微软公司的标准文件,早期外界须要数十美元向微软付款,才干购买一本薄薄的RTF标准文件。只是随着採用RTF格式标准的软件愈来愈多。RTF格式也愈来愈普遍。微软公司就把标准文件公开。放在网上供开发人员下载。
    RTF格式是很多软件都可以识别的文件格式。

    比方Word、WPS Office、Excel等都可以打开RTF格式的文件。

    对普通用户而言,RTF格式是一个非常好的文件格式转换工具,用于在不同应用程序之间进行格式化文本文档的传送。
    通用兼容性应该是RTF的最大长处,但同一时候也就具有它的缺点。比方文件一般相对较大(可能由于嵌入了兼容各种应用程序的控制符号吧)、WORD等应用软件特有的格式可能无法正常保存等。

    2.代码例如以下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using FCG.Windows.Forms;
    
    namespace RTF
    {
        public partial class RTF : Form
        {
            public RTF()
            {
                InitializeComponent();
            }
    
            /// <summary>
            /// 获取文档编辑区域使用的 RtfEditor 实例。
            /// </summary>
            internal RtfEditor RtfEditor
            {
                get
                {
                    return rtfEditor;
                }
            }
    
            void rtfEditor_FileNameChanged(object sender, EventArgs e)
            {
                string FileName = Path.GetFileName(rtfEditor.FileFullName);
                if (FileName == "")
                    FileName = "YYS-RTF编辑器";
                this.Text = FileName;
            }
    
            protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
                
                string[] args =Environment.GetCommandLineArgs();
                if (args.Length < 2)//arg[0]=exepath , arg[1] = filename
                {
                    //File_Func_NewFile();
                }
                else
                {
                    string filename =args[1];
                    if(filename.Trim().ToLower()!="-test")
                        rtfEditor.LoadFile(filename);
                }
    
                rtfEditor.FileNameChanged += new EventHandler(rtfEditor_FileNameChanged);
                rtfEditor_FileNameChanged(this, null);
            }
    
    
            /// <summary>
            /// 在关闭程序之前,推断文本是否须要保存
            /// </summary>
            private void App_Closing(FormClosingEventArgs e)
            {
                if (rtfEditor.Modified)
                {//文档被改动过
                    DialogResult result = MessageBox.Show("文件内容已更改,想保存文件吗?", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
                    switch (result)
                    {
                        case DialogResult.Yes: //“保存”,则运行保存文件的操作
                            //假设没有选择要保存的文件名称。则弹出保存对话框。由用户选择要保存的文件名称后保存文本
                            if (saveFileDialog.FileName == "")
                            {
                                if (saveFileDialog.ShowDialog(this.TopLevelControl) == DialogResult.OK)
                                {
                                    rtfEditor.SaveFile(saveFileDialog.FileName, RichTextBoxStreamType.PlainText);
                                }
                            }
                            else
                            {
                                //假设已经选择了要保存的文件名称,则保存文本到文件里
                                rtfEditor.SaveFile(saveFileDialog.FileName, RichTextBoxStreamType.PlainText);
                            }
                            break;
                        case DialogResult.No://不保存
                            break;
    
                        default://取消操作
                            e.Cancel = true;
                            break;
                    }
                }
            }
            /// <summary>
            /// 事件处理 - 窗体关闭
            /// </summary>
            /// <param name="e"></param>
            protected override void OnFormClosing(FormClosingEventArgs e)
            {
                base.OnFormClosing(e);
    
                if (!this.Modal)
                    App_Closing(e);
            }
    
        }
    }

    3.如图所看到的:


    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    利用FUSE编写自定义的文件系统
    ubuntu16.04 overlay 不支持redirect_dir开关
    ip rule实现源IP路由,实现一个主机多IP(或多网段)同时通(外部看是完全两个独立IP)
    段地址机制以及段地址转换触发segmentation falt
    sshfs+overlayfs实现一个共享只读资源被多个主机挂载成可写目录
    解析prototxt文件的python库 prototxt-parser(使用parsy自定义文件格式解析)
    arris1750 pandorabox安装bandwidthd之后带宽监控(nlbwmon)报资源不足
    工作中的C++问题汇总
    CMake相关代码片段
    编写合格的C代码(1):通过编译选项将特定警告视为错误
  • 原文地址:https://www.cnblogs.com/yxwkf/p/4799895.html
Copyright © 2011-2022 走看看