激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

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

C# RichTextBox制作文本編輯器

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

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

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

延伸 · 閱讀

精彩推薦
  • C#Unity3D實現虛擬按鈕控制人物移動效果

    Unity3D實現虛擬按鈕控制人物移動效果

    這篇文章主要為大家詳細介紹了Unity3D實現虛擬按鈕控制人物移動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一...

    shenqingyu060520232410972022-03-11
  • C#C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    這篇文章主要介紹了C# 實現對PPT文檔加密、解密及重置密碼的操作方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下...

    E-iceblue5012022-02-12
  • C#C#裁剪,縮放,清晰度,水印處理操作示例

    C#裁剪,縮放,清晰度,水印處理操作示例

    這篇文章主要為大家詳細介紹了C#裁剪,縮放,清晰度,水印處理操作示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    吳 劍8332021-12-08
  • C#C#實現XML文件讀取

    C#實現XML文件讀取

    這篇文章主要為大家詳細介紹了C#實現XML文件讀取的相關代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    Just_for_Myself6702022-02-22
  • C#深入解析C#中的交錯數組與隱式類型的數組

    深入解析C#中的交錯數組與隱式類型的數組

    這篇文章主要介紹了深入解析C#中的交錯數組與隱式類型的數組,隱式類型的數組通常與匿名類型以及對象初始值設定項和集合初始值設定項一起使用,需要的...

    C#教程網6172021-11-09
  • C#C#通過KD樹進行距離最近點的查找

    C#通過KD樹進行距離最近點的查找

    這篇文章主要為大家詳細介紹了C#通過KD樹進行距離最近點的查找,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    帆帆帆6112022-01-22
  • C#WPF 自定義雷達圖開發實例教程

    WPF 自定義雷達圖開發實例教程

    這篇文章主要介紹了WPF 自定義雷達圖開發實例教程,本文介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下...

    WinterFish13112021-12-06
  • C#C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    這篇文章主要介紹了C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題,簡單描述了訪問者模式的定義并結合具體實例形式分析了C#使用訪問者模式解決長...

    GhostRider9502022-01-21
主站蜘蛛池模板: ⅴideo裸体秀hd | 日本一区二区三区视频在线 | 久久久久久精 | cosplay裸体福利写真 | 免费观看一级欧美大 | 日韩在线观看视频免费 | av在线一区二区三区 | 九九热九九热 | 二级大黄大片高清在线视频 | 亚洲3atv精品一区二区三区 | 久久久久久久免费精品 | av电影免费观看 | av在线免费电影 | fc2成人免费人成在线观看播放 | 视频一区二区三区在线播放 | 成人毛片网站 | 在线观看福利网站 | 99久99| 欧美亚洲一级 | 在线播放中文 | 欧美不卡在线 | 久久人人爽人人爽人人片av高清 | 国产99久久| 99影视在线视频免费观看 | 亚洲啊v在线观看 | 亚洲精品一区二区三区大胸 | 在线免费91 | 欧美三级美国一级 | 97中文字幕第一一一页 | 污版视频在线观看 | 亚洲综合中文 | 日本黄色一级视频 | 久久99久久98精品免观看软件 | 黄色网页在线观看 | 韩国精品视频在线观看 | 日本网站一区 | 黑人三级毛片 | 激情五月少妇a | 国产成人综合在线观看 | 欧美一级做 | 国产成人精品一区二区仙踪林 |