RtfEditor v1.0 提供下载 点击下载(包括 RtfEditor.dll、Wordpad 和 Wordpad 程序源代码)
一、介绍
在应用程序设计中,大家经常会用 RichTextBox 控件显示、输入和操作带有格式的文本。该控件功能完整,但为了让用户使用这些功能,你不得不添加大量的工具栏和菜单,而这类工作很繁琐。为此,我设计了 RTF Editor 控件,它是一个可嵌入到 WinForm 窗体的编辑器。它完整地实现了 RTF 文档编辑功能。您可以创建和编辑文本文档或有复杂格式和图形的文档,并可以将文件保存为普通文本文档(*.txt)或多信息文本文件(*.rtf)。具有如下特性:
- 可以插入图片
- 格式化文本
- 打印支持
- 所见即所得编辑;支持页面设置和打印预览。
- 在其他 .NET 项目中引用
该控件具有大部分常用菜单和工具栏,你可以方便地将它用于你的应用程序中,省去大量的工作。控件界面如下图所示:
该控件是免费的,无任何功能限制(不提供源代码,但使用该控件设计的写字板程序源代码提供下载)。有任何问题可留言给我(email: chuangen@126.com,website: http://chuangen.name )
二、RtfEditor 类
类定义:public class RtfEditor : UserControl
说明:RTF 编辑器控件。
构造函数成员
|
名称
|
访问
|
摘要
|
|
RtfEditor()
|
public
|
构造方法
|
属性成员
|
名称
|
访问
|
摘要
|
|
CopyrightVisible : Boolean
|
public
|
是否显示设计者信息。
|
|
FileFullName : String
|
public
|
当前编辑文档的全路径名称。
|
|
MenuBarVisible : Boolean
|
public
|
是否显示 菜单栏。
|
|
Modified : Boolean
|
public
|
获取或设置一个值,该值指示自创建文本框控件或上次设置该控件的内容后,用户修改了该控件。
|
|
ReadOnly : Boolean
|
public
|
获取或设置一个值,该值指示编辑器中的文本是否为只读。
|
|
RenderMode : ToolStripRenderMode
|
public
|
获取或设置要应用于 RtfEditor 的绘制样式。
|
|
RtfDocument : String
|
public
|
获取或设置 RtfEditor 正在编辑的文本,包括所有 RTF 格式代码。
|
|
StatusBarVisible : Boolean
|
public
|
是否显示 状态栏。
|
|
TextDocument : String
|
public
|
获取或设置 RtfEditor 正在编辑的纯文本。
|
|
ToolBarFormatVisible : Boolean
|
public
|
是否显示 工具栏-格式。
|
|
ToolBarStandardVisible : Boolean
|
public
|
是否显示 工具栏-常用。
|
方法成员
|
名称
|
访问
|
摘要
|
|
LoadFile() : Void
|
public
|
打开指定文件
|
|
SaveFile() : Void
|
public
|
将 RtfEditor 控件的内容保存到开放式数据流。
|
|
SaveFile() : Void
|
public
|
将 RtfEditor 的内容保存到 RTF 格式文件。
|
|
SaveFile() : Void
|
public
|
将 RtfEditor 的内容保存到特定类型的文件中。
|
三、控件使用方法
RTF Editor控件包含在 RtfEditor.dll 中。在 WinForm 窗体中使用该控件的步骤(嵌入到窗体):
- 在你的项目中添加对 RtfEditor.dll 的引用;
- 将该DLL中的“RtfEditor”控件添加到工具箱备用;
- 在设计模式,将“RtfEditor”控件从工具箱拖放到你的窗体上,并进行编辑。
- 编写代码。
例如,你从数据库读取一段 RTF 格式文本,想要显示到 RtfEditor 中,则:
[code lang="csharp"]rtfEditor.RtfDocument = strRtf;[/code]
更多功能使用方法,请参阅演示程序 Wordpad.exe 的源代码。
四、演示程序说明
Wordpad.exe 主要目的是演示 RtfEditor 控件使用效果,它的源代码提供下载。它可以接受一个文件名或特定开关项(如“-test”)做参数,示例如下:
- wordpad.exe 启动写字板,新建文档;
- wordpad.exe file.txt 使用写字板打开文本文档;
- wordpad.exe file.rtf 使用写字板打开 RTF 文档;
- wordpad.exe -test 启动 RTF Editor 控件的演示程序。
WordpadForm 窗体类实现了完整的写字板功能,你可以使用该窗体启动“写字板”样式的独立编辑窗体。运行界面如下:

WordpadForm.cs 源代码如下。
[code lang="csharp"]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 Wordpad
{
public partial class WordpadForm : Form
{
public WordpadForm()
{
InitializeComponent();
}
///
/// 获取文档编辑区域使用的 RtfEditor 实例。
///
internal RtfEditor RtfEditor
{
get
{
return rtfEditor;
}
}
void rtfEditor_FileNameChanged(object sender, EventArgs e)
{
string FileName = Path.GetFileName(rtfEditor.FileFullName);
if (FileName == "")
FileName = "未命名";
this.Text = FileName + " - " + Application.ProductName;
}
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);
}
///
/// 在关闭程序之前,判断文本是否需要保存
///
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;
}
}
}
///
/// 事件处理 - 窗口关闭
///
///
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
if (!this.Modal)
App_Closing(e);
}
}
}[/code]
PostEditForm 窗体类演示 RtfEditor 控件的使用,运行界面:

更新历史
1. 写字板.NET,发表于 2007年05月14日 14:10:00
Url: http://blog.csdn.net/chuangen/archive/2007/05/14/1608187.aspx