加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C# RichTextBox 制作文本编辑器

发布时间:2020-12-16 01:14:01 所属栏目:百科 来源:网络整理
导读:本文利用一个简单的小例子【文本编辑器】,讲解RichTextBox的用法,仅供学习分享使用,如有不足之处,还请指正。 Windows窗体中的RichTextBox控件用于显示,输入和操作格式化的文本,RichTextBox除了拥有TextBox控件的所有功能外,还可以显示字体,颜色,链

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

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

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


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

源码下载链接

源码链接

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读