在这里了,
你自己看看吧。VERSION 5.00
Begin VB.Form Form1
Caption = "测试调用“浏览文件夹”对话框"
ClientHeight = 3195
ClientLeft = 60
ClientTop = 345
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3195
ScaleWidth = 4680
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command1
Caption = "打 开"
Height = 495
Left = 1800
TabIndex = 0
Top = 1320
Width = 1215
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
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
Option Explicit
Private Sub Command1_Click()
Dim tmp As BrowseInfo
Dim l As Long
Dim str As String * 255
tmp.hwndOwner = Me.hWnd
l = SHBrowseForFolder(tmp)
' MsgBox str(l)
Call SHGetPathFromIDList(l, str)
MsgBox str
End Sub
'l得到的是函数的返回值,数字,不是文件夹的名称!
》》》》》》》》》》》》》》》》
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 Declare Function lstrcat Lib "kernel32" _
Alias "lstrcatA" (ByVal lpString1 As String, ByVal _
lpString2 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 Command1_Click()
'显示对话框
Dim lpIDList As Long 'Declare Varibles
Dim sBuffer As String
Dim szTitle As String
Dim tBrowseInfo As BrowseInfo
szTitle = "See it?"
With tBrowseInfo
.hWndOwner = Me.hWnd 'Owner Form
.lpszTitle = lstrcat(szTitle, "")
.ulFlags = BIF_RETURNONLYFSDIRS + BIF_DONTGOBELOWDOMAIN
End With
lpIDList = SHBrowseForFolder(tBrowseInfo)
If (lpIDList) Then
sBuffer = Space(MAX_PATH)
SHGetPathFromIDList lpIDList, sBuffer
sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
MsgBox sBuffer
End If
#If UNICODE Then
Public Declare Function SHGetPathFromIDList Lib "Shell32" Alias "SHGetPathFromIDListW" (ByVal pidl As Long, ByVal szPath As Long) As Long
#Else
Public Declare Function SHGetPathFromIDList Lib "Shell32" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal szPath As String) As Long
#End If
Public Declare Function SHGetSpecialFolderLocation Lib "Shell32" (ByVal hwndOwner As Long, ByVal nFolder As Integer, ppidl As Long) As Long
Public Declare Function SHGetSpecialFolderLocation2 Lib "Shell32" Alias "SHGetSpecialFolderLocation" (ByVal hwndOwner As Long, ByVal nFolder As Integer, ppidl As ItemIDList) As Long
Public Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (lpBrowseInfo As BrowseInfo) As Long
Type SHItemID 'mkid
cb As Long 'Size of the ID (including cb itself)
abID As Byte 'The item ID (variable length)
End Type
' The ITEMIDLIST structure defines an element in an item identifier list
' (the only member of this structure is an SHITEMID structure). An item
' identifier list consists of one or more consecutive ITEMIDLIST structures
' packed on byte boundaries, followed by a 16-bit zero value. An application
' can walk a list of item identifiers by examining the size specified in each
' SHITEMID structure and stopping when it finds a size of zero. A pointer
' to an item identifier list, is sometimes called a PIDL (pronounced piddle)
Type ItemIDList 'idl
mkid As SHItemID
End Type
' Contains parameters for the the SHBrowseForFolder function and receives
' information about the folder selected by the user.
Public Type BrowseInfo 'bi
' Handle of the owner window for the dialog box.
hOwner As Long
' Pointer to an item identifier list (an ITEMIDLIST structure) specifying the location
' of the "root" folder to browse from. Only the specified folder and its subfolders
' appear in the dialog box. This member can be NULL, and in that case, the
' name space root (the desktop folder) is used.
pidlRoot As Long
' Pointer to a buffer that receives the display name of the folder selected by the
' user. The size of this buffer is assumed to be MAX_PATH bytes.
pszDisplayName As String
' Pointer to a null-terminated string that is displayed above the tree view control
' in the dialog box. This string can be used to specify instructions to the user.
lpszTitle As String
' Value specifying the types of folders to be listed in the dialog box as well as
' other options. This member can include zero or more of the following values below.
ulFlags As Long
' Address an application-defined function that the dialog box calls when events
' occur. For more information, see the description of the BrowseCallbackProc
' function. This member can be NULL.
lpfn As Long
' Application-defined value that the dialog box passes to the callback function
' (if one is specified).
lParam As Long
' Variable that receives the image associated with the selected folder. The image
' is specified as an index to the system image list.
iImage As Long
End Type
Public Function BrowseForFolder(ByVal hWnd As Long, ByVal Title As String, ByVal flags As BROWSEFORFOLDERFLAGS, ByVal BrowseSpecial As Boolean, ByVal SpecialFolderFlag As SHELLFOLDERS, lpstrBuffer As String, Optional ByVal lpCallbackProc As Long = 0, Optional lpData As Long = 0) As Long
Dim bi As BrowseInfo
Dim idl As ItemIDList
Dim rtn As Long, pidl As Long, Path As String
' the calling app
bi.hOwner = hWnd
' set the folder to limit the browse to in the dialog
' if CurOptIdx% = 0 (Default Browse), bi.pidlRoot would then be Null
If BrowseSpecial Then
rtn = SHGetSpecialFolderLocation2(hWnd, SpecialFolderFlag, idl)
bi.pidlRoot = idl.mkid.cb
End If
If lpCallbackProc <> 0 Then
bi.lpfn = lpCallbackProc
bi.lParam = lpData
End If
' set the banner text
bi.lpszTitle = Title
' set the type of folder to return
' play with these option constants to see what can be returned
bi.ulFlags = flags
' show the browse folder dialog
pidl = SHBrowseForFolder(bi)