Option Explicit
'读写INI的API函数
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any,
ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
'自定义写入INI函数
Public Function WriteIni(ByVal section As String, ByVal key As String, ByVal value As String) As Boolean
Dim x As Long, Buff As String * 128, I As Integer
Buff = value + Chr(0)
x = WritePrivateProfileString(section, key, Buff, App.Path + "\MenuSetting.ini")
WriteIni = x
End Function
'自定义读取INI函数
Public Function ReadIni(ByVal section As String, ByVal key As String) As String
Dim x As Long, Buff As String * 128, I As Integer
x = GetPrivateProfileString(section, key, "", Buff, 128, App.Path + "\MenuSetting.ini")
I = InStr(Buff, Chr(0))
ReadIni = Trim(Left(Buff, I - 1))
End Function
Option Explicit
'声明用于判断写入INI中的FileName(n)中的n变量
Dim I As String '为了能添在FileName串的后面,声明为String
Private Sub Form_Load()
I = 0 '初值
Text1.Left = 0
Text1.Top = 0
Text1 = ""
Text1.FontSize = 12
Me.Caption = "txtEditor"
Me.Width = 8000
Me.Height = 6000
AddMenu '添加动态菜单
End Sub
Private Sub Form_Resize() '这个没什么可说,为了使例程完整而已
Text1.Width = Me.ScaleWidth
Text1.Height = Me.ScaleHeight
End Sub
Private Sub mnuExit_Click()
End '退出
End Sub
'打开文件
Private Sub mnuOpen_Click()
Dim sF As String
CommonDialog1.Filter = "文档文件(*.txt)|*.txt|所有文件(*.*)|*.*"
CommonDialog1.ShowOpen
Open CommonDialog1.FileName For Input As #1
Text1.Text = StrConv(InputB$(LOF(1), 1), vbUnicode)
Close #1
If I >= 3 Then I = 0 '如大于等于3则返回原值
I = I + 1
sF = "FileName" + I
'打开后写进INI文件
CommonDialog1.FileName = WriteIni("Open", sF, CommonDialog1.FileName)
AddMenu '立即添加使动态菜单生效
End Sub
'添加菜单
Private Sub AddMenu()
Dim fN1 As String, fN2 As String, fN3 As String
'从INI文件中读取数据
fN1 = ReadIni("Open", "FileName1")
fN2 = ReadIni("Open", "FilEName2")
fN3 = ReadIni("Open", "FileName3")
'如数据存在则令动态菜单可见并给其Caption属性赋值
If fN3 <> "" Then mnuSep02.Visible = True: mnuAdd(1).Visible = True: mnuAdd(1).Caption = fN1
If fN2 <> "" Then mnuSep02.Visible = True: mnuAdd(2).Visible = True: mnuAdd(2).Caption = fN2
If fN1 <> "" Then mnuSep02.Visible = True: mnuAdd(3).Visible = True: mnuAdd(3).Caption = fN3
End Sub
--------------------------------
请问这篇"演示了利用INI文件实现动态菜单的创建"若干控件、编辑菜单部分按钮与控制看懂一点. 有几个地方不明白.以下:
Private Sub Form_Resize()
------------------------
Private Sub AddMenu()
------------------------
还有在Private Sub Form_Load() 所提到的"Me",
上面这3行的相关代码是按钮还是什么??如何添加上去的?谁能注释一下?
'读写INI的API函数
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any,
ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
'自定义写入INI函数
Public Function WriteIni(ByVal section As String, ByVal key As String, ByVal value As String) As Boolean
Dim x As Long, Buff As String * 128, I As Integer
Buff = value + Chr(0)
x = WritePrivateProfileString(section, key, Buff, App.Path + "\MenuSetting.ini")
WriteIni = x
End Function
'自定义读取INI函数
Public Function ReadIni(ByVal section As String, ByVal key As String) As String
Dim x As Long, Buff As String * 128, I As Integer
x = GetPrivateProfileString(section, key, "", Buff, 128, App.Path + "\MenuSetting.ini")
I = InStr(Buff, Chr(0))
ReadIni = Trim(Left(Buff, I - 1))
End Function
Option Explicit
'声明用于判断写入INI中的FileName(n)中的n变量
Dim I As String '为了能添在FileName串的后面,声明为String
Private Sub Form_Load()
I = 0 '初值
Text1.Left = 0
Text1.Top = 0
Text1 = ""
Text1.FontSize = 12
Me.Caption = "txtEditor"
Me.Width = 8000
Me.Height = 6000
AddMenu '添加动态菜单
End Sub
Private Sub Form_Resize() '这个没什么可说,为了使例程完整而已
Text1.Width = Me.ScaleWidth
Text1.Height = Me.ScaleHeight
End Sub
Private Sub mnuExit_Click()
End '退出
End Sub
'打开文件
Private Sub mnuOpen_Click()
Dim sF As String
CommonDialog1.Filter = "文档文件(*.txt)|*.txt|所有文件(*.*)|*.*"
CommonDialog1.ShowOpen
Open CommonDialog1.FileName For Input As #1
Text1.Text = StrConv(InputB$(LOF(1), 1), vbUnicode)
Close #1
If I >= 3 Then I = 0 '如大于等于3则返回原值
I = I + 1
sF = "FileName" + I
'打开后写进INI文件
CommonDialog1.FileName = WriteIni("Open", sF, CommonDialog1.FileName)
AddMenu '立即添加使动态菜单生效
End Sub
'添加菜单
Private Sub AddMenu()
Dim fN1 As String, fN2 As String, fN3 As String
'从INI文件中读取数据
fN1 = ReadIni("Open", "FileName1")
fN2 = ReadIni("Open", "FilEName2")
fN3 = ReadIni("Open", "FileName3")
'如数据存在则令动态菜单可见并给其Caption属性赋值
If fN3 <> "" Then mnuSep02.Visible = True: mnuAdd(1).Visible = True: mnuAdd(1).Caption = fN1
If fN2 <> "" Then mnuSep02.Visible = True: mnuAdd(2).Visible = True: mnuAdd(2).Caption = fN2
If fN1 <> "" Then mnuSep02.Visible = True: mnuAdd(3).Visible = True: mnuAdd(3).Caption = fN3
End Sub
--------------------------------
请问这篇"演示了利用INI文件实现动态菜单的创建"若干控件、编辑菜单部分按钮与控制看懂一点. 有几个地方不明白.以下:
Private Sub Form_Resize()
------------------------
Private Sub AddMenu()
------------------------
还有在Private Sub Form_Load() 所提到的"Me",
上面这3行的相关代码是按钮还是什么??如何添加上去的?谁能注释一下?