国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - C# - C# RichTextBox制作文本編輯器

C# RichTextBox制作文本編輯器

2021-12-31 13:29飛翔的月亮 C#

這篇文章主要為大家詳細(xì)介紹了C# RichTextBox制作文本編輯器的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文利用一個簡單的小例子【文本編輯器】,講解richtextbox的用法。

windows窗體中的richtextbox控件用于顯示,輸入和操作格式化的文本,richtextbox除了擁有textbox控件的所有功能外,還可以顯示字體,顏色,鏈接,從文件中讀取和加載圖像,以及查找指定的字符。richtextbox控件通常用于提供類似字體處理程序(如microsoft word)的文本操作和顯示功能。richtextbox控件可以顯示滾動條,且默認(rèn)根據(jù)需要進行顯示。

涉及知識點:

  • selectionfont 獲取或設(shè)置當(dāng)前選定文本或插入點的字體。
  • fontstyle 指定應(yīng)用到文本的字形信息。
  • selectionalignment  獲取或設(shè)置應(yīng)用到當(dāng)前選定內(nèi)容或插入點的對齊方式。
  • selectionindent 獲取或設(shè)置所選內(nèi)容開始行的縮進距離(以像素為單位)。
  • selectioncharoffset 獲取或設(shè)置控件中的文本是顯示在基線上、作為上標(biāo)還是作為基線下方的下標(biāo)。
  • selectioncolor 獲取或設(shè)置當(dāng)前選定文本或插入點的文本顏色。
  • selectionbackcolor   獲取或設(shè)置在 system.windows.forms.richtextbox 控件中選中文本時文本的顏色。
  • selectionbullet 獲取或設(shè)置一個值,通過該值指示項目符號樣式是否應(yīng)用到當(dāng)前選定內(nèi)容或插入點。
  • clipboard paste 粘貼指定剪貼板格式的剪貼板內(nèi)容【插入圖片時使用】。
  • find 在對搜索應(yīng)用特定選項的情況下,在 system.windows.forms.richtextbox 控件的文本中搜索位于控件內(nèi)特定位置的字符串。

效果圖如下【以下設(shè)置文本對應(yīng)的格式】:

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>
 /// 下標(biāo)
 /// </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>
 /// 上標(biāo)
 /// </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)//循環(huán)查找字符串,并用藍(lán)色加粗12號times new roman標(biāo)記之
   {
    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為初始位置的值,使其達(dá)到初始位置,順利結(jié)束循環(huán),否則會有異常。
    {
     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();
  }
 }
}

頁面代碼【由于實現(xiàn)了代碼封裝,所有頁面代碼較少】

?
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是一個功能豐富的控件,值得學(xué)習(xí)。

點擊文末原文地址下載源碼。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩成人一区 | 亚洲高清视频在线 | 免费一级网站 | 亚洲aⅴ天堂av在线电影软件 | 极品一区 | eeuss国产一区二区三区四区 | 国产福利视频在线 | 中文字幕欧美日韩 | 在线看片你懂得 | 久久精品国产精品青草 | 久久久久久香蕉 | 韩日一区二区三区 | 久久综合久久88 | 97久久香蕉国产线看观看 | 亚洲精品一区 | 国产精品自拍在线观看 | 欧美日韩成人 | 电影在线观看免费 | 欧美黄视频在线观看 | 欧美日本一区二区三区 | 日本黄色一级电影 | 99免费在线视频 | 日韩a电影 | 精品黑人一区二区三区久久 | 激情综合五 | 国产精品爱久久久久久久 | 九九资源站 | 欧美一级二级三级视频 | 亚洲欧美制服诱惑 | 不卡一二区 | 中文字幕一区二区三区在线视频 | 狠狠干天天草 | 青青草久久网 | 网站黄色在线观看免费 | 日韩免费一区 | 国产成人久久一区二区三区 | 91精品国产综合久久久久 | 久久国产成人 | 男人的天堂视频网站 | 欧美午夜影院 | 久久久久国产精品一区二区 |