用VB6做记事本的方法

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

也许你们经常用到微软的软件但是却没看到编这些软件是要花费很多精力的。我试着模仿

记事本编了个类似的软件,功能比Windows自带的少了几个但却添加了几个新的功能。虽然

是个简单的程序(可以说是Windows上技术含量最少的程序了)但是却花费了我几天的时间,

我上网找代码,在百度群里问人(我是个菜鸟请大家多多包涵....)
终于才编了这个记事本。下面我就教大家编这个记事本。
这个程序有三个窗体(一个主窗体,一个关于窗体,一个是特殊字符输入的窗体) 主窗

体只有三个控件 TextBox StatusBar CommonDialog 这些都可以轻易找到。把这些控件加

入之后就开始修改属性了。TextBox的属性如下:
name=text1
font=fixedsys
multitline=true
text清空
记得把TextBox放大到充满窗体那样的大小

CommonDialog的属性如下:
filter=文本文档(.txt).txt所有文件(.).

StatusBar自定义设置:
窗格1
文本:自制记事本
窗格2
对齐:-2
样式:-6
窗格3
样式:-5

代码:

===============================================================================
===============================================================================


第一个窗体代码:
Private Declare Function SendMessage Lib user32 Alias SendMessageA (ByVal Hwnd

As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const EM_UNDO = &HC7 '用 SendMessage API 函数向 TextBox 发送 EM_UNDO 消息,要

确定 TextBox 是否可以 Undo,发送 EM_CANUNDO 消息。
Dim a '声明变量(撤销,字体,a)
Private Sub Form_Load()
Me.Caption = 自制记事本 & & 未命名
End Sub

Private Sub Form_Resize()
Text1.Left = 0 '改变大小时改变控件大小
Text1.Top = 0
Text1.Width = Me.Width - 100
Text1.Height = Me.Height - 1080
End Sub

Private Sub Form_Unload(Cancel As Integer)
On Error Resume Next
If a = 1 Then '如果文字改变了就保存
b = MsgBox(文件字符已改变。是否保存文件?, vbYesNoCancel + vbExclamation, 自制

记事本)
If b = vbYes Then '如果选是就保存
CommonDialog1.ShowSave '保存
Open CommonDialog1.FileName For Output As #1
Print #1, Text1.Text
Close 1
End If
If b = vbNo Then '选否就关闭
End
End If
If b = vbCancel Then '取消退出
Cancel = 1
End If
End If
End Sub

Private Sub Text1_Change()
a = 1 'a=1的作用为下文铺垫
End Sub

Private Sub 帮助主题_Click()
CommonDialog1.ShowHelp '这个帮助可有可无 如果你能加入帮助最好
End Sub

Private Sub 保存_Click()
On Error Resume Next
CommonDialog1.ShowSave '保存的代码...
Open CommonDialog1.FileName For Output As #1
Print #1, Text1.Text
Close 1
Me.Caption = 自制记事本 & & CommonDialog1.FileName '改变窗体标题
a = 0 'a=0 防止重复保存
End Sub

Private Sub

撤销_Click()
Dim UndoResult As Long '撤销的函数
UndoResult = SendMessage(Text1.Hwnd, EM_UNDO, 0, 0)
End Sub

Private Sub 打开_Click()
On Error Resume Next
If a = 1 Then '如果文字改变了就保存
If MsgBox(文件字符已改变。是否保存文件?, vbOKCancel + vbExclamation, 自制记事

本) = vbOK Then
CommonDialog1.ShowSave
Open CommonDialog1.FileName For Output As #1
Print #1, Text1.Text
Close 1
End If
End If
CommonDialog1.ShowOpen '打开文件
Open CommonDialog1.FileName For Input As #1
Text1.Text = StrConv(InputB$(LOF(1), 1), vbUnicode)
Close #1
Me.Caption = 自制记事本 & & CommonDialog1.FileName '改变窗体标题
a = 0 'a=0 防止重复保存
End Sub

Private Sub 复制_Click()
Clipboard.Clear 'clipboard是剪切板的意思 这句话是清除剪切板的内容
Clipboard.SetText Text1.SelText '把选中的文字加入剪切板
End Sub

Private Sub 关于自制记事本_Click()
Form2.Show
End Sub

Private Sub 剪切_Click()
Clipboard.Clear '撤销比复制就多了个把选择的内容清除
Clipboard.SetText Text1.SelText
Text1.SelText =
End Sub

Private Sub 全选_Click()
Text1.SelStart = 0 '0表示从文章开头选
Text1.SelLength = Len(Text1.Text) 'len(text1.text)文章的长度
End Sub

Private Sub 删除_Click()
Text1.SelText = '清除选择内容
End Sub

Private Sub 时间日期_Click()
Text1.SelText = Date & " " & Str(Time) & " " & "星期" & Weekday(Date - 1)
End Sub

Private Sub 特殊字符_Click()
Form3.Show
End Sub

Private Sub 退出_Click()
Unload Me '用unload me比end的好处就是unload me可以触发保存事件
End Sub

Private Sub 新建_Click()
On Error Resume Next
If a = 1 Then
If MsgBox(文件字符已改变。是否保存文件?, vbOKCancel + vbExclamation, 自制记事

本) = vbOK Then
CommonDialog1.ShowSave
Open CommonDialog1.FileName For Output As #1
Print #1, Text1.Text
Close 1
End If
End If
Text1.Text = '新建就是清空textbox。。。。
Me.Caption = 自制记事本 & & 未命名
a = 0
End Sub

Private Sub 颜色_Click()
CommonDialog1.ShowColor '选择字体颜色
Text1.ForeColor = CommonDialog1.Color
End Sub

Private Sub 粘贴_Click()
If Clipboard.GetFormat(1) Then '如果剪贴板有内容才能复制
Text1.SelText = Clipboard.GetText(1) '复制
End If
End Sub

Private Sub 字数统计_Click()
If Text1.SelLength = 0 Then
MsgBox "字数统计(全篇)" & Str(Len(Text1.Text)), vbInformation, "字数统计" '统计

字数
Else: MsgBox "字数统计(选择)" & Str(Text1.SelLength), vbInformation, "字数统计"
End If
End Sub

Private Sub 字体_Click()
CommonDialog1.ShowFont '修改字体属性
Text1.FontBold = CommonDialog1.FontBold '粗体字
Text1.FontItalic = CommonDialog1.FontItalic '斜体字
Text1.FontStrikethru = CommonDialog1.FontStrikethru '删除线
Text1.FontUnder

line = CommonDialog1.FontUnderline '下划线
Text1.Font = CommonDialog1.FontName '字体名称
Text1.FontSize = CommonDialog1.FontSize '字体大小
End Sub


==============================================================================
==============================================================================


第二个是关于窗体 在form.picture加入一张你喜欢的图片(最好用自己PS的)来介绍自己的

软件
以下是代码:

Private Sub Form_LostFocus()
SetFocus '使窗体不会失去焦点
End Sub

Private Sub Form_Unload(Cancel As Integer)
Form1.Show '介绍完了回到主窗体
End Sub

===============================================================================
===============================================================================


第三个是特殊文字输入的窗体 这个窗体比较复杂我想了好久才有灵感怎么做
首先在窗体加入N个label 这些label的capion属性就是你想加入的特殊字符
然后代码如下:
Option Explicit '这是窗体总在最前的代码 比较复杂...
Const Hwndx = -1
Private Declare Function SetWindowPos Lib user32 (ByVal Hwnd As Long, _
ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As

Long, _
ByVal cy As Long, ByVal wFlags As Long) As Long

Private Sub Form_Load()
Dim XX As Long '这是窗体在前的参数
XX = SetWindowPos(Me.Hwnd, Hwndx, 0, 0, 0, 0, 3)
End Sub

label的click事件的格式如下
Form1.Text1.SelText = "囧"
双引号里面的是你要输入的特殊字符 例如:囧 记得要与label的标题一一对应

以下是窗体3的完整代码:
Option Explicit
Const Hwndx = -1
Private Declare Function SetWindowPos Lib "user32" (ByVal Hwnd As Long, _
ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As

Long, _
ByVal cy As Long, ByVal wFlags As Long) As Long



Private Sub Form_Load()
Dim XX As Long
XX = SetWindowPos(Me.Hwnd, Hwndx, 0, 0, 0, 0, 3)
End Sub

Private Sub Label1_Click()
Form1.Text1.SelText = ":-D"
End Sub

Private Sub Label10_Click()
Form1.Text1.SelText = "-_-|||"
End Sub

Private Sub Label11_Click()
Form1.Text1.SelText = "=_="
End Sub

Private Sub Label12_Click()
Form1.Text1.SelText = "+_+"
End Sub

Private Sub Label13_Click()
Form1.Text1.SelText = "嘦"
End Sub

Private Sub Label14_Click()
Form1.Text1.SelText = "巭"
End Sub

Private Sub Label15_Click()
Form1.Text1.SelText = "深"

End Sub

Private Sub Label16_Click()
Form1.Text1.SelText = "囧"

End Sub

Private Sub Label17_Click()
Form1.Text1.SelText = "兲"

End Sub

Private Sub Label18_Click()
Form1.Text1.SelText = "嫑"

End Sub

Private Sub Label19_Click()
Form1.Text1.SelText = "跑"

End Sub

Private Sub Label2_Click()
Form1.Text1.SelText = ":-)"
End Sub

Private Sub Label20_Click()
Form1.Text1.SelText = "o(≧v≦)o~~好棒"
End Sub

Private Sub La

bel21_Click()
Form1.Text1.SelText = "≠"
End Sub

Private Sub Label22_Click()
Form1.Text1.SelText = "≤"
End Sub

Private Sub Label23_Click()
Form1.Text1.SelText = "≥"
End Sub

Private Sub Label24_Click()
Form1.Text1.SelText = "≈"
End Sub

Private Sub Label25_Click()
Form1.Text1.SelText = "≡"
End Sub

Private Sub Label26_Click()
Form1.Text1.SelText = "≒"
End Sub

Private Sub Label27_Click()
Form1.Text1.SelText = "∥"
End Sub

Private Sub Label28_Click()
Form1.Text1.SelText = "="
End Sub

Private Sub Label29_Click()
Form1.Text1.SelText = "≌"
End Sub

Private Sub Label3_Click()
Form1.Text1.SelText = ";-)"
End Sub

Private Sub Label30_Click()
Form1.Text1.SelText = "∽"
End Sub

Private Sub Label31_Click()
Form1.Text1.SelText = "≮"
End Sub

Private Sub Label32_Click()
Form1.Text1.SelText = "≯"
End Sub

Private Sub Label33_Click()
Form1.Text1.SelText = "∑"
End Sub

Private Sub Label34_Click()
Form1.Text1.SelText = "∞"
End Sub

Private Sub Label35_Click()
Form1.Text1.SelText = "∝"
End Sub

Private Sub Label36_Click()
Form1.Text1.SelText = "⊙"
End Sub

Private Sub Label4_Click()
Form1.Text1.SelText = ":-O"
End Sub

Private Sub Label5_Click()
Form1.Text1.SelText = ":)"
End Sub

Private Sub Label6_Click()
Form1.Text1.SelText = ":-P"
End Sub

Private Sub Label7_Click()
Form1.Text1.SelText = ":-("
End Sub

Private Sub Label8_Click()
Form1.Text1.SelText = "~~~^_^~~~ "
End Sub

Private Sub Label9_Click()
Form1.Text1.SelText = "-_-!"
End Sub


相关文档
最新文档