欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# RichTextBox制作文本编辑器

程序员文章站 2023-01-07 11:31:26
本文利用一个简单的小例子【文本编辑器】,讲解richtextbox的用法。 windows窗体中的richtextbox控件用于显示,输入和操作格式化的文本,richte...

本文利用一个简单的小例子【文本编辑器】,讲解richtextbox的用法。

windows窗体中的richtextbox控件用于显示,输入和操作格式化的文本,richtextbox除了拥有textbox控件的所有功能外,还可以显示字体,颜色,链接,从文件中读取和加载图像,以及查找指定的字符。richtextbox控件通常用于提供类似字体处理程序(如microsoft word)的文本操作和显示功能。richtextbox控件可以显示滚动条,且默认根据需要进行显示。

涉及知识点:

  • selectionfont 获取或设置当前选定文本或插入点的字体。
  • fontstyle 指定应用到文本的字形信息。
  • selectionalignment  获取或设置应用到当前选定内容或插入点的对齐方式。
  • selectionindent 获取或设置所选内容开始行的缩进距离(以像素为单位)。
  • selectioncharoffset 获取或设置控件中的文本是显示在基线上、作为上标还是作为基线下方的下标。
  • selectioncolor 获取或设置当前选定文本或插入点的文本颜色。
  • selectionbackcolor   获取或设置在 system.windows.forms.richtextbox 控件中选中文本时文本的颜色。
  • selectionbullet 获取或设置一个值,通过该值指示项目符号样式是否应用到当前选定内容或插入点。
  • clipboard paste 粘贴指定剪贴板格式的剪贴板内容【插入图片时使用】。
  • find 在对搜索应用特定选项的情况下,在 system.windows.forms.richtextbox 控件的文本中搜索位于控件内特定位置的字符串。

效果图如下【以下设置文本对应的格式】:

C# RichTextBox制作文本编辑器

核心代码如下

using system;
using system.collections.generic;
using system.drawing;
using system.drawing.printing;
using system.linq;
using system.text;
using system.threading.tasks;
using system.windows.forms;

namespace demorichtext.model
{
 public class defaultrickformat : baserichformat
 {
  public override void setformat(richtextbox rtbinfo)
  {

  }
 }

 /// <summary>
 /// 加粗格式
 /// </summary>
 public class boldrichformat : baserichformat
 {
  public override void setformat(richtextbox rtbinfo)
  {
   font oldfont = rtbinfo.selectionfont;
   font newfont;
   if (oldfont.bold)
   {
    newfont = new font(oldfont, oldfont.style & ~fontstyle.bold);//支持位于运算
   }
   else
   {
    newfont = new font(oldfont, oldfont.style | fontstyle.bold);
   }
   rtbinfo.selectionfont = newfont;
  }
 }

 /// <summary>
 /// 斜体
 /// </summary>
 public class italicrichformat : baserichformat
 {
  public override void setformat(richtextbox rtbinfo)
  {
   font oldfont = rtbinfo.selectionfont;
   font newfont;
   if (oldfont.italic)
   {
    newfont = new font(oldfont, oldfont.style & ~fontstyle.italic);
   }
   else
   {
    newfont = new font(oldfont, oldfont.style | fontstyle.italic);
   }
   rtbinfo.selectionfont = newfont;
   rtbinfo.focus();
  }
 }

 /// <summary>
 /// 下划线
 /// </summary>
 public class underlinerichformat : baserichformat
 {
  public override void setformat(richtextbox rtbinfo)
  {
   font oldfont = rtbinfo.selectionfont;
   font newfont;
   if (oldfont.underline)
   {
    newfont = new font(oldfont, oldfont.style & ~fontstyle.underline);
   }
   else
   {
    newfont = new font(oldfont, oldfont.style | fontstyle.underline);
   }
   rtbinfo.selectionfont = newfont;
   rtbinfo.focus();
  }
 }

 /// <summary>
 /// 删除线
 /// </summary>
 public class strikelinerichformat : baserichformat
 {
  public override void setformat(richtextbox rtbinfo)
  {
   font oldfont = rtbinfo.selectionfont;
   font newfont;
   if (oldfont.underline)
   {
    newfont = new font(oldfont, oldfont.style & ~fontstyle.strikeout);
   }
   else
   {
    newfont = new font(oldfont, oldfont.style | fontstyle.strikeout);
   }
   rtbinfo.selectionfont = newfont;
   rtbinfo.focus();
  }
 }

