想必大家都听说过Notepad2,Notepad++一类的记事本增强软件,想自己也写一个吗?
今天教大家用用VB.NET写了一个功能增强的记事本(支持全文字数统计、选中部分字数统计、支持doc、rtf格式等)
先上图:
感觉如果不错的话,看看源码吧:
Public Class MainForm Dim FileName As String = "无标题" Dim word As String = "" Dim ML As Boolean = False Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load Me.StartPosition = FormStartPosition.CenterScreen Me.Text = FileName + " - " + "记事本——AngelHacker" RichTextBox1.WordWrap = False RichTextBox1.ContextMenuStrip = ContextMenuStrip1 RichTextBox1.ScrollBars = RichTextBoxScrollBars.Both 新建NToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.N 打开OToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.O 保存SToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.S 剪切TToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.X 复制CToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.C 粘贴PToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.V 全选AToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.A 删除LToolStripMenuItem.ShortcutKeys = Keys.Delete 查找FToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.F 查找下一个NToolStripMenuItem.ShortcutKeys = Keys.F3 查找下一个NToolStripMenuItem.Enabled = False End Sub Private Sub 新建NToolStripMenuItem_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles 新建NToolStripMenuItem.Click RichTextBox1.Text = "" FileName = "无标题" Me.Text = FileName + " - " + "记事本——AngelHacker" End Sub Private Sub 打开OToolStripMenuItem_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles 打开OToolStripMenuItem.Click OpenFileDialog1.Filter = "文本文档(*.txt)|*.txt|RTF文档(*.rtf)|*.rtf|所有文件(*.*)|*.*" OpenFileDialog1.FileName = "" If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.Cancel Then Exit Sub Else RichTextBox1.Text = "" FileName = OpenFileDialog1.FileName Select Case OpenFileDialog1.FilterIndex Case "1" RichTextBox1.LoadFile(FileName,RichTextBoxStreamType.PlainText) Case "2" RichTextBox1.LoadFile(FileName,RichTextBoxStreamType.RichText) Case Else On Error GoTo txt RichTextBox1.LoadFile(FileName) End Select Me.Text = FileName + " - " + "记事本——AngelHacker" End If Exit Sub txt: RichTextBox1.LoadFile(FileName,RichTextBoxStreamType.PlainText) End Sub Private Sub 保存SToolStripMenuItem_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles 保存SToolStripMenuItem.Click If Me.Text = "无标题 - 记事本——AngelHacker" Then SaveFileDialog1.Filter = "文本文档(*.txt)|*.txt|RTF文档(*.rtf)|*.rtf|所有文件(*.*)|*.*" SaveFileDialog1.FileName = "" SaveFileDialog1.CheckPathExists = True If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.Cancel Then Exit Sub Else Select Case SaveFileDialog1.FilterIndex Case "1" RichTextBox1.SaveFile(SaveFileDialog1.FileName,RichTextBoxStreamType.PlainText) Case "2" RichTextBox1.SaveFile(SaveFileDialog1.FileName,RichTextBoxStreamType.RichText) Case Else RichTextBox1.SaveFile(SaveFileDialog1.FileName) End Select Me.Text = SaveFileDialog1.FileName + " - " + "记事本——AngelHacker" End If Else Select Case Mid(Me.Text,Me.Text.Length - 21,3) Case "txt" RichTextBox1.SaveFile(Mid(Me.Text,1,Me.Text.Length - 18),RichTextBoxStreamType.PlainText) Case "rtf" RichTextBox1.SaveFile(Mid(Me.Text,RichTextBoxStreamType.RichText) Case Else RichTextBox1.SaveFile(Mid(Me.Text,Me.Text.Length - 18)) End Select End If End Sub Private Sub 另存为AToolStripMenuItem_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles 另存为AToolStripMenuItem.Click SaveFileDialog1.Filter = "文本文档(*.txt)|*.txt|RTF文档(*.rtf)|*.rtf|所有文件(*.*)|*.*" SaveFileDialog1.FileName = "" SaveFileDialog1.CheckPathExists = True If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.Cancel Then Exit Sub Else Select Case SaveFileDialog1.FilterIndex Case "1" RichTextBox1.SaveFile(SaveFileDialog1.FileName,RichTextBoxStreamType.PlainText) Case "2" RichTextBox1.SaveFile(SaveFileDialog1.FileName,RichTextBoxStreamType.RichText) Case Else RichTextBox1.SaveFile(SaveFileDialog1.FileName) End Select Me.Text = SaveFileDialog1.FileName + " - " + "记事本——AngelHacker" End If End Sub Private Sub 退出XToolStripMenuItem_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles 退出XToolStripMenuItem.Click End End Sub Private Sub 重置UToolStripMenuItem_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles 重置UToolStripMenuItem.Click,重置UToolStripMenuItem1.Click RichTextBox1.Text = "" RichTextBox1.SelectionStart = 1 word = "" 查找下一个NToolStripMenuItem.Enabled = False RichTextBox1.Font = Font End Sub Private Sub 剪切TToolStripMenuItem_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles 剪切TToolStripMenuItem.Click,剪切TToolStripMenuItem1.Click RichTextBox1.Cut() End Sub Private Sub 复制CToolStripMenuItem_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles 复制CToolStripMenuItem.Click,复制CToolStripMenuItem1.Click RichTextBox1.Copy() End Sub Private Sub 粘贴PToolStripMenuItem_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles 粘贴PToolStripMenuItem.Click,粘贴PToolStripMenuItem1.Click RichTextBox1.Paste() End Sub Private Sub 全选AToolStripMenuItem_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles 全选AToolStripMenuItem.Click,全选AToolStripMenuItem1.Click RichTextBox1.SelectAll() End Sub Private Sub 删除LToolStripMenuItem_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles 删除LToolStripMenuItem.Click,删除DToolStripMenuItem.Click RichTextBox1.Text = RichTextBox1.Text.Remove(RichTextBox1.SelectionStart,RichTextBox1.SelectionLength) End Sub Private Sub 字数统计ToolStripMenuItem_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles 字数统计ToolStripMenuItem.Click,字数统计SToolStripMenuItem.Click If RichTextBox1.SelectedText = "" Then MsgBox("全文共" + RichTextBox1.TextLength.ToString + "字符") Else MsgBox("选中的字符数为" + RichTextBox1.SelectionLength.ToString) End If End Sub Private Sub 自动换行WToolStripMenuItem_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles 自动换行WToolStripMenuItem.Click If ML = False Then RichTextBox1.WordWrap = True 自动换行WToolStripMenuItem.Checked = True ML = True Else RichTextBox1.WordWrap = False 自动换行WToolStripMenuItem.Checked = False ML = False End If End Sub Private Sub 关于AToolStripMenuItem_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles 关于AToolStripMenuItem.Click AboutBox1.ShowDialog() End Sub Private Sub 字体FToolStripMenuItem_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles 字体FToolStripMenuItem.Click If FontDialog1.ShowDialog() = Windows.Forms.DialogResult.Cancel Then Exit Sub Else RichTextBox1.Font = FontDialog1.Font End If End Sub Private Sub 查找FToolStripMenuItem_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles 查找FToolStripMenuItem.Click word = InputBox("查找内容","查找") If word = "" Then Exit Sub Else Dim a As Integer = RichTextBox1.Find(word,RichTextBox1.SelectionStart,RichTextBoxFinds.MatchCase) If a = -1 Then MsgBox("找不到" + """" + word + """") Else 查找下一个NToolStripMenuItem.Enabled = True Exit Sub End If End If End Sub Private Sub 查找下一个NToolStripMenuItem_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles 查找下一个NToolStripMenuItem.Click Dim wz As Integer If RichTextBox1.SelectionStart = RichTextBox1.TextLength Then MsgBox("找不到" + """" + word + """") Else wz = RichTextBox1.SelectionStart wz += 1 Dim a As Integer = RichTextBox1.Find(word,wz,RichTextBoxFinds.MatchCase) If a = -1 Then MsgBox("找不到" + """" + word + """") Else Exit Sub End If End If End Sub Private Sub 时间日期DToolStripMenuItem_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles 时间日期DToolStripMenuItem.Click If ML = False Then RichTextBox1.Text += Date.Now Else RichTextBox1.Text += Chr(13) & Chr(10) & Date.Now End If End Sub End Class
基本上没用什么属性设置,都是靠代码实现的,由于功能十分简单,使用的是面向过程的方法实现,看起来简单点。
微软的记事本其实很简单,给大家开阔下思路。
需要完整源码的可以到这里下载:http://download.csdn.net/source/3412684
原文链接:https://www.f2er.com/vb/261142.html