📄
字号:
VERSION 5.00
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
Begin VB.Form frmRecent
Appearance = 0 'Flat
BackColor = &H80000005&
Caption = "菜单_近期访问文件"
ClientHeight = 3195
ClientLeft = 165
ClientTop = 855
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3195
ScaleWidth = 4680
StartUpPosition = 3 '窗口缺省
Begin MSComDlg.CommonDialog CommonDialog1
Left = 3240
Top = 2400
_ExtentX = 847
_ExtentY = 847
_Version = 393216
End
Begin VB.Menu mnuFile
Caption = "文件"
Begin VB.Menu mnuFileOpen
Caption = "打开"
End
Begin VB.Menu mnuSeparator
Caption = "-"
End
Begin VB.Menu mnuFileArray
Caption = "近期文件列表"
Index = 0
End
End
Begin VB.Menu mnuExit
Caption = "退出"
End
End
Attribute VB_Name = "frmRecent"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'菜单_近期访问文件
'启动对象
Option Explicit
Dim intI As Integer, intRecent As Integer
Dim strKey As String, strINI As String
Dim strINIFileName As String, strFileName As String
Dim strFileArray() As String
Dim mnuA As Menu, intMenuNum As Integer
Private Sub Form_Load()
mnuFileArray(0).Visible = False '使标题为“菜单列表”的菜单不可视
mnuSeparator.Visible = False '使分割线不可视
'读INI文件的有关内容到菜单数组
ReadFromINI Me
End Sub
'从INI文件读取信息
Private Sub ReadFromINI(ByVal frmA As Form)
'将strINI设置为255个字符,保存键字符串
strINI = String(255, 0)
With frmA
For intI = 1 To MaxFileNumber
strKey = "RecentFile" & intI '设置键名
'从段“RecentFiles”读取strKey所代表的键字符串,保存在strINI中,
'返回值intRecent表示读取的实际字符个数
intRecent = GetPrivateProfileString("RecentFiles", _
strKey, "********", strINI, Len(strINI), _
App.Path & "\RecentFile.ini")
If intRecent And Left(strINI, 8) <> "********" Then
'如果读取的实际字符数不为0,并且strINI前8个字符不为********
'则显示文件菜单的分割线
.mnuSeparator.Visible = True
If .mnuFileArray.UBound < intI Then
'菜单数组无法容纳从INI文件中读取的文件名字符串,
'使用Load加载菜单项,可以加载任意个菜单项
Load .mnuFileArray(intI)
End If
'将返回的字符串加上表示为快捷键的序号,
'赋予菜单数组的Caption属性
.mnuFileArray(intI).Caption = "&" & intI & "." & strINI
.mnuFileArray(intI).Visible = True '使菜单项可视
End If
Next intI
End With
End Sub
'向INI文件写入信息
Private Sub WriteToINI()
strINIFileName = App.Path & "\RecentFile.ini" 'INI文件名
'由菜单数组中获得文件名
'由mnuFileArray.Count可以获得菜单数组中菜单项的个数
For intI = 1 To mnuFileArray.Count - 1
strINI = GetFileFromArray(intI, Me) '从菜单数组中获得文件名
strKey = "RecentFile" & intI
'"RecentFiles"是段名
'strKey是键名
'strINI是要写入INI文件的字符串
'strINIFileName是INI文件名
WritePrivateProfileString "RecentFiles", _
strKey, strINI, strINIFileName
Next intI
End Sub
'从菜单数组中获得文件名,保存在GetFileFromArray
Private Function GetFileFromArray(index As Integer, _
ByVal frmA As Form) As String
'index是菜单数组中的索引号
'frmA是窗体
Set mnuA = frmA.mnuFileArray(index) '创建菜单对象
'从菜单数组中获取文件名
'将菜单标题的左边3个字符去掉,剩下的是文件全名
strFileName = Right(mnuA.Caption, Len(mnuA.Caption) - 3)
GetFileFromArray = strFileName '返回函数值
End Function
'将文件加到菜单数组中
Private Sub AddFileToArray(ByVal strFileName As String, _
ByVal frmA As Form)
'strFileName是从公共对话框中返回的文件名
'frmA是窗体类型的对象变量
'strFileArray是动态数组,使用Preserve可以保持原有数据不变
ReDim Preserve strFileArray(0) As String
'将传递过来的文件名保存在文件数组中
strFileArray(0) = strFileName
With frmA
.mnuSeparator.Visible = True
intMenuNum = .mnuFileArray.UBound '菜单数组中菜单个数
Dim iLoop As Integer
iLoop = 1
'遍历菜单数组
For intI = 1 To intMenuNum
'获得菜单数组中索引号为intI的文件名
strINI = GetFileFromArray(intI, Me)
If strINI <> strFileName Then
'如果传递过来的文件名不在文件数组中,
'则重新定义文件数组,并保留以前的文件数组
ReDim Preserve strFileArray(iLoop) As String
'将菜单数组中的文件名保存在文件数组中
strFileArray(iLoop) = strINI
iLoop = iLoop + 1
End If
Next intI
If intMenuNum < UBound(strFileArray) + 1 And _
intMenuNum < MaxFileNumber Then
'如果菜单数组小于要显示的文件数,并且小于设置的菜单数组的最大数,
'则使用Load语句创建一个菜单对象
Load mnuFileArray(intMenuNum + 1)
End If
intMenuNum = .mnuFileArray.UBound '菜单数组中菜单个数
'遍历菜单数组
For intI = 1 To intMenuNum
'将文件数组名显示在菜单中
.mnuFileArray(intI).Caption = "&" & intI & "." _
& strFileArray(intI - 1)
.mnuFileArray(intI).Visible = True '使菜单项可视
Next intI
End With
End Sub
'卸载窗体时将菜单数组中的内容写入INI文件
Private Sub Form_Unload(Cancel As Integer)
WriteToINI
End Sub
'使用公共对话框打开可执行文件
Private Sub mnuFileOpen_Click()
Dim strOpenFileName As String
On Error Resume Next
CommonDialog1.CancelError = True
CommonDialog1.DialogTitle = "打开文件"
CommonDialog1.Filter = "可执行文件(*.exe)|*.exe"
CommonDialog1.ShowOpen
If Err <> cdlCancel Then
strOpenFileName = CommonDialog1.FileName '取得文件名
Else
Exit Sub
End If
'将文件加到菜单数组中
AddFileToArray strOpenFileName, Me
End Sub
'单击菜单中的文件可以执行
Private Sub mnuFileArray_Click(index As Integer)
Dim strX As String
Dim vntX
Dim strOpenFileName As String
'从菜单数组中获得文件名
strFileName = GetFileFromArray(index, Me)
'判断文件在目录中是否存在
strX = Dir(strFileName, vbNormal + vbHidden + vbReadOnly)
If strX = "" Then
MsgBox strFileName & "文件不存在!"
Exit Sub
End If
vntX = Shell(strFileName, 1) '执行EXE文件
End Sub
'退出
Private Sub mnuExit_Click()
Unload Me '卸载窗体
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -