一个关于选择目录框的问题
我想在打开此框的时候
可以设置任意一个目录作为默认选择的节点
即它不是选择“我的电脑”而是比如c:\windows\system的一个目录call fl.ChooseFolder(Me.hwnd, "请选择一个作为数据存放的目录:", fbfDrives, fbcFolders)
'--------------------------------------------------------
Public Enum EnumRootFolder
fbfDeskTop = &H0
fbfPrograms = &H2
fbfControls = &H3
fbfPrinters = &H4
fbfPersonal = &H5
fbfFavorites = &H6
fbfStartup = &H7
fbfRecent = &H8
fbfSendTo = &H9
fbfBitbucket = &HA
fbfStartMenu = &HB
fbfDesktopDirectory = 16
fbfDrives = &H11
fbfNetWork = &H12
fbfNetHood = &H13
fbfFonts = &H14
fbfTemplates = &H15
End EnumPublic Enum EnumChoose
fbcFolders = &H1
fbcComputers = &H1000
fbcPrinters = &H2000
fbcEverything = &H4000
End EnumPublic Function ChooseFolder(hWnd As Long, Message As String, RootFolder As EnumRootFolder, ChooseWho As EnumChoose) As String
'选择目录对话框
On Error GoTo er
Dim Nullpos As Integer
Dim lpIDList As Long
Dim res As Long
Dim sPath As String
Dim Binfo As BrowseInfo
Dim RootID As Long
Binfo.hWndOwner = hWnd
Binfo.lpszTitle = lstrcat(Message, "")
Binfo.ulFlags = ChooseWho
Binfo.pIDLRoot = RootFolder
If RootID <> 0 Then Binfo.pIDLRoot = RootID
lpIDList = SHBrowseForFolder(Binfo)
If lpIDList <> 0 Then
sPath = String(260, Chr(0))
res = SHGetPathFromIDList(lpIDList, sPath)
Nullpos = InStr(sPath, vbNullChar)
If Nullpos <> 0 Then
sPath = Left(sPath, Nullpos - 1)
ChooseFolder = sPath
End If
End If
Exit Function
er:
ChooseFolder = ""
End Function
我想在打开此框的时候
可以设置任意一个目录作为默认选择的节点
即它不是选择“我的电脑”而是比如c:\windows\system的一个目录call fl.ChooseFolder(Me.hwnd, "请选择一个作为数据存放的目录:", fbfDrives, fbcFolders)
'--------------------------------------------------------
Public Enum EnumRootFolder
fbfDeskTop = &H0
fbfPrograms = &H2
fbfControls = &H3
fbfPrinters = &H4
fbfPersonal = &H5
fbfFavorites = &H6
fbfStartup = &H7
fbfRecent = &H8
fbfSendTo = &H9
fbfBitbucket = &HA
fbfStartMenu = &HB
fbfDesktopDirectory = 16
fbfDrives = &H11
fbfNetWork = &H12
fbfNetHood = &H13
fbfFonts = &H14
fbfTemplates = &H15
End EnumPublic Enum EnumChoose
fbcFolders = &H1
fbcComputers = &H1000
fbcPrinters = &H2000
fbcEverything = &H4000
End EnumPublic Function ChooseFolder(hWnd As Long, Message As String, RootFolder As EnumRootFolder, ChooseWho As EnumChoose) As String
'选择目录对话框
On Error GoTo er
Dim Nullpos As Integer
Dim lpIDList As Long
Dim res As Long
Dim sPath As String
Dim Binfo As BrowseInfo
Dim RootID As Long
Binfo.hWndOwner = hWnd
Binfo.lpszTitle = lstrcat(Message, "")
Binfo.ulFlags = ChooseWho
Binfo.pIDLRoot = RootFolder
If RootID <> 0 Then Binfo.pIDLRoot = RootID
lpIDList = SHBrowseForFolder(Binfo)
If lpIDList <> 0 Then
sPath = String(260, Chr(0))
res = SHGetPathFromIDList(lpIDList, sPath)
Nullpos = InStr(sPath, vbNullChar)
If Nullpos <> 0 Then
sPath = Left(sPath, Nullpos - 1)
ChooseFolder = sPath
End If
End If
Exit Function
er:
ChooseFolder = ""
End Function
解决方案 »
- VB中如何使窗体界面和窗体中的程序处理分开?
- 如何让VB只显示窗体内容,不显示title标题行
- 运行完毕一个BAT文件后,调用另一个BAT文件?
- 在线等待,关于VB的两个小问题!!!!
- 怎样编程把html网页设为桌面?
- Implements IObjectSafety是什么意思啊!
- 觅条形码打印控件!
- VB新手上http://www.experts-exchange.com的感受
- 如何把自己的程序贴到别人的程序上.20分不承敬意!
- 急!VB6 的DATA REPORT中不能对控件编程?
- 如何判断一个数组是否还没初始化?
- 一个问题挂两天都没人问,难道高手都去看欧锦赛了吗?真怀念有标准答案的年代!
-----------------------
用户类型未定义
为什么不用CommonDialog????两句就可以了。CommonDialog1.InitDir = "C:\PWIN98\SYSTEM"
CommonDialog1.ShowSave
但我发现有些软件就能达到
将下列代码复制在一个标准模块中,在程序中调用即可。Option ExplicitPrivate Const BIF_STATUSTEXT = &H4&
Private Const BIF_RETURNONLYFSDIRS = 1
Private Const BIF_DONTGOBELOWDOMAIN = 2
Private Const MAX_PATH = 260Private Const WM_USER = &H400
Private Const BFFM_INITIALIZED = 1
Private Const BFFM_SELCHANGED = 2
Private Const BFFM_SETSTATUSTEXT = (WM_USER + 100)
Private Const BFFM_SETSELECTION = (WM_USER + 102)Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam 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 Type BrowseInfo
hWndOwner As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As String
ulFlags As Long
lpfnCallback As Long
lParam As Long
iImage As Long
End TypePrivate m_CurrentDirectory As String'====================== 选择目录的函数 ========================================================================
'Owner ----------- 调用该函数的窗体
'Title ----------- 显示在目录选择对话框上的标题 (可选)
'StarDir --------- 默认打开的目录 (可选)
Public Function BrowseForFolder(Owner As Form, Optional Title As String, Optional StartDir As String) As String Dim lpIDList As Long
Dim szTitle As String
Dim sBuffer As String
Dim tBrowseInfo As BrowseInfo
m_CurrentDirectory = StartDir & vbNullChar szTitle = Title
With tBrowseInfo
.hWndOwner = Owner.hWnd
.pIDLRoot = 0
.lpszTitle = IIf(Title <> "", Title, "路径选择:")
.ulFlags = BIF_RETURNONLYFSDIRS + BIF_DONTGOBELOWDOMAIN + BIF_STATUSTEXT
If StartDir <> "" Then
.lpfnCallback = GetAddressofFunction(AddressOf BrowseCallbackProc)
End If
End With lpIDList = SHBrowseForFolder(tBrowseInfo)
If (lpIDList) Then
sBuffer = Space(MAX_PATH)
SHGetPathFromIDList lpIDList, sBuffer
sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
BrowseForFolder = sBuffer
Else
BrowseForFolder = ""
End If
End Function
Private Function BrowseCallbackProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal lp As Long, ByVal pData As Long) As Long
Dim lpIDList As Long
Dim ret As Long
Dim sBuffer As String
On Error Resume Next
Select Case uMsg
Case BFFM_INITIALIZED
Call SendMessage(hWnd, BFFM_SETSELECTION, 1, m_CurrentDirectory)
Case BFFM_SELCHANGED
sBuffer = Space(MAX_PATH)
ret = SHGetPathFromIDList(lp, sBuffer)
If ret = 1 Then
Call SendMessage(hWnd, BFFM_SETSTATUSTEXT, 0, sBuffer)
End If
End Select
BrowseCallbackProc = 0
End FunctionPrivate Function GetAddressofFunction(add As Long) As Long
GetAddressofFunction = add
End Function