RTF Editor 控件(.NET 2.0, v1.0)

RtfEditor v1.0 提供下载 点击下载(包括 RtfEditor.dll、Wordpad 和 Wordpad 程序源代码)

 

本文永久链接:http://chuangen.name/archives/2008/07/26/rtf-editor-control.html

一、介绍

在应用程序设计中,大家经常会用 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 窗体中使用该控件的步骤(嵌入到窗体):

  1. 在你的项目中添加对 RtfEditor.dll 的引用;
  2. 将该DLL中的“RtfEditor”控件添加到工具箱备用;
  3. 在设计模式,将“RtfEditor”控件从工具箱拖放到你的窗体上,并进行编辑。
  4. 编写代码。

例如,你从数据库读取一段 RTF 格式文本,想要显示到 RtfEditor 中,则:

  1. rtfEditor.RtfDocument = strRtf;

更多功能使用方法,请参阅演示程序 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 源代码如下。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO;
  9. using FCG.Windows.Forms;
  10. namespace Wordpad
  11. {
  12.     public partial class WordpadForm : Form
  13.     {
  14.         public WordpadForm()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.         /// <summary>
  19.         /// 获取文档编辑区域使用的 RtfEditor 实例。
  20.         /// </summary>
  21.         internal RtfEditor RtfEditor
  22.         {
  23.             get
  24.             {
  25.                 return rtfEditor;
  26.             }
  27.         }
  28.         void rtfEditor_FileNameChanged(object sender, EventArgs e)
  29.         {
  30.             string FileName = Path.GetFileName(rtfEditor.FileFullName);
  31.             if (FileName == "")
  32.                 FileName = "未命名";
  33.             this.Text = FileName + " - " + Application.ProductName;
  34.         }
  35.         protected override void OnLoad(EventArgs e)
  36.         {
  37.             base.OnLoad(e);
  38.             
  39.             string[] args =Environment.GetCommandLineArgs();
  40.             if (args.Length < 2)//arg[0]=exepath , arg[1] = filename
  41.             {
  42.                 //File_Func_NewFile();
  43.             }
  44.             else
  45.             {
  46.                 string filename =args[1];
  47.                 if(filename.Trim().ToLower()!="-test")
  48.                     rtfEditor.LoadFile(filename);
  49.             }
  50.             rtfEditor.FileNameChanged += new EventHandler(rtfEditor_FileNameChanged);
  51.             rtfEditor_FileNameChanged(thisnull);
  52.         }
  53.         /// <summary>
  54.         /// 在关闭程序之前,判断文本是否需要保存
  55.         /// </summary>
  56.         private void App_Closing(FormClosingEventArgs e)
  57.         {
  58.             if (rtfEditor.Modified)
  59.             {//文档被修改过
  60.                 DialogResult result = MessageBox.Show("文件内容已更改,想保存文件吗?""提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
  61.                 switch (result)
  62.                 {
  63.                     case DialogResult.Yes: //“保存”,则执行保存文件的操作
  64.                         //如果没有选择要保存的文件名,则弹出保存对话框,由用户选择要保存的文件名后保存文本
  65.                         if (saveFileDialog.FileName == "")
  66.                         {
  67.                             if (saveFileDialog.ShowDialog(this.TopLevelControl) == DialogResult.OK)
  68.                             {
  69.                                 rtfEditor.SaveFile(saveFileDialog.FileName, RichTextBoxStreamType.PlainText);
  70.                             }
  71.                         }
  72.                         else
  73.                         {//如果已经选择了要保存的文件名,则保存文本到文件中
  74.                             rtfEditor.SaveFile(saveFileDialog.FileName, RichTextBoxStreamType.PlainText);
  75.                         }
  76.                         break;
  77.                     case DialogResult.No://不保存
  78.                         break;
  79.                     default://取消操作
  80.                         e.Cancel = true;
  81.                         break;
  82.                 }
  83.             }
  84.         }
  85.         /// <summary>
  86.         /// 事件处理 - 窗口关闭
  87.         /// </summary>
  88.         /// <param name="e"></param>
  89.         protected override void OnFormClosing(FormClosingEventArgs e)
  90.         {
  91.             base.OnFormClosing(e);
  92.             if (!this.Modal)
  93.                 App_Closing(e);
  94.         }
  95.     }
  96. }

PostEditForm 窗体类演示 RtfEditor 控件的使用,运行界面:

 

更新历史

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值