用VB編寫的記事本源碼教程
精選優(yōu)質(zhì)文檔-傾情為你奉上一實驗要求用所學的VB語言編寫一個自己的記事本,其功能與計算機中記事本的功能基本一致,程序結構清晰。二實驗目的通過編寫記事本,熟悉并掌握所學VB的程序語言,鍛煉編程思想,從而觸類旁通發(fā)揮自己的創(chuàng)造力編輯自己的作品。三設計軟件所實現(xiàn)的功能窗體界面包括:文件、編輯、格式、查看、幫助五個主菜單。每個主菜單都有自己的子菜單,其主菜單的功能如下:1. 文件:包括新建、打開、保存、另存為、頁面設置、打印、退出的功能。2. 編輯:包括撤銷、剪切、復制、粘貼、刪除、查找、查找下一個、替換、轉(zhuǎn)到、全選、時間/日期的功能。3. 格式:包括自動換行、字體的功能。4. 查看:包括狀態(tài)欄的功能5. 幫助:包括幫助主題,關于記事本。四設計介紹.1.繪制界面新建一個標準的.exe工程,將其caption屬性改為”無標題-記事本”,點擊icon屬性給它找一個合適的圖標,其編輯的界面圖如下:2.編輯菜單按ctrl+E調(diào)出菜單編輯器,來做如下幾個菜單:(1)文件菜單:文件(第一層)file、新建(第二層)new、打開(第二層)open、保存(第二層)save、另存為(第二層)lingsave、-(第二層)spar1(分隔線)、 頁面設置(第二層)design、打印(第二層)print、-(第二層)spar2(分隔線)、退出(第二層)exit(2)編輯菜單編輯(第一層)edit、撤銷(第二層)cancel、-(第二層)spar3、剪切(第二層)qie、復制(第二層)copy、粘貼(第二層)tie、刪除(第二層)delete、-(第二層)spar4、查找(第二層)find、查找下一個(第二層)findnext、替換(第二層)instead、轉(zhuǎn)到(第二層)zhuan、-(第二層)spar5、全選(第二層)selectall、時間/日期(第二層)date(3)格式菜單格式(第一層)style、自動換行(第二層)autozhuan、字體(第二層)font(4)查看菜單查看(第一層)cha、狀態(tài)欄(第二層)state(5)幫助菜單幫助(第一層)help、幫助主題(第二層)topic、關于記事本(第二層)about3.編輯代碼(1)新建Private Sub new_Click()Dim m As New Form1If d Then If Form1.Caption = "無標題-記事本" Then yy = MsgBox("文件 無標題-記事本 文字已經(jīng)改變," + Chr(13) + Chr(10) + "是否保存文件?", 51, "記事本") Else yy = MsgBox("文件 " + rr + " 文字已經(jīng)改變," + Chr(13) + Chr(10) + "是否保存文件?", 51, "記事本") End IfEnd IfIf yy = 6 Then Call lingsave_ClickElseIf yy = 2 Then Exit Sub End IfForm1.Hidem.ShowEnd Sub(2)打開Private Sub open_Click()cd.InitDir = "D:"cd.CancelError = Truecd.Filter = "全部文擋(*.*)|*.*|文本文擋(*.txt)|*.txt"cd.FilterIndex = 2If d <> True Then start: On Error GoTo ee cd.ShowOpen Open cd.FileName For Input As #1 rr = cd.FileName Do While Not EOF(1) Line Input #1, a$ whole$ = whole$ + a$ + Chr(13) + Chr(10) Loop Text1.Text = whole$ Close #1 ss = Split(cd.FileTitle, ".") dd = ss(0) Form1.Caption = dd + "-記事本" Else If Form1.Caption = "無標題-記事本" Then yy = MsgBox("文件 無標題-記事本 文字已經(jīng)改變,是否保存文件?", 51, "記事本") Else yy = MsgBox("文件 " + rr + " 文字已經(jīng)改變,是否保存文件?", 51, "記事本") End If If yy = 6 Then Call lingsave_Click GoTo start ElseIf yy = 7 Then GoTo start Else Exit Sub End IfEnd Ifee: Exit SubEnd Sub(3)保存Private Sub save_Click() If Form1.Caption = "無標題-記事本" Then Call lingsave_Click d = False Else cc = Split(Form1.Caption, "-") ff = cc(0) Open ff + ".txt" For Output As #1 Print #1, Text1.Text Close #1 d = False End IfEnd Sub(4)另存為Private Sub lingsave_Click()cd.InitDir = "D:"cd.CancelError = TrueOn Error GoTo jjcd.Filter = "全部文擋(*.*)|*.*|文本文擋(*.txt)|*.txt"cd.FilterIndex = 2cd.ShowSaveOpen cd.FileName For Output As #2Print #2, Text1.TextClose #2d = False ss = Split(cd.FileTitle, ".") dd = ss(0) Form1.Caption = dd + "-記事本"jj: Exit SubEnd Sub(5)打印Private Sub print_Click()Dim firpage, endpage, numcd.CancelError = TrueOn Error GoTo jjcd.ShowPrinternum = cd.CopiesPrinter.font = "楷體"Printer.FontSize = 10Printer.PaperSize = vbPRPSA4For i = 1 To num Printer.CurrentX = 10 Printer.CurrentY = 10 Printer.Print Text1.TextNext iPrinter.EndDocExit Subjj:Exit SubEnd Sub(6)退出Private Sub exit_Click()EndEnd Sub(7)剪切Private Sub qie_Click() Clipboard.Clear Clipboard.SetText Text1.SelText Text1.SelText = ""End Sub(8)復制Private Sub copy_Click()Clipboard.ClearClipboard.SetText Text1.SelText End Sub(9)粘貼Private Sub tie_Click()Text1.SelText = Clipboard.GetTextEnd Sub(10)查找Private Sub find_Click()Form2.ShowEnd SubForm2中代碼:注:dd、ee都是全局變量Private Sub Command1_Click()If Check1.Value = 1 Thendd = InStr(Form1.Text1.Text, Form2.Text1.Text)Elsedd = InStr(LCase(Form1.Text1.Text), LCase(Form2.Text1.Text)End Ifee = Len(Form2.Text1.Text)Form1.Text1.SelStart = dd - 1Form1.Text1.SelLength = eekk = kk + 1Form1.SetFocusEnd SubPrivate Sub Command2_Click()Form2.HideEnd Sub(11)替換Private Sub instead_Click()Form3.ShowEnd SubForm3中的內(nèi)容Private Sub Command1_Click()If Check1.Value = 1 Then If dd <> none And ee <> none Then ff = InStr(dd + ee, LCase(Form1.Text1.Text), LCase(Form3.Text1.Text) Else ff = InStr(LCase(Form1.Text1.Text), LCase(Form3.Text1.Text) End If Form1.Text1.SelStart = ff - 1 dd = ff ee = Len(Text1.Text) Form1.Text1.SelLength = ee kk = kk + 1Else If dd <> none And ee <> none Then ff = InStr(dd + ee, LCase(Form1.Text1.Text), LCase(Form3.Text1.Text) Else ff = InStr(LCase(Form1.Text1.Text), LCase(Form3.Text1.Text) End If Form1.Text1.SelStart = ff - 1 dd = ff ee = Len(Text1.Text) Form1.Text1.SelLength = ee kk = kk + 1End IfForm1.SetFocusEnd SubPrivate Sub Command2_Click()Form1.Text1.SelText = Text2.TextEnd SubPrivate Sub Command3_Click()Do While (dd + ee) < Len(Form1.Text1.Text) If Check1.Value = 1 Then If dd <> 0 And ee <> 0 Then ff = InStr(dd + ee, LCase(Form1.Text1.Text), LCase(Form3.Text1.Text) Else ff = InStr(LCase(Form1.Text1.Text), LCase(Form3.Text1.Text) End If Form1.Text1.SelStart = ff - 1 dd = ff ee = Len(Text1.Text) Form1.Text1.SelLength = ee Form1.SetFocus Else If dd <> 0 And ee <> 0 Then ff = InStr(dd + ee, LCase(Form1.Text1.Text), LCase(Form3.Text1.Text) Else ff = InStr(LCase(Form1.Text1.Text), LCase(Form3.Text1.Text) End If Form1.Text1.SelStart = ff - 1 dd = ff ee = Len(Text1.Text) Form1.Text1.SelLength = ee Form1.SetFocus End If Form1.Text1.SelText = Text2.TextLoopEnd SubPrivate Sub Command4_Click()Form3.HideEnd Sub(10)全選Private Sub selectall_Click()Text1.SelStart = Len(RTrim(Text1.Text) - Len(Trim(Text1.Text)Text1.SelLength = Len(Text1.Text)End Sub(11)時間/日期Private Sub date_Click()Text1.SelText = NowEnd Sub(12)字體Private Sub font_Click()cd.Flags = 259cd.ShowFontText1.FontBold = cd.FontBoldText1.FontItalic = cd.FontItalicText1.FontName = cd.FontNameText1.FontUnderline = cd.FontUnderlineText1.FontStrikethru = cd.FontStrikethruText1.ForeColor = cd.ColorEnd Sub(13)其它事件Private Sub Form_Resize()Text1.Width = ScaleWidthText1.Height = ScaleHeightEnd SubPrivate Sub Text1_Change()d = TrueEnd SubPrivate Sub Timer1_Timer()If Text1.SelText <> "" Then qie.Enabled = True copy.Enabled = True delete.Enabled = True If Text1.SelText = Text1.Text Then selectall.Enabled = False Else selectall.Enabled = True End If zhuan.Enabled = False Else qie.Enabled = False copy.Enabled = False delete.Enabled = False instead.Enabled = True date.Enabled = True selectall.Enabled = True zhuan.Enabled = False If Len(Text1.Text) <> 0 Then find.Enabled = True findnext.Enabled = True Else find.Enabled = False findnext.Enabled = False End IfEnd IfIf Clipboard.GetText <> "" Then tie.Enabled = TrueElse tie.Enabled = FalseEnd IfIf d = True Then cancel.Enabled = True Else cancel.Enabled = False End IfEnd Sub(14)變量Form1通用中Dim d As Boolean Dim rrModule1中定義的全局變量:Public dd As Integer, ee As IntegerPublic kk As Integer五、總結通過編寫記事本,我進一步熟悉了所學VB的程序語言,成功的編寫了一個記事本。專心-專注-專業(yè)