"VB中5种打开文件夹浏览框的方法总结"

版权申诉
0 下载量 173 浏览量 更新于2024-03-01 收藏 90KB DOC 举报
在这里我们使用另一种方法,而不是直接调用 BrowseForFolder 方法。我们可以创建一个 Shell 控件对象,并通过调用其 NameSpace 方法来实现文件夹的浏览。以下是示例代码: Private Sub Command2_Click() Dim sh As Object Set sh = CreateObject("Shell.Application") Set f = sh.BrowseForFolder(0, "选择文件夹", 0) Set fld = f.ParentFolder.ParseName(f.Title) MsgBox fld End Sub 第三种方法是使用 API 函数来实现文件夹的浏览。我们可以使用以下代码来实现: Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long Private Type BrowseInfo hWndOwner As Long pIDLRoot As Long pszDisplayName As Long lpszTitle As Long ulFlags As Long lpfnCallback As Long lParam As Long iImage As Long End Type Private Sub Command3_Click() Dim bi As BrowseInfo Dim pidl As Long Dim path As String With bi .hWndOwner = Me.hWnd .lpszTitle = lstrcat("选择文件夹", "") .ulFlags = &H1 End With pidl = SHBrowseForFolder(bi) If pidl Then path = Space$(260) SHGetPathFromIDList pidl, path path = Left$(path, InStr(path, vbNullChar) - 1) MsgBox path End If End Sub 另外两种方法是使用自定义对话框和使用 FileSystemObject 对象来实现文件夹的浏览。无论使用哪种方法,都可以轻松地实现在 vb 中打开文件夹浏览框。希望对读者有所帮助。