c#-RTF文本編輯器

1「.RTF」什麼?html

多信息文本格式 (RTF) 是一種方便於不一樣的設備、系統查看的文本和圖形文檔格式。編輯器


RTF 使用美國國內標準協會 (ANSI)、 PC-八、 Macintosh(mac蘋果),或 IBM 的 PC 字符設置控制顯示形式和打印形式。
在不一樣的操做系統下建立的RTF文檔可以在多種操做系統和應用程序之間互相傳輸、查看。
當前,做爲 MS-DOS、 Microsoft Windows、 OS/二、 Macintosh蘋果系統,應用程序之間處理文檔的特殊翻譯軟件。ide


RTF是Rich Text Format的縮寫,意即多文本格式。工具

這是一種相似DOC格式(Word文檔)的文件,有很是好的兼容性,使用Windows「附件」中的「寫字板」就能打開並進行編輯。this

使用「寫字板」打開一個RTF格式文件時。將看到文件的內容;假設要查看RTF格式文件的源碼,僅僅要使用「記事本」將它打開便可了。這就是說,你全然可以像編輯HTML文件同樣,使用「記事本」來編輯RTF格式文件。spa


做爲微軟公司的標準文件,早期外界需要數十美圓向微軟付款,才幹購買一本薄薄的RTF標準文件。只是隨着採用RTF格式標準的軟件越來越多。RTF格式也越來越廣泛。微軟公司就把標準文件公開。放在網上供開發人員下載。
RTF格式是不少軟件均可以識別的文件格式。操作系統

比方Word、WPS Office、Excel等均可以打開RTF格式的文件。
翻譯

對普通用戶而言,RTF格式是一個很是好的文件格式轉換工具,用於在不一樣應用程序之間進行格式化文本文檔的傳送。
通用兼容性應該是RTF的最大長處,但同一時候也就具備它的缺點。比方文件通常相對較大(可能因爲嵌入了兼容各類應用程序的控制符號吧)、WORD等應用軟件特有的格式可能沒法正常保存等。

code

2.代碼例如如下:orm

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.如圖所看到的:

相關文章
相關標籤/搜索