 /// <summary>
 /// 左对齐
 /// </summary>
 public class leftrichformat : baserichformat
 {
  public override void setformat(richtextbox rtbinfo)
  {
   rtbinfo.selectionalignment = horizontalalignment.left;
   rtbinfo.focus();
  }
 }

 /// <summary>
 /// 居中对齐
 /// </summary>
 public class centerrichformat : baserichformat
 {
  public override void setformat(richtextbox rtbinfo)
  {
   if (rtbinfo.selectionalignment == horizontalalignment.center)
   {
    rtbinfo.selectionalignment = horizontalalignment.left;
   }
   else
   {
    rtbinfo.selectionalignment = horizontalalignment.center;
   }

   rtbinfo.focus();
  }
 }

 /// <summary>
 /// 右对齐
 /// </summary>
 public class rightrichformat : baserichformat
 {
  public override void setformat(richtextbox rtbinfo)
  {
   if (rtbinfo.selectionalignment == horizontalalignment.right)
   {
    rtbinfo.selectionalignment = horizontalalignment.left;
   }
   else
   {
    rtbinfo.selectionalignment = horizontalalignment.right;
   }

   rtbinfo.focus();
  }
 }

 /// <summary>
 /// 缩进对齐
 /// </summary>
 public class indentrichformat : baserichformat
 {
  public override void setformat(richtextbox rtbinfo)
  {
   //每次以10个像素进行缩进
   rtbinfo.selectionindent = rtbinfo.selectionindent + 10;
   rtbinfo.focus();
  }
 }

 /// <summary>
 /// 缩进对齐
 /// </summary>
 public class outindentrichformat : baserichformat
 {
  public override void setformat(richtextbox rtbinfo)
  {
   //每次以10个像素进行缩进
   rtbinfo.selectionindent = rtbinfo.selectionindent - 10;
   rtbinfo.focus();
  }
 }

 /// <summary>
 /// 下标
 /// </summary>
 public class subscriptrichformat : baserichformat
 {
  public override void setformat(richtextbox rtbinfo)
  {
   if (rtbinfo.selectioncharoffset < 0)
   {
    rtbinfo.selectioncharoffset = 0;
   }
   else {
    rtbinfo.selectioncharoffset = -5;
   }
   rtbinfo.focus();
  }
 }

 /// <summary>
 /// 上标
 /// </summary>
 public class superscriptrichformat : baserichformat
 {
  public override void setformat(richtextbox rtbinfo)
  {
   if (rtbinfo.selectioncharoffset > 0)
   {
    rtbinfo.selectioncharoffset = 0;
   }
   else {
    rtbinfo.selectioncharoffset = 5;
   }
   rtbinfo.focus();
  }
 }

 /// <summary>
 /// 字体
 /// </summary>
 public class fontrichformat : baserichformat
 {
  public override void setformat(richtextbox rtbinfo)
  {
   fontdialog f = new fontdialog();
   if (f.showdialog() == dialogresult.ok)
   {
    fontfamily family = f.font.fontfamily;
    rtbinfo.selectionfont = new font(family, rtbinfo.selectionfont.size, rtbinfo.selectionfont.style);
   }
   rtbinfo.focus();
  }
 }

 /// <summary>
 /// 文本颜色
 /// </summary>
 public class forecolorrichformat : baserichformat
 {
  public override void setformat(richtextbox rtbinfo)
  {
   colordialog f = new colordialog();
   if (f.showdialog() == dialogresult.ok)
   {

    rtbinfo.selectioncolor = f.color;
   }
   rtbinfo.focus();
  }
 }

 /// <summary>
 /// 文本背景颜色
 /// </summary>
 public class bgcolorrichformat : baserichformat
 {
  public override void setformat(richtextbox rtbinfo)
  {
   colordialog f = new colordialog();
   if (f.showdialog() == dialogresult.ok)
   {

    rtbinfo.selectionbackcolor = f.color;
   }
   rtbinfo.focus();
  }
 }

 /// <summary>
 /// ul列表,项目符号样式
 /// </summary>
 public class ulrichformat : baserichformat
 {
  public override void setformat(richtextbox rtbinfo)
  {
   if (rtbinfo.selectionbullet)
   {
    rtbinfo.selectionbullet = false;
   }
   else {
    rtbinfo.selectionbullet = true;
    rtbinfo.bulletindent = 10;
   }
   rtbinfo.focus();
  }
 }

