如果你要求的是弹出用户选择路径的对话框可以用api调用系统,不需要自己画 Public 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 Public Const NOERROR = 0 Public Const BIF_RETURNONLYFSDIRS = &H1 Public Const BIF_DONTGOBELOWDOMAIN = &H2 Public Const BIF_STATUSTEXT = &H4 Public Const BIF_RETURNFSANCESTORS = &H8 Public Const BIF_BROWSEFORCOMPUTER = &H1000 Public Const BIF_BROWSEFORPRINTER = &H2000 Public Declare Function SHBrowseForFolder Lib "Shell32.dll" Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long Public Declare Function SHGetPathFromIDList Lib "Shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long Public Declare Function SHGetSpecialFolderLocation Lib "Shell32.dll" (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As ITEMIDLIST) As Long Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As LongPublic Function ShowFolder() As String Dim bi As BROWSEINFO Dim t As Long Dim FolderPath As String Dim rtn&, pidl&, path$, pos%bi.hOwner = Me.hWnd bi.lpszTitle = "选择目录..." bi.ulFlags = BIF_RETURNONLYFSDIRS pidl& = SHBrowseForFolder(bi)
path = Space(512) t = SHGetPathFromIDList(ByVal pidl&, ByVal path) pos% = InStr(path$, Chr$(0)) FolderPath = Left(path$, pos - 1) ShowFolder = FolderPath End Function
上面有点乱,重发例子: 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 Const BIF_RETURNONLYFSDIRS = 1 Const MAX_PATH = 260 Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long) Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long 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 Sub Form_Load() Dim iNull As Integer, lpIDList As Long, lResult As Long Dim sPath As String, udtBI As BrowseInfo With udtBI 'Set the owner window .hWndOwner = Me.hWnd 'lstrcat appends the two strings and returns the memory address .lpszTitle = lstrcat("C:\", "") 'Return only if the user selected a directory .ulFlags = BIF_RETURNONLYFSDIRS End With 'Show the 'Browse for folder' dialog lpIDList = SHBrowseForFolder(udtBI) If lpIDList Then sPath = String$(MAX_PATH, 0) 'Get the path from the IDList SHGetPathFromIDList lpIDList, sPath 'free the block of memory CoTaskMemFree lpIDList iNull = InStr(sPath, vbNullChar) If iNull Then sPath = Left$(sPath, iNull - 1) End If End If MsgBox sPath End Sub
'下面只用了shell32.dll的函数,可以保证在所有win32系统中运行Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As ITEMIDLIST) As Long Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long 'ITEMIDLISTPublic Type BROWSEINFO 'bi 'lfont As String 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 TypeType SHITEMID 'mkid cb As Long 'Size of the ID (including cb itself) abID As Byte 'The item ID (variable length) End Type Type ITEMIDLIST 'idl mkid As SHITEMID End Type Public Const BIF_RETURNONLYFSDIRS = &H1' Does not include network folders below the domain level in the tree view control. ' For starting the Find Computer Public Const BIF_DONTGOBELOWDOMAIN = &H2' Includes a status area in the dialog box. The callback function can set ' the status text by sending messages to the dialog box. Public Const BIF_STATUSTEXT = &H4' Only returns file system ancestors. If the user selects anything other ' than a file system ancestor, the OK button is grayed. Public Const BIF_RETURNFSANCESTORS = &H8' Only returns computers. If the user selects anything other ' than a computer, the OK button is grayed. Public Const BIF_BROWSEFORCOMPUTER = &H1000' Only returns (network) printers. If the user selects anything other ' than a printer, the OK button is grayed. Public Const BIF_BROWSEFORPRINTER = &H2000Public Const FO_MOVE As Long = &H1 Public Const FO_COPY As Long = &H2 Public Const FO_DELETE As Long = &H3 Public Const FO_RENAME As Long = &H4 Public Const FOF_MULTIDESTFILES As Long = &H1 Public Const FOF_CONFIRMMOUSE As Long = &H2 Public Const FOF_SILENT As Long = &H4 Public Const FOF_RENAMEONCOLLISION As Long = &H8 Public Const FOF_NOCONFIRMATION As Long = &H10 Public Const FOF_WANTMAPPINGHANDLE As Long = &H20 Public Const FOF_CREATEPROGRESSDLG As Long = &H0 Public Const FOF_ALLOWUNDO As Long = &H40 Public Const FOF_FILESONLY As Long = &H80 Public Const FOF_SIMPLEPROGRESS As Long = &H100 Public Const FOF_NOCONFIRMMKDIR As Long = &H200Type SHFILEOPSTRUCT hwnd As Long wFunc As Long pFrom As String pTo As String fFlags As Long fAnyOperationsAborted As Long hNameMappings As Long lpszProgressTitle As String End Type Public Function SelectPath(ByVal Control As Form, ByVal Title As String) As String Dim bi As BROWSEINFO Dim idl As ITEMIDLIST Dim rtn&, pidl&, Path$, pos% Dim T Dim SpecOut, SpecIn As String
bi.hOwner = Control.hwnd 'centres the dialog on the screen
bi.lpszTitle = Title '"Browsing is limited to: " & Option1(CurOptIdx%).Caption ' set the type of folder to return ' play with these option constants to see what can be returned bi.ulFlags = BIF_RETURNONLYFSDIRS 'the type of folder(s) to return pidl& = SHBrowseForFolder(bi) 'show the dialog box
Path = Space(512) 'sets the maximum characters T = SHGetPathFromIDList(ByVal pidl&, ByVal Path) 'gets the selected path pos% = InStr(Path$, Chr$(0)) 'extracts the path from the string SpecIn = Left(Path$, pos - 1) 'sets the extracted path to SpecIn If Right$(SpecIn, 1) = "\" Then 'makes sure that "\" is at the end of the path SpecOut = SpecIn 'if so then, do nothing Else 'otherwise SpecOut = SpecIn + "\" 'add the "\" to the end of the path End If
If SpecOut = "\" Then Exit Function SelectPath = SpecOut End Function
Public 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
Public Const NOERROR = 0
Public Const BIF_RETURNONLYFSDIRS = &H1
Public Const BIF_DONTGOBELOWDOMAIN = &H2
Public Const BIF_STATUSTEXT = &H4
Public Const BIF_RETURNFSANCESTORS = &H8
Public Const BIF_BROWSEFORCOMPUTER = &H1000
Public Const BIF_BROWSEFORPRINTER = &H2000
Public Declare Function SHBrowseForFolder Lib "Shell32.dll" Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
Public Declare Function SHGetPathFromIDList Lib "Shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Public Declare Function SHGetSpecialFolderLocation Lib "Shell32.dll" (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As ITEMIDLIST) As Long
Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As LongPublic Function ShowFolder() As String
Dim bi As BROWSEINFO
Dim t As Long
Dim FolderPath As String
Dim rtn&, pidl&, path$, pos%bi.hOwner = Me.hWnd
bi.lpszTitle = "选择目录..."
bi.ulFlags = BIF_RETURNONLYFSDIRS
pidl& = SHBrowseForFolder(bi)
path = Space(512)
t = SHGetPathFromIDList(ByVal pidl&, ByVal path)
pos% = InStr(path$, Chr$(0))
FolderPath = Left(path$, pos - 1)
ShowFolder = FolderPath
End Function
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
Const BIF_RETURNONLYFSDIRS = 1
Const MAX_PATH = 260
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
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 Sub Form_Load()
Dim iNull As Integer, lpIDList As Long, lResult As Long
Dim sPath As String, udtBI As BrowseInfo With udtBI
'Set the owner window
.hWndOwner = Me.hWnd
'lstrcat appends the two strings and returns the memory address
.lpszTitle = lstrcat("C:\", "")
'Return only if the user selected a directory
.ulFlags = BIF_RETURNONLYFSDIRS
End With 'Show the 'Browse for folder' dialog
lpIDList = SHBrowseForFolder(udtBI)
If lpIDList Then
sPath = String$(MAX_PATH, 0)
'Get the path from the IDList
SHGetPathFromIDList lpIDList, sPath
'free the block of memory
CoTaskMemFree lpIDList
iNull = InStr(sPath, vbNullChar)
If iNull Then
sPath = Left$(sPath, iNull - 1)
End If
End If MsgBox sPath
End Sub
Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As ITEMIDLIST) As Long
Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long 'ITEMIDLISTPublic Type BROWSEINFO 'bi
'lfont As String
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 TypeType SHITEMID 'mkid
cb As Long 'Size of the ID (including cb itself)
abID As Byte 'The item ID (variable length)
End Type
Type ITEMIDLIST 'idl
mkid As SHITEMID
End Type
Public Const BIF_RETURNONLYFSDIRS = &H1' Does not include network folders below the domain level in the tree view control.
' For starting the Find Computer
Public Const BIF_DONTGOBELOWDOMAIN = &H2' Includes a status area in the dialog box. The callback function can set
' the status text by sending messages to the dialog box.
Public Const BIF_STATUSTEXT = &H4' Only returns file system ancestors. If the user selects anything other
' than a file system ancestor, the OK button is grayed.
Public Const BIF_RETURNFSANCESTORS = &H8' Only returns computers. If the user selects anything other
' than a computer, the OK button is grayed.
Public Const BIF_BROWSEFORCOMPUTER = &H1000' Only returns (network) printers. If the user selects anything other
' than a printer, the OK button is grayed.
Public Const BIF_BROWSEFORPRINTER = &H2000Public Const FO_MOVE As Long = &H1
Public Const FO_COPY As Long = &H2
Public Const FO_DELETE As Long = &H3
Public Const FO_RENAME As Long = &H4
Public Const FOF_MULTIDESTFILES As Long = &H1
Public Const FOF_CONFIRMMOUSE As Long = &H2
Public Const FOF_SILENT As Long = &H4
Public Const FOF_RENAMEONCOLLISION As Long = &H8
Public Const FOF_NOCONFIRMATION As Long = &H10
Public Const FOF_WANTMAPPINGHANDLE As Long = &H20
Public Const FOF_CREATEPROGRESSDLG As Long = &H0
Public Const FOF_ALLOWUNDO As Long = &H40
Public Const FOF_FILESONLY As Long = &H80
Public Const FOF_SIMPLEPROGRESS As Long = &H100
Public Const FOF_NOCONFIRMMKDIR As Long = &H200Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Long
fAnyOperationsAborted As Long
hNameMappings As Long
lpszProgressTitle As String
End Type
Public Function SelectPath(ByVal Control As Form, ByVal Title As String) As String
Dim bi As BROWSEINFO
Dim idl As ITEMIDLIST
Dim rtn&, pidl&, Path$, pos%
Dim T
Dim SpecOut, SpecIn As String
bi.hOwner = Control.hwnd 'centres the dialog on the screen
bi.lpszTitle = Title '"Browsing is limited to: " & Option1(CurOptIdx%).Caption
' set the type of folder to return
' play with these option constants to see what can be returned
bi.ulFlags = BIF_RETURNONLYFSDIRS 'the type of folder(s) to return
pidl& = SHBrowseForFolder(bi) 'show the dialog box
Path = Space(512) 'sets the maximum characters
T = SHGetPathFromIDList(ByVal pidl&, ByVal Path) 'gets the selected path pos% = InStr(Path$, Chr$(0)) 'extracts the path from the string
SpecIn = Left(Path$, pos - 1) 'sets the extracted path to SpecIn If Right$(SpecIn, 1) = "\" Then 'makes sure that "\" is at the end of the path
SpecOut = SpecIn 'if so then, do nothing
Else 'otherwise
SpecOut = SpecIn + "\" 'add the "\" to the end of the path
End If
If SpecOut = "\" Then Exit Function
SelectPath = SpecOut
End Function