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("请选择文件夹", "") '标题 '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
CommonDialog的返回值不是包括路徑嗎?
A() = Split(Me.CommonDialog1.FileName, "\")
Me.Text1.Text = A(UBound(A()) - 1)
這個得到的是最底層的文件裌名稱
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("请选择文件夹", "") '标题
'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
正常的commondialog只能指定到一个问题然后点确定,在选择一个文件夹后就会进到这个文件夹中了,我想要的效果是选择文件夹之后,直接返回当前文件夹的路径,而不是通过选择的文件来找当前的文件夹。
ZOU_SEAFARER(国际海员)朋友的方法确实也能达到这个要求,不过设计书上要求使用的是commondialog,而不是shell。
还是要谢谢两位,我只是想知道是不是可以通过commondialog来实现这种功能,如果不能我就可以向客户反映这个情况了,再次谢谢帮忙的各位了.
指文件夹确定文件夹你就是说看commondialog能否直接选中文件夹了
'引用Microsoft Shell Controls And Automation Private shlShell As Shell32.Shell
Private shlFolder As Shell32.Folder
Private Const BIF_RETURNONLYFSDIRS = &H1Private Sub Command1_Click() If shlShell Is Nothing Then
Set shlShell = New Shell32.Shell
End If
Set shlFolder = shlShell.BrowseForFolder(Me.hWnd, "请选择路径", BIF_RETURNONLYFSDIRS) If Not shlFolder Is Nothing Then
MsgBox shlFolder.Items.Item.Path
End If
End Sub
———————————————————————————————————————
BrowseForFolder 还可以在以上 3 个参数后,加一个可选参数选择起始路径。不过它需要引用一组常数来表示特定的路径:
typedef enum {
ssfALTSTARTUP = 0x1d,
ssfAPPDATA = 0x1a,
ssfBITBUCKET = 0xa,
ssfCOMMONALTSTARTUP = 0x1e,
ssfCOMMONAPPDATA = 0x23,
ssfCOMMONDESKTOPDIR = 0x19,
ssfCOMMONFAVORITES = 0x1f,
ssfCOMMONPROGRAMS = 0x17,
ssfCOMMONSTARTMENU = 0x16,
ssfCOMMONSTARTUP = 0x18,
ssfCONTROLS = 0x3,
ssfCOOKIES = 0x21,
ssfDESKTOP = 0x0,
ssfDESKTOPDIRECTORY = 0x10,
ssfDRIVES = 0x11,
ssfFAVORITES = 0x6,
ssfFONTS = 0x14,
ssfHISTORY = 0x22,
ssfINTERNETCACHE = 0x20,
ssfLOCALAPPDATA = 0x1c,
ssfMYPICTURES = 0x27,
ssfNETHOOD = 0x13,
ssfNETWORK = 0x12,
ssfPERSONAL = 0x5,
ssfPRINTERS = 0x4,
ssfPRINTHOOD = 0x1b,
ssfPROFILE = 0x28,
ssfPROGRAMFILES = 0x26,
ssfPROGRAMS = 0x2,
ssfRECENT = 0x8,
ssfSENDTO = 0x9,
ssfSTARTMENU = 0xb,
ssfSTARTUP = 0x7,
ssfSYSTEM = 0x25,
ssfTEMPLATES = 0x15,
ssfWINDOWS = 0x24
} ShellSpecialFolderConstants;