VB 怎么制作“浏览”按扭,可以浏览电脑上的文件和文件夹,将浏览选择好后的地址显示在text1控制中。
现在可以了,,,,那是浏览的指定文件,,,,,,再做一个浏览的指定文件夹,只要浏览到文件夹就可以了...
现在可以了,,,,那是浏览的指定文件,,,,,,再做一个浏览的指定文件夹,只要浏览到文件夹就可以了
展开
2个回答
展开全部
给你提供几种方法,第一个如一楼那样,使用公用对话框,选择文件。第二你可以使用VB提供的驱动器列表,文件夹列表,文件列表来完成此操作,非常简单,如需代码可以联系我。第三可以通过API调用Windows的浏览文件夹对话框,给你如下代码,添加到标准模块里,需要时只要调用就行。
'这个模块利用Shell API函数弹出文件夹浏览窗口
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
Public Function SeleteFolder(ByVal frmX As Form)
'参数frmX为调用文件夹浏览窗口的窗体
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 txtDisplayNafrmx As String
With BI
.hOwner = frmX.hwnd
nFolder = GetFolderValue(m_wCurOptIdx)
If SHGetSpecialFolderLocation(ByVal frmX.hwnd, ByVal nFolder, IDL) = NOERROR Then
.pidlRoot = IDL.mkid.cb
End If
.pszDisplayName = String$(MAX_PATH, 0)
.lpszTitle = "请选择文件夹: "
.ulFlags = 0
End With
txtPath = ""
txtDisplayName = ""
pIdl = SHBrowseForFolder(BI)
If pIdl = 0 Then Exit Function
sPath = String$(MAX_PATH, 0)
SHGetPathFromIDList ByVal pIdl, ByVal sPath
txtPath = Left(sPath, InStr(sPath, vbNullChar) - 1)
'Debug.Print txtPath
'txtDisplayName = Left$(BI.pszDisplayName, _
InStr(BI.pszDisplayName, vbNullChar) - 1)
'SHGetFileInfo ByVal pIdl, 0&, SHFI, Len(SHFI), _
SHGFI_PIDL Or SHGFI_ICON Or SHGFI_SMALLICON
'SHGetFileInfo ByVal pIdl, 0&, SHFI, Len(SHFI), _
SHGFI_PIDL Or SHGFI_ICON
'CoTaskMemFree pIdl
SeleteFolder = txtPath
'MsgBox "你选择的文件夹是" + Chr(13) + Chr(10) + txtPath
End Function
'这个模块利用Shell API函数弹出文件夹浏览窗口
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
Public Function SeleteFolder(ByVal frmX As Form)
'参数frmX为调用文件夹浏览窗口的窗体
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 txtDisplayNafrmx As String
With BI
.hOwner = frmX.hwnd
nFolder = GetFolderValue(m_wCurOptIdx)
If SHGetSpecialFolderLocation(ByVal frmX.hwnd, ByVal nFolder, IDL) = NOERROR Then
.pidlRoot = IDL.mkid.cb
End If
.pszDisplayName = String$(MAX_PATH, 0)
.lpszTitle = "请选择文件夹: "
.ulFlags = 0
End With
txtPath = ""
txtDisplayName = ""
pIdl = SHBrowseForFolder(BI)
If pIdl = 0 Then Exit Function
sPath = String$(MAX_PATH, 0)
SHGetPathFromIDList ByVal pIdl, ByVal sPath
txtPath = Left(sPath, InStr(sPath, vbNullChar) - 1)
'Debug.Print txtPath
'txtDisplayName = Left$(BI.pszDisplayName, _
InStr(BI.pszDisplayName, vbNullChar) - 1)
'SHGetFileInfo ByVal pIdl, 0&, SHFI, Len(SHFI), _
SHGFI_PIDL Or SHGFI_ICON Or SHGFI_SMALLICON
'SHGetFileInfo ByVal pIdl, 0&, SHFI, Len(SHFI), _
SHGFI_PIDL Or SHGFI_ICON
'CoTaskMemFree pIdl
SeleteFolder = txtPath
'MsgBox "你选择的文件夹是" + Chr(13) + Chr(10) + txtPath
End Function
展开全部
在窗体上发一个CommonDialog控件,命名成cldOpen,
在放一个按钮,Caption设置为:浏览(&B) ,命名为cmdBrowse:
Private Sub cmdBrowse_Click()
Me.cdlOpen.Filter = "打开文件|*.*"
cdlOpen.Flags = cdlOFNPathMustExist
cdlOpen.DialogTitle = "浏览"
Me.cdlOpen.ShowOpen
Me.text1.Text = Me.cdlOpen.FileName
End Sub
在放一个按钮,Caption设置为:浏览(&B) ,命名为cmdBrowse:
Private Sub cmdBrowse_Click()
Me.cdlOpen.Filter = "打开文件|*.*"
cdlOpen.Flags = cdlOFNPathMustExist
cdlOpen.DialogTitle = "浏览"
Me.cdlOpen.ShowOpen
Me.text1.Text = Me.cdlOpen.FileName
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询