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


版權聲明:本文博主原創文章。博客,未經同意不得轉載。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM