利用INI文件给程序创建动态菜单
2008-02-23 06:57:53来源:互联网 阅读 ()
用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文件实现动态菜单的创建,若需要移植到你的程序中,应作相应的改动,使其更具合理性和实用性。有什么看法欢迎大家与我交流!
上一篇: 移动文件到回收站
下一篇: 开启文件属性窗口
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 利用INI文件给程序创建动态菜单 2018-06-17
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash