Private Type BrowseInfo
lngHwnd As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As Long
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End TypePrivate Const BIF_RETURNONLYFSDIRS = 1
Private Const MAX_PATH = 260Private 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 LongPrivate Function BrowseForFolder(ByVal lngHwnd As Long, ByVal strPrompt As String) As String On Error GoTo ehBrowseForFolder 'Trap for errors Dim intNull As Integer
Dim lngIDList As Long, lngResult As Long
Dim strPath As String
Dim udtBI As BrowseInfo 'Set API properties (housed in a UDT)
With udtBI
.lngHwnd = lngHwnd
.lpszTitle = lstrcat(strPrompt, "")
.ulFlags = BIF_RETURNONLYFSDIRS
End With 'Display the browse folder...
lngIDList = SHBrowseForFolder(udtBI) If lngIDList <> 0 Then
'Create string of nulls so it will fill in with the path
strPath = String(MAX_PATH, 0) 'Retrieves the path selected, places in the null
'character filled string
lngResult = SHGetPathFromIDList(lngIDList, strPath) 'Frees memory
Call CoTaskMemFree(lngIDList) 'Find the first instance of a null character,
'so we can get just the path
intNull = InStr(strPath, vbNullChar)
'Greater than 0 means the path exists...
If intNull > 0 Then
'Set the value
strPath = Left(strPath, intNull - 1)
End If
End If 'Return the path name
BrowseForFolder = strPath
Exit Function 'AbortehBrowseForFolder: 'Return no value
BrowseForFolder = EmptyEnd FunctionPrivate Sub Command1_Click()
aa = BrowseForFolder(Me.hWnd, "")
MsgBox aa
End Sub
lngHwnd As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As Long
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End TypePrivate Const BIF_RETURNONLYFSDIRS = 1
Private Const MAX_PATH = 260Private 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 LongPrivate Function BrowseForFolder(ByVal lngHwnd As Long, ByVal strPrompt As String) As String On Error GoTo ehBrowseForFolder 'Trap for errors Dim intNull As Integer
Dim lngIDList As Long, lngResult As Long
Dim strPath As String
Dim udtBI As BrowseInfo 'Set API properties (housed in a UDT)
With udtBI
.lngHwnd = lngHwnd
.lpszTitle = lstrcat(strPrompt, "")
.ulFlags = BIF_RETURNONLYFSDIRS
End With 'Display the browse folder...
lngIDList = SHBrowseForFolder(udtBI) If lngIDList <> 0 Then
'Create string of nulls so it will fill in with the path
strPath = String(MAX_PATH, 0) 'Retrieves the path selected, places in the null
'character filled string
lngResult = SHGetPathFromIDList(lngIDList, strPath) 'Frees memory
Call CoTaskMemFree(lngIDList) 'Find the first instance of a null character,
'so we can get just the path
intNull = InStr(strPath, vbNullChar)
'Greater than 0 means the path exists...
If intNull > 0 Then
'Set the value
strPath = Left(strPath, intNull - 1)
End If
End If 'Return the path name
BrowseForFolder = strPath
Exit Function 'AbortehBrowseForFolder: 'Return no value
BrowseForFolder = EmptyEnd FunctionPrivate Sub Command1_Click()
aa = BrowseForFolder(Me.hWnd, "")
MsgBox aa
End Sub
解决方案 »
- 在VB6中如何才能识别和使用USB串口?
- VB中如何删除某文件中的特定字符
- 这条语句是什么意思?
- 救命啊,我在客户这里,软件出现这个错误是为什么啊?
- 请高手给个算法!
- 请教高手,用什么函数或方法能得到已知文件夹下的未知文件名啊?谢谢回答!! (zjn12 )
- 请问,如何实现自动映射局域网内的指定机器?
- 为什么我用activeskin后~~ 不能放其他控件的~ 例如Shape 还有 media player这些控件啊
- 用什么API可以实现SendKeys的功能,请附例子,谢谢。
- vb新手求助,高人们帮忙给看看谢谢1
- 请问怎样激活一个非活动窗口?
- 我学vb开一年了,但是对API 和怎样编控件一点都不懂,请各位高手介绍这方面的知识和给出例子 ,一定给分!!
'写在窗体中
Private Sub Cmd1_Click()
Dim TempStr As string
If ShowDir(me.hWnd, TempStr, 提示文字) Then
'成功!路径在TempStr
Else
'不成功
End If
End Sub'写在模块中
Public Declare Function SHBrowseForFolder _
Lib "shell32.dll" Alias "SHBrowseForFolderA" _
(lpBrowseInfo As BROWSEINFO) As Long
Public Declare Function SHGetPathFromIDList _
Lib "shell32.dll" _
(ByVal pidl As Long, _
pszPath As String) As Long
Public Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlage As Long
lpfn As Long
lparam As Long
iImage As Long
End TypePublic Function ShowDir(MehWnd As Long, _
DirPath As String, _
Optional Title As String = "请选择文件夹:", _
Optional flage As Long = &H1, _
Optional DirID As Long) As Long
Dim BI As BROWSEINFO
Dim TempID As Long
Dim TempStr As String
TempStr = String$(255, Chr$(0))
With BI
.hOwner = MehWnd
.pidlRoot = 0
.lpszTitle = Title + Chr$(0)
.ulFlage = flage
End With
TempID = SHBrowseForFolder(BI)
DirID = TempID
If SHGetPathFromIDList(ByVal TempID, ByVal TempStr) Then
DirPath = Left$(TempStr, InStr(TempStr, Chr$(0)) - 1)
ShowDir = -1
Else
ShowDir = 0
End If
End Function
If 你不知道怎样给分 Then 去看:http://www.csdn.net/expert/faq.shtm
http://ygyuan.3322.net/有源程序,可以预认默认目录.