'读写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