想必大家都聽說過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 Subtxt: 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, 1, Me.Text.Length - 18), RichTextBoxStreamType.RichText) Case Else RichTextBox1.SaveFile(Mid(Me.Text, 1, 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 SubEnd Class
基本上沒用什麼屬性設定,都是靠代碼實現的,由於功能十分簡單,使用的是面向過程的方法實現,看起來簡單點。
微軟的記事本其實很簡單,給大家開闊下思路。
需要完整源碼的可以到這裡下載:http://download.csdn.net/source/3412684
歡迎轉載:http://blog.csdn.com/AngelHacker/