您的位置:首页 > 编程语言 > C#

c#-RTF文本编辑器

2015-09-11 08:38 323 查看
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.如图所看到的:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: