本文利用一个简单的小例子【文本编辑器】,讲解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 控件的文本中搜索位于控件内特定位置的字符串。

效果图

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

核心代码

如下

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Drawing;
  4 using System.Drawing.Printing;
  5 using System.Linq;
  6 using System.Text;
  7 using System.Threading.Tasks;
  8 using System.Windows.Forms;
  9
 10 namespace DemoRichText.Model
 11 {
 12     public class DefaultRickFormat : BaseRichFormat
 13     {
 14         public override void SetFormat(RichTextBox rtbInfo)
 15         {
 16
 17         }
 18     }
 19
 20     /// <summary>
 21     /// 加粗格式
 22     /// </summary>
 23     public class BoldRichFormat : BaseRichFormat
 24     {
 25         public override void SetFormat(RichTextBox rtbInfo)
 26         {
 27             Font oldFont = rtbInfo.SelectionFont;
 28             Font newFont;
 29             if (oldFont.Bold)
 30             {
 31                 newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Bold);//支持位于运算
 32             }
 33             else
 34             {
 35                 newFont = new Font(oldFont, oldFont.Style | FontStyle.Bold);
 36             }
 37             rtbInfo.SelectionFont = newFont;
 38         }
 39     }
 40
 41     /// <summary>
 42     /// 斜体
 43     /// </summary>
 44     public class ItalicRichFormat : BaseRichFormat
 45     {
 46         public override void SetFormat(RichTextBox rtbInfo)
 47         {
 48             Font oldFont = rtbInfo.SelectionFont;
 49             Font newFont;
 50             if (oldFont.Italic)
 51             {
 52                 newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Italic);
 53             }
 54             else
 55             {
 56                 newFont = new Font(oldFont, oldFont.Style | FontStyle.Italic);
 57             }
 58             rtbInfo.SelectionFont = newFont;
 59             rtbInfo.Focus();
 60         }
 61     }
 62
 63     /// <summary>
 64     /// 下划线
 65     /// </summary>
 66     public class UnderLineRichFormat : BaseRichFormat
 67     {
 68         public override void SetFormat(RichTextBox rtbInfo)
 69         {
 70             Font oldFont = rtbInfo.SelectionFont;
 71             Font newFont;
 72             if (oldFont.Underline)
 73             {
 74                 newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Underline);
 75             }
 76             else
 77             {
 78                 newFont = new Font(oldFont, oldFont.Style | FontStyle.Underline);
 79             }
 80             rtbInfo.SelectionFont = newFont;
 81             rtbInfo.Focus();
 82         }
 83     }
 84
 85     /// <summary>
 86     /// 删除线
 87     /// </summary>
 88     public class StrikeLineRichFormat : BaseRichFormat
 89     {
 90         public override void SetFormat(RichTextBox rtbInfo)
 91         {
 92             Font oldFont = rtbInfo.SelectionFont;
 93             Font newFont;
 94             if (oldFont.Underline)
 95             {
 96                 newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Strikeout);
 97             }
 98             else
 99             {
100                 newFont = new Font(oldFont, oldFont.Style | FontStyle.Strikeout);
101             }
102             rtbInfo.SelectionFont = newFont;
103             rtbInfo.Focus();
104         }
105     }
106
107     /// <summary>
108     /// 左对齐
109     /// </summary>
110     public class LeftRichFormat : BaseRichFormat
111     {
112         public override void SetFormat(RichTextBox rtbInfo)
113         {
114             rtbInfo.SelectionAlignment = HorizontalAlignment.Left;
115             rtbInfo.Focus();
116         }
117     }
118
119     /// <summary>
120     /// 居中对齐
121     /// </summary>
122     public class CenterRichFormat : BaseRichFormat
123     {
124         public override void SetFormat(RichTextBox rtbInfo)
125         {
126             if (rtbInfo.SelectionAlignment == HorizontalAlignment.Center)
127             {
128                 rtbInfo.SelectionAlignment = HorizontalAlignment.Left;
129             }
130             else
131             {
132                 rtbInfo.SelectionAlignment = HorizontalAlignment.Center;
133             }
134
135             rtbInfo.Focus();
136         }
137     }
138
139     /// <summary>
140     /// 右对齐
141     /// </summary>
142     public class RightRichFormat : BaseRichFormat
143     {
144         public override void SetFormat(RichTextBox rtbInfo)
145         {
146             if (rtbInfo.SelectionAlignment == HorizontalAlignment.Right)
147             {
148                 rtbInfo.SelectionAlignment = HorizontalAlignment.Left;
149             }
150             else
151             {
152                 rtbInfo.SelectionAlignment = HorizontalAlignment.Right;
153             }
154
155             rtbInfo.Focus();
156         }
157     }
158
159     /// <summary>
160     /// 缩进对齐
161     /// </summary>
162     public class IndentRichFormat : BaseRichFormat
163     {
164         public override void SetFormat(RichTextBox rtbInfo)
165         {
166             //每次以10个像素进行缩进
167             rtbInfo.SelectionIndent = rtbInfo.SelectionIndent + 10;
168             rtbInfo.Focus();
169         }
170     }
171
172     /// <summary>
173     /// 缩进对齐
174     /// </summary>
175     public class OutIndentRichFormat : BaseRichFormat
176     {
177         public override void SetFormat(RichTextBox rtbInfo)
178         {
179             //每次以10个像素进行缩进
180             rtbInfo.SelectionIndent = rtbInfo.SelectionIndent - 10;
181             rtbInfo.Focus();
182         }
183     }
184
185     /// <summary>
186     /// 下标
187     /// </summary>
188     public class SubScriptRichFormat : BaseRichFormat
189     {
190         public override void SetFormat(RichTextBox rtbInfo)
191         {
192             if (rtbInfo.SelectionCharOffset < 0)
193             {
194                 rtbInfo.SelectionCharOffset = 0;
195             }
196             else {
197                 rtbInfo.SelectionCharOffset = -5;
198             }
199             rtbInfo.Focus();
200         }
201     }
202
203     /// <summary>
204     /// 上标
205     /// </summary>
206     public class SuperScriptRichFormat : BaseRichFormat
207     {
208         public override void SetFormat(RichTextBox rtbInfo)
209         {
210             if (rtbInfo.SelectionCharOffset > 0)
211             {
212                 rtbInfo.SelectionCharOffset = 0;
213             }
214             else {
215                 rtbInfo.SelectionCharOffset = 5;
216             }
217             rtbInfo.Focus();
218         }
219     }
220
221     /// <summary>
222     /// 字体
223     /// </summary>
224     public class FontRichFormat : BaseRichFormat
225     {
226         public override void SetFormat(RichTextBox rtbInfo)
227         {
228             FontDialog f = new FontDialog();
229             if (f.ShowDialog() == DialogResult.OK)
230             {
231                 FontFamily family = f.Font.FontFamily;
232                 rtbInfo.SelectionFont = new Font(family, rtbInfo.SelectionFont.Size, rtbInfo.SelectionFont.Style);
233             }
234             rtbInfo.Focus();
235         }
236     }
237
238     /// <summary>
239     /// 文本颜色
240     /// </summary>
241     public class ForeColorRichFormat : BaseRichFormat
242     {
243         public override void SetFormat(RichTextBox rtbInfo)
244         {
245             ColorDialog f = new ColorDialog();
246             if (f.ShowDialog() == DialogResult.OK)
247             {
248
249                 rtbInfo.SelectionColor = f.Color;
250             }
251             rtbInfo.Focus();
252         }
253     }
254
255     /// <summary>
256     /// 文本背景颜色
257     /// </summary>
258     public class BgColorRichFormat : BaseRichFormat
259     {
260         public override void SetFormat(RichTextBox rtbInfo)
261         {
262             ColorDialog f = new ColorDialog();
263             if (f.ShowDialog() == DialogResult.OK)
264             {
265
266                 rtbInfo.SelectionBackColor = f.Color;
267             }
268             rtbInfo.Focus();
269         }
270     }
271
272     /// <summary>
273     /// UL列表,项目符号样式
274     /// </summary>
275     public class UlRichFormat : BaseRichFormat
276     {
277         public override void SetFormat(RichTextBox rtbInfo)
278         {
279             if (rtbInfo.SelectionBullet)
280             {
281                 rtbInfo.SelectionBullet = false;
282             }
283             else {
284                 rtbInfo.SelectionBullet = true;
285                 rtbInfo.BulletIndent = 10;
286             }
287             rtbInfo.Focus();
288         }
289     }
290
291     /// <summary>
292     /// 图片插入
293     /// </summary>
294     public class PicRichFormat : BaseRichFormat
295     {
296         public override void SetFormat(RichTextBox rtbInfo)
297         {
298             OpenFileDialog o = new OpenFileDialog();
299             o.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
300             o.Title = "请选择图片";
301             o.Filter = "jpeg|*.jpeg|jpg|*.jpg|png|*.png|gif|*.gif";
302             if (o.ShowDialog() == DialogResult.OK) {
303                 string fileName = o.FileName;
304                 try
305                 {
306                    Image bmp = Image.FromFile(fileName);
307                    Clipboard.SetDataObject(bmp);
308
309                     DataFormats.Format dataFormat = DataFormats.GetFormat(DataFormats.Bitmap);
310                     if (rtbInfo.CanPaste(dataFormat))
311                     {
312                         rtbInfo.Paste(dataFormat);
313                     }
314
315                 }
316                 catch (Exception exc)
317                 {
318                     MessageBox.Show("图片插入失败。" + exc.Message, "提示",
319                                     MessageBoxButtons.OK, MessageBoxIcon.Information);
320                 }
321
322             }
323             rtbInfo.Focus();
324         }
325     }
326
327     /// <summary>
328     /// 删除
329     /// </summary>
330     public class DelRichFormat : BaseRichFormat
331     {
332         public override void SetFormat(RichTextBox rtbInfo)
333         {
334             rtbInfo.SelectedText = "";
335             rtbInfo.Focus();
336         }
337     }
338
339     /// <summary>
340     /// 查找
341     /// </summary>
342     public class SearchRichFormat : BaseRichFormat
343     {
344         public override void SetFormat(RichTextBox rtbInfo)
345         {
346             string find = rtbInfo.Tag.ToString();
347             int index=  rtbInfo.Find(find, 0,RichTextBoxFinds.None);
348             int startPos = index;
349             int nextIndex = 0;
350             while (nextIndex != startPos)//循环查找字符串,并用蓝色加粗12号Times New Roman标记之
351             {
352                 rtbInfo.SelectionStart = index;
353                 rtbInfo.SelectionLength = find.Length;
354                 rtbInfo.SelectionColor = Color.Blue;
355                 rtbInfo.SelectionFont = new Font("Times New Roman", (float)12, FontStyle.Bold);
356                 rtbInfo.Focus();
357                 nextIndex = rtbInfo.Find(find, index + find.Length, RichTextBoxFinds.None);
358                 if (nextIndex == -1)//若查到文件末尾,则充值nextIndex为初始位置的值,使其达到初始位置,顺利结束循环,否则会有异常。
359                 {
360                     nextIndex = startPos;
361                 }
362                 index = nextIndex;
363             }
364             rtbInfo.Focus();
365         }
366     }
367
368     /// <summary>
369     /// 打印
370     /// </summary>
371     public class PrintRichFormat : BaseRichFormat
372     {
373         private RichTextBox richTextbox;
374
375         public override void SetFormat(RichTextBox rtbInfo)
376         {
377             this.richTextbox = rtbInfo;
378             PrintDocument pd = new PrintDocument();
379             pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
380             // 打印文档
381             pd.Print();
382         }
383
384         private void pd_PrintPage(object sender, PrintPageEventArgs ev)
385         {
386             //ev.Graphics.DrawString(richTextbox.Text);
387             //ev.HasMorePages = true;
388         }
389     }
390
391     /// <summary>
392     /// 字体大小
393     /// </summary>
394     public class FontSizeRichFormat : BaseRichFormat
395     {
396         public override void SetFormat(RichTextBox rtbInfo)
397         {
398             string fontSize = rtbInfo.Tag.ToString();
399             float fsize = 0.0f;
400             if (float.TryParse(fontSize, out fsize)) {
401                 rtbInfo.SelectionFont = new Font(rtbInfo.Font.FontFamily, fsize, rtbInfo.SelectionFont.Style);
402             }
403             rtbInfo.Focus();
404         }
405     }
406 }

