c#-RTF文本编辑器

来源:互联网 发布:管家婆记账软件免费版 编辑:程序博客网 时间:2024/04/25 03:15

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.如图所示:


0 0