当前位置:编程学习 > VB >>

利用INI文件给程序创建动态菜单

答案:
利用INI文件给程序创建动态菜单
作者:土人
象Word那样,在“文件”菜单中可以找到我们最近打开或编辑过的文件。这是程序在运行时创建了动态菜单的缘故。VB程序实现此功能有很多文章探讨过,但大都着眼于Windows的注册表。我们的注册表早已发福了,有没有办法不让她再增加负担?
用INI文件!INI文件是系统、应用程序的配置文件。它可以使得我们的VB程序更具灵活性,充分地利用它自然也能够实现动态菜单的创建。下面给个例子。

首先,我们得给程序添加一个模块,在模块中申明读写INI用的两个Windows API函数,并在其中封装自定义的读取、写入INI的函数:

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

接着,回到主窗体,给主窗体添加若干控件、编辑菜单(控件和菜单名称详见代码),其中,“文件”菜单的动态菜单要用数组,其Index号可以从1开始,注意将这些动态菜单设为不可见。本例可保存最近打开的三个文件,如需要多一些可自行添加。以下是主窗体代码:

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文件实现动态菜单的创建,若需要移植到你的程序中,应作相应的改动,使其更具合理性和实用性。有什么看法欢迎大家与我交流!

上一个:网络数据库三国:IBM微软Oracle争霸
下一个:VB中利用WinRAR进行文件压缩

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,