View Code

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

 1 using DemoRichText.Model;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.ComponentModel;
 5 using System.Data;
 6 using System.Drawing;
 7 using System.Linq;
 8 using System.Text;
 9 using System.Threading.Tasks;
10 using System.Windows.Forms;
11
12 namespace DemoRichText
13 {
14     public partial class MainForm : Form
15     {
16         public MainForm()
17         {
18             InitializeComponent();
19         }
20
21
22         public void btnButtonClick(object sender, EventArgs e) {
23             Button btn = (Button)sender;
24             BTNType btnType;
25             if (Enum.TryParse<BTNType>(btn.Tag.ToString(), out btnType)) {
26                 if (btnType == BTNType.Search) {
27                     if (!string.IsNullOrEmpty(this.txtSearch.Text.Trim()))
28                     {
29                         this.rtbInfo.Tag = this.txtSearch.Text.Trim();
30                     }
31                     else {
32                         return;
33                     }
34
35                 }
36                 IRichFormat richFomat = RichFormatFactory.CreateRichFormat(btnType);
37                 richFomat.SetFormat(this.rtbInfo);
38             }
39         }
40
41         private void combFontSize_SelectedIndexChanged(object sender, EventArgs e)
42         {
43             float fsize = 12.0f;
44             if (combFontSize.SelectedIndex > -1) {
45                 if (float.TryParse(combFontSize.SelectedItem.ToString(), out fsize)) {
46                     rtbInfo.Tag = fsize.ToString();
47                     IRichFormat richFomat = RichFormatFactory.CreateRichFormat(BTNType.FontSize);
48                     richFomat.SetFormat(this.rtbInfo);
49                 }
50                 return;
51             }
52         }
53     }
54 }

