C# RichTextBox制作文本编辑器

时间:2022-09-21 08:34:55

本文利用一个简单的小例子【文本编辑器】,讲解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制作文本编辑器

核心代码如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
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();
  }
 }
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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是一个功能丰富的控件,值得学习。

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

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

原文链接:http://www.cnblogs.com/hsiang/archive/2017/04/10/6691420.html