开发手记(九)——在文件菜单中记录最近使用过的文件

       Word的“文件”菜单最下面列出了最近使用过的文件名及路径,文件名及路径会随着用户的使用不断地发生变化,这符合统计学中“最近使用”最大可能是“经常使用”的原则,方便了用户,提高了效率。它的实现方法有很多,我举一例,供大家参考。







       一、在工作目录下创建一个LastFile.ini文件,其中第一行为历史文件的总数,以下行是历史文件的全路径。当然您也可以使用数据表存储,那样编程时也许更方便一些。







       LastFile.ini文件内容如:4







"D:\程序实例\slzj\slzj源代码\2004.11.18水利造价\示例.mdb"







"C:\WINDOWS\Desktop\111\111.mdb"







"D:\程序实例\slzj\slzj源代码\2004.11.18水利造价\示例.mdb"







"D:\程序实例\slzj\slzj源代码\2004.11.3\2004.11.3\2004.11.3\2004.11.3\示例(审查).mdb"







    二、在Form_Load中编写如下代码,达到在文件菜单中显示历史文件的效果







'**************显示以往打开的文件记录***************************







    '对配置文件不存在的情况下,作出操作。







    If Dir(App.Path & "\lastfile.ini") = "" Then







        Open App.Path & "\lastfile.ini" For Output As #1







        Write #1, 0







        Close #1







End If







'打开lastfile.ini文件







    Open App.Path & "\lastfile.ini" For Input As #1







    Dim strLastfile2 As String







    '获取历史文件的数目







    Line Input #1, strLastfile2







    iMaxLastfile = Int(strLastfile2)







    Dim i As Integer







    '添加历史文件到ActiveBar菜单,先在ActiveBar中预设4各Command和一个分割线。并把他们的Visible=False







    For i = 1 To iMaxLastfile







        Line Input #1, strLastfile2







        strLastfile(i - 1) = Mid(strLastfile2, 2, Len(strLastfile2) - 2)‘去引号







        AABar.Bands("MenuFile").Tools.item(i + 10).Caption = strLastfile(i - 1)







        AABar.Bands("MenuFile").Tools.item(i + 10).Visible = True







    Next







    '关闭文件







    Close #1







    '设置分隔条







    If iMaxLastfile <> 0 Then







        AABar.Bands("MenuFile").Tools.item(15).Visible = True







End If







三、在Form_Unload中添加如下代码,将打开文件记录写入配置文件。







    Open App.Path & "\LastFile.ini" For Output As #1







    Dim i As Integer







    Write #1, iMaxLastfile‘写入历史文件总数







    For i = 0 To iMaxLastfile - 1







        Write #1, strLastfile(i)‘写入历史文件路径







    Next







    Close #1







四、在需要更新菜单中文件历史记录的地方使用下面函数(如:打开一个文件,新建并打开一个文件等)







Private Sub UpdateLastFile(ByVal strPath As String)







    On Error GoTo SaveErr:







    Dim strDuan As String







    strDuan = strPath







    '判断要添加的文件是否时列表中的第一个文件







    If strDuan <> AABar.Bands("MenuFile").Tools.item(11).Caption Then







        '将列表中的文件依次下移一位,空出第一位







        Dim i As Integer







        For i = 3 To 1 Step -1







            strLastfile(i) = strLastfile(i - 1)







            AABar.Bands("MenuFile").Tools.item(11 + i).Caption = AABar.Bands("MenuFile").Tools.item(10 + i).Caption







        Next







        '将头一位设置为当前操作的文件路径







        strLastfile(0) = strDuan







        AABar.Bands("MenuFile").Tools.item(11).Caption = strDuan







        '如果列表文件数小于最大文件数则加一







        If iMaxLastfile < 4 Then







            iMaxLastfile = iMaxLastfile + 1







        End If







        '设置新移动的列表项可见







        AABar.Bands("MenuFile").Tools.item(iMaxLastfile + 10).Visible = True







    End If







    '如果列表不为空则下方的分隔条可见







    If iMaxLastfile <> 0 Then







        AABar.Bands("MenuFile").Tools.item(15).Visible = True







    Else







        AABar.Bands("MenuFile").Tools.item(15).Visible = False







    End If







    Exit Sub







SaveErr:







    dbEncrypt.SaveError "MDIForm1-UpdateLastFile"







End Sub







五、单击文件历史记录时调用如下函数。







Private Sub MenuLastfile(ByVal strName As String, Index As Integer)







    On Error GoTo SaveErr:







    '如果文件已不存在则提示







    If Dir(strName) = "" Then







        MsgBox "文件不存在,请确认后再次打开!", vbOKOnly + vbInformation, "打开文件"







        Exit Sub







    End If







    '设置当前打开文件为列表中的选择文件







    strConnection = strName







    '**************重新设置历史文件列表顺序*****************







    Dim i As Integer







    For i = Index To 12 Step –1 '把列表中选择文件的位置之上的文件依次下移







        strLastfile(i - 11) = strLastfile(i - 12)







     AABar.Bands("MenuFile").Tools.item(i).Caption= AABar.Bands("MenuFile").Tools.item(i - 1).Caption







    Next







    strLastfile(0) = strName







    '将选择的文件的放在列表中的首位







    AABar.Bands("MenuFile").Tools.item(11).Caption = strConnection







    CloseWnd‘自定义过程,用于关闭系统中打开的除MDI窗口外的所有窗口







  







 







    strConnection = strName







    Me.Caption = "水利造价管理系统" & "-" & strConnection







    ShowMenu







    Exit Sub







SaveErr:







    dbEncrypt.SaveError "MDIForm1-MenuLastFile"







End Sub







Private Sub CloseWnd()







    On Error GoTo SaveErr:







    Dim i As Integer







    For i = Forms.count - 1 To 1 Step -1







        If Forms(i).Name <> "FrmDaoHang1" And Forms(i).Name <> "FrmDaoHang2" And Forms(i).Name <> "FrmToolSearch" Then







            Unload Forms(i) '关闭到倒数第二个窗体







        End If







    Next







    Exit Sub







SaveErr:







    dbEncrypt.SaveError "MDIForm1-CloseWnd"







End Sub

About this Entry

This page contains a single entry by Sky published on September 14, 2005 2:53 PM.

开发手记(十)——在VB中解压缩ACCESS数据库文件 was the previous entry in this blog.

开发手记(十)——在VB中解压缩ACCESS数据库文件 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.