View Code

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

源码下载链接

源码链接

转载于:https://www.cnblogs.com/hsiang/p/6691420.html

C# RichTextBox 制作文本编辑器相关推荐

  1. Linuxqt制作文本编辑器_Python实操!速收藏!学习使用Python创建文本编辑器应用程序

    朋友们,大家好,这次再次与作者见面,作者将发表一篇文章,介绍如何使用Python Tkinter创建文本编辑器.Tkinter是一个Python库,在您想要创建应用程序时非常有用. 立即讨论如何制作此 ...

  2. 2023.04.27 QT 制作文本编辑器

    有改变字体.颜色.打开文件以及保存文件功能 一.代码部分: 1. 头文件: #ifndef WIDGET_H #define WIDGET_H#include <QWidget> #inc ...

  3. 用python制作文本编辑器

    import tkinter import tkinter as tk from tkinter import * import tkinter.filedialog import tkinter.m ...

  4. 【tkinter制作文本编辑器(3)】编辑菜单选项栏事件和右键菜单功能实现(撤销、还原、复制、粘贴、剪切、查找及全选)

    编辑菜单选项栏事件功能实现 1. 编辑菜单选项栏事件功能实现步骤 1.1 撤销 1.2 恢复 1.3 剪切 1.4 复制 1.5 粘贴 1.6 全选 1.7 查找 2. 快捷键绑定 3. 右键弹出菜单 ...

  5. python制作文本编辑器_Python小实战:制作文本编辑器

    学了半年了,该施展一下了

  6. 用Canvas实现文本编辑器(支持艺术字渲染与动画)

    导言 目前富文本编辑器的实现主要有两种技术方案:一个是利用contenteditable属性直接对html元素进行编辑,如draft.js:另一种是代理textarea + 自定义div + 模拟光标 ...

  7. Python: pyqt5 自己写一个窗口文本编辑器

    PyQt5是Digia的一套Qt5应用框架与python的结合,同时支持2.x和3.x.Qt库由Riverbank Computing开发,是最强大的GUI库之一. >>> 今天,我 ...

  8. 二、Qt定时器与文本编辑器制作《QT 入门到实战》

    学习目标 了解 qt 的 pixmap 了解 qt 的 label 如何显示图片 了解定时器的开启 了解定时器的关闭 了解文件如何进行读取 了解 QFileDialog 的使用 了解了一个文本编辑器的 ...

  9. 15分钟内制作自己的文本编辑器:Yandex的实践

    好像没有足够的文本编辑器. 但是请考虑以下问题:使用大约60行代码(几乎没有内容),您可以制作自己的安全稳定的文本编辑器. 它会非常简单,不会有很多功能,但是即使您几乎不知道如何编写代码,它也会由您自 ...

  10. yandex浏览器_15分钟内制作自己的文本编辑器:Yandex的实践

    yandex浏览器 好像没有足够的文本编辑器. 但是请考虑以下问题:使用大约60行代码(几乎没有内容),您可以制作自己的安全稳定的文本编辑器. 它会非常简单,不会有很多功能,但是即使您几乎不知道如何编 ...

