本文利用一個簡單的小例子【文本編輯器】,講解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
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