在VB中直接选取文件路径直接使用控件就可以实现
选取文件夹路径可以采用下述办法。
添加模块文件直接进行调用。模块内容代码
'---------------------------------------------------------------------------------------' Module : modulefile' Author : ROVAST' Date : 2014-4-22' Purpose : 文件相关 *** 作模块' Function : 1、选取文件夹'---------------------------------------------------------------------------------------Option ExplicitPrivate Type browseInfo hWndOwner As Long pIDLRoot As Long pszdisplayname As Long lpsz@R_301_5979@ As Long ulFlags As Long lpfnCallback As Long lParam As Long iImage As LongEnd TypeConst BIF_RETURNONLYFSDirs = 1Const BIF_NEWDIALOGSTYLE = &H40Const BIF_EDITBox = &H10Const BIF_USENEWUI = BIF_NEWDIALOGSTYLE Or BIF_EDITBoxConst 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 LongPrivate Declare Function SHbrowseForFolder lib "shell32" (lpbi As browseInfo) As LongPrivate Declare Function SHGetPathFromIDList lib "shell32" (ByVal pIDList As Long,ByVal lpBuffer As String) As Long'---------------------------------------------------------------------------------------' Procedure : browseForFolder' Author : ROVAST' Date : 2014-4-22' Purpose : 选取文件夹(不含新建文件夹指令) 返回browseForFolder'---------------------------------------------------------------------------------------'Public Function browseForFolder(Optional s@R_301_5979@ As String = "请选择文件夹") As String Dim iNull As Integer,lpIDList As Long,lResult As Long Dim sPath As String,udtBI As browseInfo With udtBI .hWndOwner = 0 ' Me.hWnd .lpsz@R_301_5979@ = lstrcat(s@R_301_5979@,"") .ulFlags = BIF_RETURNONLYFSDirs Or BIF_USENEWUI End With lpIDList = SHbrowseForFolder(udtBI) If lpIDList Then sPath = String$(MAX_PATH,0) SHGetPathFromIDList lpIDList,sPath CoTaskMemFree lpIDList iNull = InStr(sPath,vbNullChar) If iNull Then sPath = left$(sPath,iNull - 1) End If End If browseForFolder = sPathEnd Function'---------------------------------------------------------------------------------------' Procedure : browseForFolder1' Author : ROVAST' Date : 2014-4-22' Purpose : 选取文件夹路径(含新建文件夹) 返回browseForFolder1 字符串'---------------------------------------------------------------------------------------'Public Function browseForFolder1(Optional s@R_301_5979@ As String = "请选择文件夹") As String Dim iNull As Integer,"") .ulFlags = BIF_RETURNONLYFSDirs End With lpIDList = SHbrowseForFolder(udtBI) If lpIDList Then sPath = String$(MAX_PATH,iNull - 1) End If End If browseForFolder1 = sPathEnd Function
在主窗体中可以插入按钮。添加下述代码,其中前一个没有新建文件夹功能,后一个有新建文件夹功能
Option ExplicitPrivate Sub Command1_Click() Dim path1 As String path1 = browseForFolder MsgBox path1End SubPrivate Sub Command2_Click() Dim path As String path = browseForFolder1 MsgBox pathEnd Sub
此方法也是摘自互联网,自己只是整理了一下,具体原理自己也不是很清楚。当然要是有人愿意分析更好了,现在由于时间原因,也只能是拿来主义了
参考地址:http://zhidao.baidu.com/question/333483648.html?qbl=relate_question_3&word=vb%20%D1%A1%D4%F1%CE%C4%BC%FE%BC%D0%B6%D4%BB%B0%BF%F2
总结以上是内存溢出为你收集整理的VB实现选取文件夹路径全部内容,希望文章能够帮你解决VB实现选取文件夹路径所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)