用VB编写的记事本源码教程

  • 格式:doc
  • 大小:67.00 KB
  • 文档页数:8

一.实验要求用所学的VB语言编写一个自己的记事本,其功能与计算机中记事本的功能基本一致,程序结构清晰。

二.实验目的通过编写记事本,熟悉并掌握所学VB的程序语言,锻炼编程思想,从而触类旁通发挥自己的创造力编辑自己的作品。

三.设计软件所实现的功能窗体界面包括:文件、编辑、格式、查看、帮助五个主菜单。

每个主菜单都有自己的子菜单,其主菜单的功能如下:1.文件:包括新建、打开、保存、另存为、页面设置、打印、退出的功能。

2.编辑:包括撤销、剪切、复制、粘贴、删除、查找、查找下一个、替换、转到、全选、时间/日期的功能。

3.格式:包括自动换行、字体的功能。

4.查看:包括状态栏的功能5.帮助:包括帮助主题,关于记事本。

四.设计介绍.1.绘制界面新建一个标准的.exe工程,将其caption属性改为”无标题-记事本”,点击icon属性给它找一个合适的图标,其编辑的界面图如下:2.编辑菜单按ctrl+E调出菜单编辑器,来做如下几个菜单:(1)文件菜单:文件(第一层)file、新建(第二层)new、打开(第二层)open、保存(第二层)save、另存为(第二层)lingsave、-(第二层)spar1(分隔线)、页面设置(第二层)design、打印(第二层)print、-(第二层)spar2(分隔线)、退出(第二层)exit(2)编辑菜单编辑(第一层)edit、撤销(第二层)cancel、-(第二层)spar3、剪切(第二层)qie、复制(第二层)copy、粘贴(第二层)tie、删除(第二层)delete、-(第二层)spar4、查找(第二层)find、查找下一个(第二层)findnext、替换(第二层)instead、转到(第二层)zhuan、-(第二层)spar5、全选(第二层)selectall、时间/日期(第二层)date(3)格式菜单格式(第一层)style、自动换行(第二层)autozhuan、字体(第二层)font(4)查看菜单查看(第一层)cha、状态栏(第二层)state(5)帮助菜单帮助(第一层)help、帮助主题(第二层)topic、关于记事本(第二层)about3.编辑代码(1)新建Private Sub new_Click()Dim m As New Form1If d ThenIf Form1.Caption = "无标题-记事本" Thenyy = MsgBox("文件无标题-记事本文字已经改变," + Chr(13) + Chr(10) + "是否保存文件?", 51, "记事本")Elseyy = MsgBox("文件 " + rr + " 文字已经改变," + Chr(13) + Chr(10) + "是否保存文件?", 51, "记事本")End IfEnd IfIf yy = 6 ThenCall lingsave_ClickElseIf yy = 2 ThenExit SubEnd IfForm1.Hidem.ShowEnd Sub(2)打开Private Sub open_Click()cd.InitDir = "D:"cd.CancelError = Truecd.Filter = "全部文挡(*.*)|*.*|文本文挡(*.txt)|*.txt"cd.FilterIndex = 2If d <> True Thenstart: On Error GoTo eecd.ShowOpenOpen cd.FileName For Input As #1rr = cd.FileNameDo While Not EOF(1)Line Input #1, a$whole$ = whole$ + a$ + Chr(13) + Chr(10)LoopText1.Text = whole$Close #1ss = Split(cd.FileTitle, ".")dd = ss(0)Form1.Caption = dd + "-记事本"ElseIf Form1.Caption = "无标题-记事本" Thenyy = MsgBox("文件无标题-记事本文字已经改变,是否保存文件?", 51, "记事本") Elseyy = MsgBox("文件 " + rr + " 文字已经改变,是否保存文件?", 51, "记事本")End IfIf yy = 6 ThenCall lingsave_ClickGoTo startElseIf yy = 7 ThenGoTo startElseExit SubEnd IfEnd Ifee: Exit SubEnd Sub(3)保存Private Sub save_Click()If Form1.Caption = "无标题-记事本" ThenCall lingsave_Clickd = FalseElsecc = Split(Form1.Caption, "-")ff = cc(0)Open ff + ".txt" For Output As #1Print #1, Text1.TextClose #1d = FalseEnd 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 #2 Print #2, Text1.TextClose #2d = Falsess = 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 numPrinter.CurrentX = 10Printer.CurrentY = 10Printer.Print Text1.TextNext iPrinter.EndDocExit Subjj:Exit SubEnd Sub(6)退出Private Sub exit_Click()EndEnd Sub(7)剪切Private Sub qie_Click()Clipboard.ClearClipboard.SetText Text1.SelText Text1.SelText = ""End Sub(8)复制Private Sub copy_Click() Clipboard.ClearClipboard.SetText Text1.SelTextEnd 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中的内容Private Sub Command1_Click()If Check1.Value = 1 ThenIf dd <> none And ee <> none Thenff = InStr(dd + ee, LCase(Form1.Text1.Text), LCase(Form3.Text1.Text)) Elseff = InStr(LCase(Form1.Text1.Text), LCase(Form3.Text1.Text))End IfForm1.Text1.SelStart = ff - 1dd = ffee = Len(Text1.Text)Form1.Text1.SelLength = eekk = kk + 1ElseIf dd <> none And ee <> none Thenff = InStr(dd + ee, LCase(Form1.Text1.Text), LCase(Form3.Text1.Text))Elseff = InStr(LCase(Form1.Text1.Text), LCase(Form3.Text1.Text))End IfForm1.Text1.SelStart = ff - 1dd = ffee = Len(Text1.Text)Form1.Text1.SelLength = eekk = 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 ThenIf dd <> 0 And ee <> 0 Thenff = InStr(dd + ee, LCase(Form1.Text1.Text), LCase(Form3.Text1.Text)) Elseff = InStr(LCase(Form1.Text1.Text), LCase(Form3.Text1.Text))End IfForm1.Text1.SelStart = ff - 1dd = ffee = Len(Text1.Text)Form1.Text1.SelLength = eeForm1.SetFocusElseIf dd <> 0 And ee <> 0 Thenff = InStr(dd + ee, LCase(Form1.Text1.Text), LCase(Form3.Text1.Text)) Elseff = InStr(LCase(Form1.Text1.Text), LCase(Form3.Text1.Text))End IfForm1.Text1.SelStart = ff - 1dd = ffee = Len(Text1.Text)Form1.Text1.SelLength = eeForm1.SetFocusEnd IfForm1.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 <> "" Thenqie.Enabled = Truecopy.Enabled = Truedelete.Enabled = TrueIf Text1.SelText = Text1.Text Thenselectall.Enabled = FalseElseselectall.Enabled = TrueEnd Ifzhuan.Enabled = FalseElseqie.Enabled = Falsecopy.Enabled = Falsedelete.Enabled = Falseinstead.Enabled = Truedate.Enabled = Trueselectall.Enabled = Truezhuan.Enabled = FalseIf Len(Text1.Text) <> 0 Thenfind.Enabled = Truefindnext.Enabled = TrueElsefind.Enabled = Falsefindnext.Enabled = FalseEnd IfEnd IfIf Clipboard.GetText <> "" Thentie.Enabled = TrueElsetie.Enabled = FalseEnd IfIf d = True Thencancel.Enabled = TrueElsecancel.Enabled = FalseEnd IfEnd Sub(14)变量Form1通用中Dim d As Boolean Dim rrModule1中定义的全局变量:Public dd As Integer, ee As IntegerPublic kk As Integer五、总结通过编写记事本,我进一步熟悉了所学VB的程序语言,成功的编写了一个记事本。