Visual Basic 提供了一个标准的注册表位置以存储创建于 Visual Basic 的应用程序的程序信息,同时VB也提供了几个标准的语句或函数来处理存储在应用程序注册位置的程序设置如GetSetting函数、SaveSetting 语句、DeleteSetting 语句。本例的历史记录(是最近打开文件的记录,例如WORD软件)就是用这几个函数实现的。 为了学习方便,提供的源码已经作了详细的中文注释,看看源码框中的代码: '----------------------------------------- ' 使用历史记录的例子 '----------------------------------------- ' 洪恩在线 求知无限 '----------------------------------------- 'Visual Basic 提供了一个标准的注册表位置以存储创建于 Visual Basic 的应用程序的程序信息: 'HKEY_CURRENT_USER\Software\VB and VBA Program Settings\appname\section\key 'Visual Basic 也提供了四个语句或函数来处理存储在应用程序注册位置的程序设置值。 '函数或语句 描述 'GetSetting 函数 检索注册表设置值。 'SaveSetting 语句 保存或创建注册表设置值。 'GetAllSettings 函数 返回一个包含多项注册表设置值的数组。 'DeleteSetting 语句 删除注册表设置值。 '----------------------------------------- '本例中使用了除GetAllSettings 函数之外的几个函数或语句 '----------------------------------------- '如果仅检索一项注册表设置值,通过以下的语法使用 GetSetting 函数: 'GetSetting(appname, section, key[, default]) '各参数的意义: appname, section, key, Value ' 应用程序名称,表的名称,表项的名称,表项的值 '注:这些名称是我们自己定义的,而不是系统默认值 '本例中注册表的结构如下所示: '应用程序名称: Demo '表的名称: Rfile '---------------表项名----------表项的值--- '表项1: FirstFile 0 '表项2: File0 第一个历史文件的路径 '表项2: File1 第二个历史文件的路径 '表项2: File2 第三个历史文件的路径 '表项2: File3 第四个历史文件的路径 '----------------------------------------- '按以下语法使用 SaveSetting 语句: 'SaveSetting appname, section, Key, Value '通过以下语法使用 DeleteSetting 语句: 'DeleteSetting(appname, section, key) '----------------------------------------- Option Explicit 'A_Name是表示应用程序名称的变量 Dim A_Name As String 'S_Name是表示表的名称的变量 Dim S_Name As String 'MaxRFiles设置最多可以记录历史文件的数量,我们自己可以更改 Const MaxRFiles = 4 '当“退出程序”按钮被按下时 Private Sub CmdExit_Click() Unload Me End Sub '当“清除历史记录”按钮被按下时 Private Sub CmdClear_Click() '调用ClearRecentFiles[清除历史记录的子过程] ClearRecentFiles End Sub '当主窗体加载时 Private Sub Form_Load() A_Name = "Demo" S_Name = "RFile" '调用ReadRecentFiles[读最近历史记录的子过程] ReadRecentFiles End Sub '当“退出”菜单项被点击时 Private Sub mExit_Click() Unload Me End Sub '当历史记录文件的菜单项被点击时 Private Sub mLastFile_Click(Index As Integer) '更新历史记录 UpdateRecentFiles Index End Sub '当“打开”菜单项被点击时 Private Sub mOpen_Click() Dim fIndex As Integer On Error Resume Next CommonDialog1.CancelError = True '如果在打开文件对话框中按下“取消”按钮,则进行错误响应 CommonDialog1.DialogTitle = "打开文件" CommonDialog1.FileName = "" CommonDialog1.Filter = "所有文件(*.*)|*.*" CommonDialog1.FilterIndex = 1 CommonDialog1.Flags = cdlOFNCreatePrompt + cdlOFNHideReadOnly CommonDialog1.ShowOpen If Err = cdlCancel Then '“取消”按钮按下时,进行错误处理 Else fIndex = InRecentFiles(CommonDialog1.FileName) If fIndex > MaxRFiles Then WriteRecentFiles CommonDialog1.FileName Else UpdateRecentFiles fIndex End If End If End Sub '将打开的文件路径写入注册表的子过程 Private Sub WriteRecentFiles(FileName As String) Dim fileptr As Integer If Len(Trim(FileName)) Then fileptr = Val(GetSetting(A_Name, S_Name, "FirstFile", "0")) fileptr = IIf(fileptr - 1 >= 0, fileptr - 1, MaxRFiles - 1) SaveSetting A_Name, S_Name, "FirstFile", fileptr & "" SaveSetting A_Name, S_Name, "File" & fileptr, FileName ReadRecentFiles End If End Sub '读最近历史文件的子过程 Private Sub ReadRecentFiles() Di |
|站长邮箱|小黑屋|手机版|Office中国/Access中国
( 粤ICP备10043721号-1 )
GMT+8, 2025-4-3 07:31 , Processed in 0.111524 second(s), 23 queries .
Powered by Discuz! X3.3
© 2001-2017 Comsenz Inc.