想必大家都听说过Notepad2,Notepad++一类的记事本增强软件,想自己也写一个吗?
今天教大家用用VB.NET写了一个功能增强的记事本(支持全文字数统计、选中部分字数统计、支持doc、rtf格式等)
先上图:
感觉如果不错的话,看看源码吧:
- PublicClassMainForm
- DimFileNameAsString="无标题"
- DimwordAsString=""
- DimMLAsBoolean=False
- PrivateSubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.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
- EndSub
- PrivateSub新建NToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles新建NToolStripMenuItem.Click
- RichTextBox1.Text=""
- FileName="无标题"
- PrivateSub打开OToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles打开OToolStripMenuItem.Click
- OpenFileDialog1.Filter="文本文档(*.txt)|*.txt|RTF文档(*.rtf)|*.rtf|所有文件(*.*)|*.*"
- OpenFileDialog1.FileName=""
- IfOpenFileDialog1.ShowDialog()=Windows.Forms.DialogResult.CancelThen
- ExitSub
- Else
- RichTextBox1.Text=""
- FileName=OpenFileDialog1.FileName
- SelectCaseOpenFileDialog1.FilterIndex
- Case"1"
- RichTextBox1.LoadFile(FileName,RichTextBoxStreamType.PlainText)
- Case"2"
- BoxStreamType.RichText)
- CaseElse
- OnErrorGoTotxt
- RichTextBox1.LoadFile(FileName)
- EndSelect
- EndIf
- ExitSub
- txt:
- RichTextBox1.LoadFile(FileName,RichTextBoxStreamType.PlainText)
- PrivateSub保存SToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles保存SToolStripMenuItem.Click
- IfMe.Text="无标题-记事本——AngelHacker"Then
- SaveFileDialog1.Filter="文本文档(*.txt)|*.txt|RTF文档(*.rtf)|*.rtf|所有文件(*.*)|*.*"
- SaveFileDialog1.FileName=""
- SaveFileDialog1.CheckPathExists=True
- IfSaveFileDialog1.ShowDialog()=Windows.Forms.DialogResult.CancelThen
- SelectCaseSaveFileDialog1.FilterIndex
- RichTextBox1.SaveFile(SaveFileDialog1.FileName,108) 3px solid; padding-bottom:0px!important; line-height:18px; border-right-style:none; list-style-type:decimal-leading-zero; margin:0px; padding-left:10px!important; padding-right:3px!important; border-top-style:none; padding-top:0px!important"> RichTextBox1.SaveFile(SaveFileDialog1.FileName)
- EndSelect
- Me.Text=SaveFileDialog1.FileName+"-"+"记事本——AngelHacker"
- EndIf
- Else
- SelectCaseMid(Me.Text,Me.Text.Length-21,3)
- Case"txt"
- RichTextBox1.SaveFile(Mid(Me.Text,1,Me.Text.Length-18),RichTextBoxStreamType.PlainText)
- Case"rtf"
- BoxStreamType.RichText)
- CaseElse
- Me.Text.Length-18))
- PrivateSub另存为AToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles另存为AToolStripMenuItem.Click
- SaveFileDialog1.Filter="文本文档(*.txt)|*.txt|RTF文档(*.rtf)|*.rtf|所有文件(*.*)|*.*"
- SaveFileDialog1.FileName=""
- SaveFileDialog1.CheckPathExists=True
- IfSaveFileDialog1.ShowDialog()=Windows.Forms.DialogResult.CancelThen
- SelectCaseSaveFileDialog1.FilterIndex
- Case"1"
- RichTextBox1.SaveFile(SaveFileDialog1.FileName,108) 3px solid; padding-bottom:0px!important; line-height:18px; border-right-style:none; list-style-type:decimal-leading-zero; margin:0px; padding-left:10px!important; padding-right:3px!important; border-top-style:none; padding-top:0px!important"> Case"2"
- BoxStreamType.RichText)
- RichTextBox1.SaveFile(SaveFileDialog1.FileName)
- Me.Text=SaveFileDialog1.FileName+"-"+"记事本——AngelHacker"
- EndSub
- PrivateSub退出XToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles退出XToolStripMenuItem.Click
- End
- PrivateSub重置UToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles重置UToolStripMenuItem.Click,重置UToolStripMenuItem1.Click
- RichTextBox1.SelectionStart=1
- word=""
- RichTextBox1.Font=Font
- PrivateSub剪切TToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles剪切TToolStripMenuItem.Click,剪切TToolStripMenuItem1.Click
- RichTextBox1.Cut()
- PrivateSub复制CToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles复制CToolStripMenuItem.Click,复制CToolStripMenuItem1.Click
- RichTextBox1.Copy()
- PrivateSub粘贴PToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles粘贴PToolStripMenuItem.Click,粘贴PToolStripMenuItem1.Click
- RichTextBox1.Paste()
- PrivateSub全选AToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles全选AToolStripMenuItem.Click,全选AToolStripMenuItem1.Click
- RichTextBox1.SelectAll()
- PrivateSub删除LToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles删除LToolStripMenuItem.Click,删除DToolStripMenuItem.Click
- RichTextBox1.Text=RichTextBox1.Text.Remove(RichTextBox1.SelectionStart,RichTextBox1.SelectionLength)
- PrivateSub字数统计ToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles字数统计ToolStripMenuItem.Click,字数统计SToolStripMenuItem.Click
- IfRichTextBox1.SelectedText=""Then
- MsgBox("全文共"+RichTextBox1.TextLength.ToString+"字符")
- MsgBox("选中的字符数为"+RichTextBox1.SelectionLength.ToString)
- PrivateSub自动换行WToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles自动换行WToolStripMenuItem.Click
- IfML=FalseThen
- RichTextBox1.WordWrap=True
- 自动换行WToolStripMenuItem.Checked=True
- ML=True
- RichTextBox1.WordWrap=False
- 自动换行WToolStripMenuItem.Checked=False
- ML=False
- PrivateSub关于AToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles关于AToolStripMenuItem.Click
- AboutBox1.ShowDialog()
- PrivateSub字体FToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles字体FToolStripMenuItem.Click
- IfFontDialog1.ShowDialog()=Windows.Forms.DialogResult.CancelThen
- RichTextBox1.Font=FontDialog1.Font
- PrivateSub查找FToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles查找FToolStripMenuItem.Click
- word=InputBox("查找内容","查找")
- Ifword=""Then
- DimaAsInteger=RichTextBox1.Find(word,RichTextBox1.SelectionStart,RichTextBoxFinds.MatchCase)
- Ifa=-1Then
- MsgBox("找不到"+""""+word+"""")
- 查找下一个NToolStripMenuItem.Enabled=True
- PrivateSub查找下一个NToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles查找下一个NToolStripMenuItem.Click
- DimwzAsInteger
- IfRichTextBox1.SelectionStart=RichTextBox1.TextLengthThen
- MsgBox("找不到"+""""+word+"""")
- wz=RichTextBox1.SelectionStart
- wz+=1
- DimaAsInteger=RichTextBox1.Find(word,wz,RichTextBoxFinds.MatchCase)
- Ifa=-1Then
- PrivateSub时间日期DToolStripMenuItem_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handles时间日期DToolStripMenuItem.Click
- IfML=FalseThen
- RichTextBox1.Text+=Date.Now
- RichTextBox1.Text+=Chr(13)&Chr(10)&Date.Now
- EndClass
基本上没用什么属性设置,都是靠代码实现的,由于功能十分简单,使用的是面向过程的方法实现,看起来简单点。
微软的记事本其实很简单,给大家开阔下思路。
需要完整源码的可以到这里下载:http://download.csdn.net/source/3412684