 /// <summary>
 /// 图片插入
 /// </summary>
 public class picrichformat : baserichformat
 {
  public override void setformat(richtextbox rtbinfo)
  {
   openfiledialog o = new openfiledialog();
   o.initialdirectory = appdomain.currentdomain.basedirectory;
   o.title = "请选择图片";
   o.filter = "jpeg|*.jpeg|jpg|*.jpg|png|*.png|gif|*.gif"; 
   if (o.showdialog() == dialogresult.ok) {
    string filename = o.filename;
    try
    {
     image bmp = image.fromfile(filename);
     clipboard.setdataobject(bmp);

     dataformats.format dataformat = dataformats.getformat(dataformats.bitmap);
     if (rtbinfo.canpaste(dataformat))
     {
      rtbinfo.paste(dataformat);
     }
      
    }
    catch (exception exc)
    {
     messagebox.show("图片插入失败。" + exc.message, "提示",
         messageboxbuttons.ok, messageboxicon.information);
    }

   }
   rtbinfo.focus();
  }
 }

 /// <summary>
 /// 删除
 /// </summary>
 public class delrichformat : baserichformat
 {
  public override void setformat(richtextbox rtbinfo)
  {
   rtbinfo.selectedtext = "";
   rtbinfo.focus();
  }
 }

 /// <summary>
 /// 查找
 /// </summary>
 public class searchrichformat : baserichformat
 {
  public override void setformat(richtextbox rtbinfo)
  {
   string find = rtbinfo.tag.tostring();
   int index= rtbinfo.find(find, 0,richtextboxfinds.none);
   int startpos = index;
   int nextindex = 0;
   while (nextindex != startpos)//循环查找字符串,并用蓝色加粗12号times new roman标记之 
   {
    rtbinfo.selectionstart = index;
    rtbinfo.selectionlength = find.length;
    rtbinfo.selectioncolor = color.blue;
    rtbinfo.selectionfont = new font("times new roman", (float)12, fontstyle.bold);
    rtbinfo.focus();
    nextindex = rtbinfo.find(find, index + find.length, richtextboxfinds.none);
    if (nextindex == -1)//若查到文件末尾,则充值nextindex为初始位置的值,使其达到初始位置,顺利结束循环,否则会有异常。 
    {
     nextindex = startpos;
    }
    index = nextindex;
   }
   rtbinfo.focus();
  }
 }

 /// <summary>
 /// 打印
 /// </summary>
 public class printrichformat : baserichformat
 {
  private richtextbox richtextbox;

  public override void setformat(richtextbox rtbinfo)
  {
   this.richtextbox = rtbinfo;
   printdocument pd = new printdocument();
   pd.printpage += new printpageeventhandler(pd_printpage);
   // 打印文档
   pd.print();
  }

  private void pd_printpage(object sender, printpageeventargs ev)
  {
   //ev.graphics.drawstring(richtextbox.text);
   //ev.hasmorepages = true;
  }
 }

 /// <summary>
 /// 字体大小
 /// </summary>
 public class fontsizerichformat : baserichformat
 {
  public override void setformat(richtextbox rtbinfo)
  {
   string fontsize = rtbinfo.tag.tostring();
   float fsize = 0.0f;
   if (float.tryparse(fontsize, out fsize)) {
    rtbinfo.selectionfont = new font(rtbinfo.font.fontfamily, fsize, rtbinfo.selectionfont.style);
   }
   rtbinfo.focus();
  }
 }
} 

页面代码【由于实现了代码封装,所有页面代码较少】

using demorichtext.model;
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.threading.tasks;
using system.windows.forms;

namespace demorichtext
{
 public partial class mainform : form
 {
  public mainform()
  {
   initializecomponent();
  }
  

  public void btnbuttonclick(object sender, eventargs e) {
   button btn = (button)sender;
   btntype btntype;
   if (enum.tryparse<btntype>(btn.tag.tostring(), out btntype)) {
    if (btntype == btntype.search) {
     if (!string.isnullorempty(this.txtsearch.text.trim()))
     {
      this.rtbinfo.tag = this.txtsearch.text.trim();
     }
     else {
      return;
     }
     
    }
    irichformat richfomat = richformatfactory.createrichformat(btntype);
    richfomat.setformat(this.rtbinfo);
   }
  }

  private void combfontsize_selectedindexchanged(object sender, eventargs e)
  {
   float fsize = 12.0f;
   if (combfontsize.selectedindex > -1) {
    if (float.tryparse(combfontsize.selecteditem.tostring(), out fsize)) {
     rtbinfo.tag = fsize.tostring();
     irichformat richfomat = richformatfactory.createrichformat(btntype.fontsize);
     richfomat.setformat(this.rtbinfo);
    }
    return;
   }
  }
 }
}

richtextbox是一个功能丰富的控件,值得学习。

点击文末原文地址下载源码。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。