最新文章

  1. vs编译器 printf 控制台输出_【语言教程】通过语言了解GCC编译器工作过程
  2. 第三届全国县域经济基本竞争力百强县(市)
  3. 蓝桥杯乘法运算java,第四届蓝桥杯Java B——有理数类
  4. 请说明一下MyBatis中命名空间(namespace)的作用是什么?
  5. 真的了解js生成随机数吗
  6. android设置成默认应用程序,在Android中设置和取消设置默认应用
  7. 更改sql-2008sa密码
  8. JS 对象转化为数组
  9. Go语言实战 (William,Kennedy 等著)
  10. 检索 COM 类工厂中 CLSID 为 {xxx} 的组件失败,原因是出现以下错误: 8000401a 因为配置标识不正确,系统无法开始服务器进程。请检查用户名和密码。...
  11. 2021-2027全球与中国弹簧加载探针市场现状及未来发展趋势
  12. 用wget命令从FTP服务器下载数据
  13. 【计算方法】解线性方程组的四种方法
  14. C盘扩容好帮手——傲梅分区助手
  15. MATLAB2016a启动慢
  16. 关于textarea打印问题
  17. 中国移动启动2017年交直流列头柜集采:3个标段约8841台
  18. java编写point类line类_定义一个点类Point,有横坐标x和纵坐标y,定义构造
  19. Faiss(16):编译时添加对AVX512指令的支持
  20. ABAP其他基本语法

热门文章

  1. 超详细的微信公众号创建与管理教程
  2. 用例图之参与者、用例间的四种关系
  3. xbee模块和单片机_XBee与ZigBee模块区别
  4. html表格上下居中 w3c,HTML中怎么把表格居中
  5. Box2DSharp使用手册#1
  6. The following paths are ignored by one of your .gitignore
  7. 计算机桌面上的照片转pdf免费,电脑上怎样快速将图片转PDF
  8. Hadoop3.x 之 MapReduce 开发总结(月薪过万)
  9. C# XmlDocument.Save文件操作System.IO.IOException:The process cannot access the file because it is being
  10. python3实现扫码获取微信openid功能