(九)C 之WinForm程序设计-升级版文本编辑器
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.****.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
C#WinForm程序设计之升级版文本编辑器
前面的那篇博文讲解了一个简单版本的文本编辑器,实现了一些最简单的功能。那么这里我们对那个文本编辑器做一个升级吧!既然是升级版,那么就要做的像Word一点咯!首先我们来看一下我设计的界面吧!
文件(F):
编辑(E):
格式(O):
怎么样,这个界面是不是有点像Word了?图中的小图标下载地址为【小图标】。如果你有兴趣跟我一起做下去,那也把这个界面设计出来吧(顶部的有下拉功能的菜单我们先不管,功能跟中间部分的ToolStrip功能基本是一样的),然后继续看博客吧!
我们从左到右实现中间的ToolStrip栏的功能!
首先我们实现中间ToolStrip栏的第一个按钮(Name为openFileToolStripButton)的功能:打开读取文件内容并把文本内容填充到下面的RichTextBox里面吧!监听其单击事件,代码如下:
定义两个全局变量
private String title = "Untitled"; //保存打开的文件的标题 Encoding ec=Encoding.UTF8; //设置文本的格式为 UTF-8
- 1
- 2
监听点击事件:
private void openFileToolStripButton_Click(object sender, EventArgs e) { /** * openFileDialog1 是在设计界面拖出来的控件 OpenFileDialog * * 主要是打开 rtf 格式的文件 */ openFileDialog1.Filter = "文本文件|*.txt;*.html;*.docx;*.doc;*.rtf|所有文件|*.*"; //文件打开的过滤器 if (openFileDialog1.ShowDialog() == DialogResult.OK) { title = openFileDialog1.FileName; this.Text = title; //显示打开的文件名 richTextBox1.Modified = false; string ext = title.Substring(title.LastIndexOf(".") + 1);//获取文件格式 ext = ext.ToLower(); FileStream fs = new FileStream(title, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs, ec); if (ext == "rtf") //如果后缀是 rtf 加载文件进来 { richTextBox1.LoadFile(title, RichTextBoxStreamType.RichText); } else { richTextBox1.Text = sr.ReadToEnd(); } fs.Close(); sr.Close(); } }
- 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
点击打开文件然后读取文件成功后的截图为:
打开读取文件测试成功了,接下来看一看修改文件然后实现保存功能:
private void toolStripButton3_Click(object sender, EventArgs e) { if (title == "Untitled")//如果是不是打开文件然后编辑的话 点击保存就相当于另存为咯 另存为AToolStripMenuItem_Click(sender, e);//这个另存为的按钮是在顶部的菜单栏 else { string ext = title.Substring(title.LastIndexOf(".") + 1); ext = ext.ToLower(); if (ext == "rtf")//按照不同的格式保存文件 richTextBox1.SaveFile(title, RichTextBoxStreamType.RichText); else if (ext == "doc" || ext == "txt") richTextBox1.SaveFile(title, RichTextBoxStreamType.PlainText); else if (ext == "uni") richTextBox1.SaveFile(title, RichTextBoxStreamType.UnicodePlainText); else richTextBox1.SaveFile(title, RichTextBoxStreamType.PlainText); richTextBox1.Modified = false; } } private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e) { /** * saveFileDialog1 同样也是设计窗体界面拖出来的控件 */ if (saveFileDialog1.ShowDialog() == DialogResult.OK) { title = saveFileDialog1.FileName; this.Text = title; switch (saveFileDialog1.FilterIndex) { case 1: richTextBox1.SaveFile(title, RichTextBoxStreamType.RichText); break; case 2: richTextBox1.SaveFile(title, RichTextBoxStreamType.PlainText); break; case 3: richTextBox1.SaveFile(title, RichTextBoxStreamType.UnicodePlainText); break; default: richTextBox1.SaveFile(title, RichTextBoxStreamType.PlainText); break; } richTextBox1.Modified = false; } }
- 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
这样就可以修改文本内容啦,做一下简单的测试:
打开文件并输入修改的内容:
这时候点击保存按钮,结果为:
可见修改成功了!
接下来就是对字体的加粗,斜体以及下划线了,(七)C#之WinForm程序设计-简单版文本编辑器 这篇博文有详细的过程啦,这里我们就给出代码吧:
private void boldToolStripButton_Click(object sender, EventArgs e) {//加粗 richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, richTextBox1.SelectionFont.Style ^ FontStyle.Bold); } private void toolStripButton7_Click(object sender, EventArgs e) {//倾斜 richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, richTextBox1.SelectionFont.Style ^ FontStyle.Italic); } private void toolStripButton8_Click(object sender, EventArgs e) {//加下划线 richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, richTextBox1.SelectionFont.Style ^ FontStyle.Underline); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
在加下划线之后就是剪切,赋值和粘贴了。这三个功能也是很简单的,只要调用RichTextBox的Cut(), Copy()和Paste()方法即可,代码如下:
private void cutToolStripButton_Click(object sender, EventArgs e) { richTextBox1.Cut();//剪切 } private void toolStripButton10_Click(object sender, EventArgs e) { richTextBox1.Copy();//复制 } private void toolStripButton11_Click(object sender, EventArgs e) { richTextBox1.Paste();//粘贴 }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
然后就是插入图片,监听其点击事件,代码如下:
private void toolStripButton12_Click(object sender, EventArgs e) { Bitmap bmp; if (openImageDialog.ShowDialog() == DialogResult.OK) { string filename = openImageDialog.FileName; try { bmp = new Bitmap(filename);//文件转化为 Bitmap Clipboard.SetDataObject(bmp); DataFormats.Format dft = DataFormats.GetFormat(DataFormats.Bitmap); if (this.richTextBox1.CanPaste(dft)) richTextBox1.Paste(dft); //图片加入到富文本中去 } catch (Exception ex) { MessageBox.Show("图片插入失败" + ex.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
测试结果如下:
插入图片成功,接下来就是改变字体颜色了,只要拖一个ColorDialog基本就OK了,代码如下:
private void toolStripButton15_Click(object sender, EventArgs e) { /** * colorDialog1 是设计界面拖出来的控件 */ if (colorDialog1.ShowDialog() == DialogResult.OK) richTextBox1.SelectionColor = colorDialog1.Color;//直接设置选中的字段的颜色 }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
接下来的撤销与重做功能也是很简单的实现,RichTextBox控件都有封装方法:
private void toolStripButton19_Click(object sender, EventArgs e) { richTextBox1.Undo();//撤销 } private void toolStripButton20_Click(object sender, EventArgs e) { richTextBox1.Redo();//重做 }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
接着就是左对齐,居中对齐,右对齐,在上一个简单版本的文本编辑器都有讲过((七)C#之WinForm程序设计-简单版文本编辑器)。直接给出代码:
private void toolStripButton21_Click(object sender, EventArgs e) { this.richTextBox1.SelectionAlignment = HorizontalAlignment.Left; } private void toolStripButton22_Click(object sender, EventArgs e) { this.richTextBox1.SelectionAlignment = HorizontalAlignment.Center; } private void toolStripButton23_Click(object sender, EventArgs e) { this.richTextBox1.SelectionAlignment = HorizontalAlignment.Right; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
然后的 大 小 按钮就是用来调整字体大小的,方法也很简单,代码如下:
private void toolStripButton25_Click(object sender, EventArgs e) { ZoomInOut(false);//增加 } private void toolStripButton26_Click(object sender, EventArgs e) { ZoomInOut(true);//减小 } private void ZoomInOut(bool IsZoomOut) { float zoom = 0; zoom = this.richTextBox1.ZoomFactor; if (IsZoomOut) zoom -= (float)0.1; else zoom += (float)0.1; if (zoom < 0.64 || zoom > 64) return; this.richTextBox1.ZoomFactor = zoom; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
看一下效果图:
最后两个就是设置 FontFamily 和 FontSize了,代码如下:
private void cbFamily_SelectedIndexChanged(object sender, EventArgs e) {//Font Family if (this.cbFamily.SelectedItem == null) return; string ss = this.cbFamily.SelectedItem.ToString().Trim(); richTextBox1.SelectionFont = new Font(ss, richTextBox1.SelectionFont.Size, richTextBox1.SelectionFont.Style); } private void cbSize_SelectedIndexChanged(object sender, EventArgs e) {//设置字体大小 if (cbSize.SelectedItem == null) return; int size = Convert.ToInt32(this.cbSize.SelectedItem.ToString().Trim()); richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont.FontFamily, size, richTextBox1.SelectionFont.Style); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
测试效果如下:
以上就是这个升级版本的文本编辑器的全部内容了
源码地址【升级版文本编辑器
给我老师的人工智能教程打call!http://blog.****.net/jiangjunshow
C#WinForm程序设计之升级版文本编辑器
前面的那篇博文讲解了一个简单版本的文本编辑器,实现了一些最简单的功能。那么这里我们对那个文本编辑器做一个升级吧!既然是升级版,那么就要做的像Word一点咯!首先我们来看一下我设计的界面吧!
文件(F):
编辑(E):
格式(O):
怎么样,这个界面是不是有点像Word了?图中的小图标下载地址为【小图标】。如果你有兴趣跟我一起做下去,那也把这个界面设计出来吧(顶部的有下拉功能的菜单我们先不管,功能跟中间部分的ToolStrip功能基本是一样的),然后继续看博客吧!
我们从左到右实现中间的ToolStrip栏的功能!
首先我们实现中间ToolStrip栏的第一个按钮(Name为openFileToolStripButton)的功能:打开读取文件内容并把文本内容填充到下面的RichTextBox里面吧!监听其单击事件,代码如下:
定义两个全局变量
private String title = "Untitled"; //保存打开的文件的标题 Encoding ec=Encoding.UTF8; //设置文本的格式为 UTF-8
- 1
- 2
监听点击事件:
private void openFileToolStripButton_Click(object sender, EventArgs e) { /** * openFileDialog1 是在设计界面拖出来的控件 OpenFileDialog * * 主要是打开 rtf 格式的文件 */ openFileDialog1.Filter = "文本文件|*.txt;*.html;*.docx;*.doc;*.rtf|所有文件|*.*"; //文件打开的过滤器 if (openFileDialog1.ShowDialog() == DialogResult.OK) { title = openFileDialog1.FileName; this.Text = title; //显示打开的文件名 richTextBox1.Modified = false; string ext = title.Substring(title.LastIndexOf(".") + 1);//获取文件格式 ext = ext.ToLower(); FileStream fs = new FileStream(title, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs, ec); if (ext == "rtf") //如果后缀是 rtf 加载文件进来 { richTextBox1.LoadFile(title, RichTextBoxStreamType.RichText); } else { richTextBox1.Text = sr.ReadToEnd(); } fs.Close(); sr.Close(); } }
- 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
点击打开文件然后读取文件成功后的截图为:
打开读取文件测试成功了,接下来看一看修改文件然后实现保存功能:
private void toolStripButton3_Click(object sender, EventArgs e) { if (title == "Untitled")//如果是不是打开文件然后编辑的话 点击保存就相当于另存为咯 另存为AToolStripMenuItem_Click(sender, e);//这个另存为的按钮是在顶部的菜单栏 else { string ext = title.Substring(title.LastIndexOf(".") + 1); ext = ext.ToLower(); if (ext == "rtf")//按照不同的格式保存文件 richTextBox1.SaveFile(title, RichTextBoxStreamType.RichText); else if (ext == "doc" || ext == "txt") richTextBox1.SaveFile(title, RichTextBoxStreamType.PlainText); else if (ext == "uni") richTextBox1.SaveFile(title, RichTextBoxStreamType.UnicodePlainText); else richTextBox1.SaveFile(title, RichTextBoxStreamType.PlainText); richTextBox1.Modified = false; } } private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e) { /** * saveFileDialog1 同样也是设计窗体界面拖出来的控件 */ if (saveFileDialog1.ShowDialog() == DialogResult.OK) { title = saveFileDialog1.FileName; this.Text = title; switch (saveFileDialog1.FilterIndex) { case 1: richTextBox1.SaveFile(title, RichTextBoxStreamType.RichText); break; case 2: richTextBox1.SaveFile(title, RichTextBoxStreamType.PlainText); break; case 3: richTextBox1.SaveFile(title, RichTextBoxStreamType.UnicodePlainText); break; default: richTextBox1.SaveFile(title, RichTextBoxStreamType.PlainText); break; } richTextBox1.Modified = false; } }
- 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
这样就可以修改文本内容啦,做一下简单的测试:
打开文件并输入修改的内容:
这时候点击保存按钮,结果为:
可见修改成功了!
接下来就是对字体的加粗,斜体以及下划线了,(七)C#之WinForm程序设计-简单版文本编辑器 这篇博文有详细的过程啦,这里我们就给出代码吧:
private void boldToolStripButton_Click(object sender, EventArgs e) {//加粗 richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, richTextBox1.SelectionFont.Style ^ FontStyle.Bold); } private void toolStripButton7_Click(object sender, EventArgs e) {//倾斜 richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, richTextBox1.SelectionFont.Style ^ FontStyle.Italic); } private void toolStripButton8_Click(object sender, EventArgs e) {//加下划线 richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, richTextBox1.SelectionFont.Style ^ FontStyle.Underline); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
在加下划线之后就是剪切,赋值和粘贴了。这三个功能也是很简单的,只要调用RichTextBox的Cut(), Copy()和Paste()方法即可,代码如下:
private void cutToolStripButton_Click(object sender, EventArgs e) { richTextBox1.Cut();//剪切 } private void toolStripButton10_Click(object sender, EventArgs e) { richTextBox1.Copy();//复制 } private void toolStripButton11_Click(object sender, EventArgs e) { richTextBox1.Paste();//粘贴 }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
然后就是插入图片,监听其点击事件,代码如下:
private void toolStripButton12_Click(object sender, EventArgs e) { Bitmap bmp; if (openImageDialog.ShowDialog() == DialogResult.OK) { string filename = openImageDialog.FileName; try { bmp = new Bitmap(filename);//文件转化为 Bitmap Clipboard.SetDataObject(bmp); DataFormats.Format dft = DataFormats.GetFormat(DataFormats.Bitmap); if (this.richTextBox1.CanPaste(dft)) richTextBox1.Paste(dft); //图片加入到富文本中去 } catch (Exception ex) { MessageBox.Show("图片插入失败" + ex.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
测试结果如下:
插入图片成功,接下来就是改变字体颜色了,只要拖一个ColorDialog基本就OK了,代码如下:
private void toolStripButton15_Click(object sender, EventArgs e) { /** * colorDialog1 是设计界面拖出来的控件 */ if (colorDialog1.ShowDialog() == DialogResult.OK) richTextBox1.SelectionColor = colorDialog1.Color;//直接设置选中的字段的颜色 }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
接下来的撤销与重做功能也是很简单的实现,RichTextBox控件都有封装方法:
private void toolStripButton19_Click(object sender, EventArgs e) { richTextBox1.Undo();//撤销 } private void toolStripButton20_Click(object sender, EventArgs e) { richTextBox1.Redo();//重做 }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
接着就是左对齐,居中对齐,右对齐,在上一个简单版本的文本编辑器都有讲过((七)C#之WinForm程序设计-简单版文本编辑器)。直接给出代码:
private void toolStripButton21_Click(object sender, EventArgs e) { this.richTextBox1.SelectionAlignment = HorizontalAlignment.Left; } private void toolStripButton22_Click(object sender, EventArgs e) { this.richTextBox1.SelectionAlignment = HorizontalAlignment.Center; } private void toolStripButton23_Click(object sender, EventArgs e) { this.richTextBox1.SelectionAlignment = HorizontalAlignment.Right; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
然后的 大 小 按钮就是用来调整字体大小的,方法也很简单,代码如下:
private void toolStripButton25_Click(object sender, EventArgs e) { ZoomInOut(false);//增加 } private void toolStripButton26_Click(object sender, EventArgs e) { ZoomInOut(true);//减小 } private void ZoomInOut(bool IsZoomOut) { float zoom = 0; zoom = this.richTextBox1.ZoomFactor; if (IsZoomOut) zoom -= (float)0.1; else zoom += (float)0.1; if (zoom < 0.64 || zoom > 64) return; this.richTextBox1.ZoomFactor = zoom; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
看一下效果图:
最后两个就是设置 FontFamily 和 FontSize了,代码如下:
private void cbFamily_SelectedIndexChanged(object sender, EventArgs e) {//Font Family if (this.cbFamily.SelectedItem == null) return; string ss = this.cbFamily.SelectedItem.ToString().Trim(); richTextBox1.SelectionFont = new Font(ss, richTextBox1.SelectionFont.Size, richTextBox1.SelectionFont.Style); } private void cbSize_SelectedIndexChanged(object sender, EventArgs e) {//设置字体大小 if (cbSize.SelectedItem == null) return; int size = Convert.ToInt32(this.cbSize.SelectedItem.ToString().Trim()); richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont.FontFamily, size, richTextBox1.SelectionFont.Style); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
测试效果如下:
以上就是这个升级版本的文本编辑器的全部内容了
源码地址【升级版文本编辑器