C# RichTextBox 制作文本编辑器

C# RichTextBox 制作文本编辑器,第1张

概述利用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 控件的文本中搜索位于控件内特定位置的字符串。

效果图

如下【以下设置文本对应的格式】:

核心代码

如下

  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 : BaseRichFormat111 112         113 114             rtbInfo.SelectionAlignment = HorizontalAlignment.left;115 116 117 118 119     120      居中对齐121     122      CenterRichFormat : BaseRichFormat123 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 : BaseRichFormat143 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 : BaseRichFormat163 164         165 166             每次以10个像素进行缩进167             rtbInfo.SelectionIndent = rtbInfo.SelectionIndent + 10;168 169 170 171 172     173     174     175      OutIndentRichFormat : BaseRichFormat176 177         178 179             180             rtbInfo.SelectionIndent = rtbInfo.SelectionIndent - 181 182 183 184 185     186      下标187     188      SubScriptRichFormat : BaseRichFormat189 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 : BaseRichFormat207 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 : BaseRichFormat225 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 : BaseRichFormat242 243         244 245             colorDialog f =  colorDialog();246             247 248 249                 rtbInfo.Selectioncolor = f.color;250 251 252 253 254 255     256      文本背景颜色257     258      BgcolorRichFormat : BaseRichFormat259 260         261 262             colorDialog f = 263             264 265 266                 rtbInfo.SelectionBackcolor =267 268 269 270 271 272     273      ul列表,项目符号样式274     275      ulRichFormat : BaseRichFormat276 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 : BaseRichFormat295 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                 try305                 {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 : BaseRichFormat331 332         333 334             rtbInfo.SelectedText = ""335 336 337 338 339     340      查找341     342      SearchRichFormat : BaseRichFormat343 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 : BaseRichFormat372 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 : BaseRichFormat395 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  DemoRichText13 14     partial  MainForm : Form15 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是一个功能丰富的控件,值得学习。

源码下载链接

@L_419_0@

 

总结

以上是内存溢出为你收集整理的C# RichTextBox 制作文本编辑器全部内容,希望文章能够帮你解决C# RichTextBox 制作文本编辑器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1213849.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-04
下一篇2022-06-04

发表评论

登录后才能评论

评论列表(0条)

    保存