续上一节,主要代码如下:
Public Class Form3 Dim FontName As String Dim FontSize As Integer Private Sub Form3_Load(sender As Object,e As EventArgs) Handles MyBase.Load FontName = "宋体" FontSize = 10 ToolStripComboBox1.Text = ToolStripComboBox1.Items(0) ToolStripComboBox2.Text = "10" End Sub '撤销 Private Sub ToolStripButton11_Click(sender As Object,e As EventArgs) Handles ToolStripButton11.Click RichTextBox1.Undo() End Sub '重做 Private Sub ToolStripButton12_Click(sender As Object,e As EventArgs) Handles ToolStripButton12.Click RichTextBox1.Redo() End Sub '设置文字颜色 Private Sub ToolStripButton1_Click(sender As Object,e As EventArgs) Handles ToolStripButton1.Click If ColorDialog1.ShowDialog() = DialogResult.OK Then RichTextBox1.SelectionColor = ColorDialog1.Color End If End Sub '设置文字背景色 Private Sub ToolStripButton2_Click(sender As Object,e As EventArgs) Handles ToolStripButton2.Click If ColorDialog1.ShowDialog() = DialogResult.OK Then RichTextBox1.SelectionBackColor = ColorDialog1.Color End If End Sub '设置文字字体 Private Sub ToolStripComboBox1_TextChanged(sender As Object,e As EventArgs) Handles ToolStripComboBox1.SelectedIndexChanged FontName = ToolStripComboBox1.Text RichTextBox1.SelectionFont = New Font(FontName,FontSize) End Sub '设置文字大小 Private Sub ToolStripComboBox2_TextChanged(sender As Object,e As EventArgs) Handles ToolStripComboBox2.TextChanged FontSize = ToolStripComboBox2.Text RichTextBox1.SelectionFont = New Font(FontName,FontSize) End Sub '设置粗体 Private Sub ToolStripButton7_Click(sender As Object,e As EventArgs) Handles ToolStripButton7.Click Dim selFontName As String = RichTextBox1.SelectionFont.Name Dim selFontSize As Integer = RichTextBox1.SelectionFont.Size Dim selFontStyle As FontStyle = RichTextBox1.SelectionFont.Style Xor FontStyle.Bold RichTextBox1.SelectionFont = New Font(selFontName,selFontSize,selFontStyle) End Sub '设置斜体 Private Sub ToolStripButton8_Click(sender As Object,e As EventArgs) Handles ToolStripButton8.Click Dim selFontName As String = RichTextBox1.SelectionFont.Name Dim selFontSize As Integer = RichTextBox1.SelectionFont.Size Dim selFontStyle As FontStyle = RichTextBox1.SelectionFont.Style Xor FontStyle.Italic RichTextBox1.SelectionFont = New Font(selFontName,selFontStyle) End Sub '设置下划线 Private Sub ToolStripButton9_Click(sender As Object,e As EventArgs) Handles ToolStripButton9.Click Dim selFontName As String = RichTextBox1.SelectionFont.Name Dim selFontSize As Integer = RichTextBox1.SelectionFont.Size Dim selFontStyle As FontStyle = RichTextBox1.SelectionFont.Style Xor FontStyle.Underline RichTextBox1.SelectionFont = New Font(selFontName,selFontStyle) End Sub '左对齐 Private Sub ToolStripButton4_Click(sender As Object,e As EventArgs) Handles ToolStripButton4.Click RichTextBox1.SelectionAlignment = HorizontalAlignment.Left End Sub '居中对齐 Private Sub ToolStripButton5_Click(sender As Object,e As EventArgs) Handles ToolStripButton5.Click RichTextBox1.SelectionAlignment = HorizontalAlignment.Center End Sub '右对齐 Private Sub ToolStripButton3_Click(sender As Object,e As EventArgs) Handles ToolStripButton3.Click RichTextBox1.SelectionAlignment = HorizontalAlignment.Right End Sub '设置富文本框背景色 Private Sub ToolStripButton6_Click(sender As Object,e As EventArgs) Handles ToolStripButton6.Click If ColorDialog1.ShowDialog() = DialogResult.OK Then RichTextBox1.BackColor = ColorDialog1.Color End If End Sub '插入图片 Private Sub ToolStripButton10_Click(sender As Object,e As EventArgs) Handles ToolStripButton10.Click Dim imgFile As String If OpenFileDialog2.ShowDialog = DialogResult.OK Then imgFile = OpenFileDialog2.FileName Else Exit Sub End If Dim bmp As Bitmap Try bmp = Bitmap.FromFile(imgFile) Clipboard.SetDataObject(bmp) Dim MyFormat As DataFormats.Format = DataFormats.GetFormat(DataFormats.Bitmap) If RichTextBox1.CanPaste(MyFormat) Then RichTextBox1.Paste(MyFormat) End If Catch ex As Exception MessageBox.Show("插入图片时发生错误") End Try End Sub '新建文件 Private Sub 新建文件NToolStripMenuItem_Click(sender As Object,e As EventArgs) Handles 新建文件NToolStripMenuItem.Click RichTextBox1.Text = "" End Sub '载入文件 Private Sub 载入文件LToolStripMenuItem_Click(sender As Object,e As EventArgs) Handles 载入文件LToolStripMenuItem.Click If OpenFileDialog1.ShowDialog = DialogResult.OK Then RichTextBox1.LoadFile(OpenFileDialog1.FileName) End If End Sub '保存文件 Private Sub 保存文件SToolStripMenuItem_Click(sender As Object,e As EventArgs) Handles 保存文件SToolStripMenuItem.Click If SaveFileDialog1.ShowDialog = DialogResult.OK Then RichTextBox1.SaveFile(SaveFileDialog1.FileName) End If End Sub '剪切 Private Sub 剪切ToolStripMenuItem_Click(sender As Object,e As EventArgs) Handles 剪切ToolStripMenuItem.Click Clipboard.SetData(DataFormats.Rtf,RichTextBox1.SelectedRtf) RichTextBox1.SelectedText = "" End Sub '复制 Private Sub 复制CToolStripMenuItem_Click(sender As Object,e As EventArgs) Handles 复制CToolStripMenuItem.Click Clipboard.SetData(DataFormats.Rtf,RichTextBox1.SelectedRtf) End Sub '粘贴 Private Sub 粘贴PToolStripMenuItem_Click(sender As Object,e As EventArgs) Handles 粘贴PToolStripMenuItem.Click RichTextBox1.SelectedRtf = Clipboard.GetData(DataFormats.Rtf) End Sub '关于 Private Sub 关于AToolStripMenuItem_Click(sender As Object,e As EventArgs) Handles 关于AToolStripMenuItem.Click MessageBox.Show("这是一个富文件编辑器,可以编辑RTF文档" & ControlChars.CrLf & "来自: http://blog.csdn.net/uruseibest","编辑器",MessageBoxButtons.OK) End Sub '退出 Private Sub 退出XToolStripMenuItem_Click(sender As Object,e As EventArgs) Handles 退出XToolStripMenuItem.Click Application.Exit() End Sub '鼠标按下时判断所在行数,修改到状态栏 Private Sub RichTextBox1_MouseDown(sender As Object,e As MouseEventArgs) Handles RichTextBox1.MouseDown Dim allLines As Integer Dim curLines As Integer allLines = RichTextBox1.GetLineFromCharIndex(RichTextBox1.TextLength) + 1 curLines = RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart) + 1 ToolStripStatusLabel1.Text = "总 " & allLines & "行 " & "第 " & curLines & "行 " End Sub End Class