Private Declare Function SendMessageString Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Private Function GetAddressofFunction(Add As Long) As Long
GetAddressofFunction = Add
End Function
Public Function ChooseFolder(hWnd As Long, Message As String, Seleted As String, Optional RootFolder As EnumRootFolder = EnumRootFolder.fbfDrives, Optional ChooseWho As EnumChoose = EnumChoose.fbcFolders) As Boolean
'选择目录对话框
On Error GoTo er
Dim Nullpos As Integer
Dim lpIDList As Long
Dim res As Long
Dim sPath As String
Dim Binfo As BrowseInfo
Dim RootID As Long
mPath = Seleted
With Binfo
.hWndOwner = hWnd
.lpszTitle = Message
.ulFlags = BIF_STATUSTEXT Or ChooseWho
.pIDLRoot = RootFolder
.lpfnCallback = GetAddressofFunction(AddressOf BrowseCallbackProc)
If RootID <> 0 Then .pIDLRoot = RootID
End With
lpIDList = SHBrowseForFolder(Binfo)
ChooseFolder = IIf(lpIDList <> 0, True, False)
If lpIDList <> 0 Then
sPath = String(260, Chr(0))
res = SHGetPathFromIDList(lpIDList, sPath)
Nullpos = InStr(sPath, vbNullChar)
If Nullpos Then
Seleted = Left(sPath, Nullpos - 1)
End If
End If
Exit Function
er:
ChooseFolder = False
End Function
Public Function BrowseCallbackProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal lp As Long, ByVal pData As Long) As Long
Dim lpIDList As Long
Dim ret As Long
Dim sBuffer As String
On Error Resume Next
Select Case uMsg
Case BFFM_INITIALIZED: Call SendMessageString(hWnd, BFFM_SETSELECTION, 1, mPath)
Case BFFM_SELCHANGED
sBuffer = Space(260)
ret = SHGetPathFromIDList(lp, sBuffer)
If ret = 1 Then Call SendMessageString(hWnd, BFFM_SETSTATUSTEXT, 0, sBuffer)
End Select
BrowseCallbackProc = 0
End Function
Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias _
"SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
Private Declare Function SHGetSpecialFolderLocation Lib _
"shell32.dll" (ByVal hwndOwner As Long, ByVal nFolder _
As Long, pIdl As ITEMIDLIST) As Long
Private Declare Function SHGetFileInfo Lib "Shell32" Alias _
"SHGetFileInfoA" (ByVal pszPath As Any, ByVal _
dwFileAttributes As Long, psfi As SHFILEINFO, ByVal _
cbFileInfo As Long, ByVal uFlags As Long) As Long
Private Declare Function ShellAbout Lib "shell32.dll" Alias _
"ShellAboutA" (ByVal hwnd As Long, ByVal szApp As _
String, ByVal szOtherStuff As String, ByVal hIcon As Long) _
As Long
Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pIdl As Long, ByVal _
pszPath As String) As Long
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal pv As Long)
Const MAX_PATH = 260
Private Type SHITEMID
cb As Long
abID() As Byte
End Type
Private Type ITEMIDLIST
mkid As SHITEMID
End Type
Private Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
Private Type SHFILEINFO
hIcon As Long
iIcon As Long
dwAttributes As Long
szDisplayName As String * MAX_PATH
szTypeName As String * 80
End Type
Private Function GetFolderValue(wIdx As Integer) As Long
If wIdx < 2 Then
GetFolderValue = 0
ElseIf wIdx < 12 Then
GetFolderValue = wIdx
Else
GetFolderValue = wIdx + 4
End If
End Function
Private Sub Command1_Click()
Dim BI As BROWSEINFO
Dim nFolder As Long
Dim IDL As ITEMIDLIST
Dim pIdl As Long
Dim sPath As String
Dim SHFI As SHFILEINFO
Dim m_wCurOptIdx As Integer
Dim txtPath As String
Dim txtDisplayName As String
With BI
.hOwner = Me.hwnd
nFolder = GetFolderValue(m_wCurOptIdx)
If SHGetSpecialFolderLocation(ByVal Me.hwnd, ByVal nFolder, IDL) = NOERROR Then
.pidlRoot = IDL.mkid.cb
End If
.pszDisplayName = String$(MAX_PATH, 0)
.lpszTitle = "Browsing is limited to: "
.ulFlags = 0
End With
txtPath = ""
txtDisplayName = ""
pIdl = SHBrowseForFolder(BI)
If pIdl = 0 Then Exit Sub
sPath = String$(MAX_PATH, 0)
SHGetPathFromIDList ByVal pIdl, ByVal sPath