模块代码: 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 Long Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As LongPrivate 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 TypePrivate m_CurrentDirectory As String 'The current directory 'Public Function BrowseForFolder(owner As Form, Title As String, StartDir As String) As String 'Opens a Treeview control that displays the directories in a computer 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 .lpszTitle = lstrcat(szTitle, "") .ulFlags = BIF_RETURNONLYFSDIRS + BIF_DONTGOBELOWDOMAIN + BIF_STATUSTEXT .lpfnCallback = GetAddressofFunction(AddressOf BrowseCallbackProc) 'get address of function. 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 IfEnd FunctionPrivate 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 'Sugested by MS to prevent an error from 'propagating back into the calling process. 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 = 0End Function' This function allows you to assign a function pointer to a vaiable. Private Function GetAddressofFunction(add As Long) As Long GetAddressofFunction = add End Function 窗体代码: Option ExplicitPrivate Sub Form_Load() Dim strGetDir As String Me.Show strGetDir = BrowseForFolder(Me, "Select A Directory", CurDir) If Len(strGetDir) = 0 Then Exit Sub 'user selected cancel MsgBox strGetDirEnd Sub这个不但可以实现楼上两位朋友的需要,还可以在对话框中实时显示你当时选中的目录哦!
原来是用回调函数啊,感谢 QiaoDaLi!
给二楼的代码完善一下,加个功能: 让"确定"按钮的状态随当前被选文件夹中是否存在有要查找的文件而改变 就像系统安装驱动时那样的效果模块代码: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_ENABLEOK = (WM_USER + 101) 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, lParam As Any) 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 Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As LongPrivate 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 TypePrivate m_CurrentDirectory As String 'The current directory '当前目录 Private m_FindFileName As StringPublic Function BrowseForFolder(frm As Form, Title As String, StartDir As String, Optional FindFile As String) As String 'Opens a TreeView control that displays the directories in a computer '打开一个目录数对话框显示计算机中的目录 Dim lpIDList As Long Dim szTitle As String Dim sBuffer As String Dim tBrowseInfo As BrowseInfo m_CurrentDirectory = StartDir & vbNullChar m_FindFileName = FindFile szTitle = Title With tBrowseInfo .hWndOwner = frm.hwnd .lpszTitle = lstrcat(szTitle, "") .ulFlags = BIF_RETURNONLYFSDIRS + BIF_DONTGOBELOWDOMAIN + BIF_STATUSTEXT 'get address of function. '获取函数的地址。 .lpfnCallback = GetAddressofFunction(AddressOf BrowseCallbackProc) 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 IfEnd FunctionPrivate 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 'Suggested by MS to prevent an error from 'propagation back into the calling process. Select Case uMsg Case BFFM_INITIALIZED Call SendMessage(hwnd, BFFM_SETSELECTION, 1, ByVal m_CurrentDirectory) Case BFFM_SELCHANGED sBuffer = Space(MAX_PATH) ret = SHGetPathFromIDList(lp, sBuffer) If ret = 1 Then Call SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, ByVal sBuffer) '下面是[让"确定"按钮的状态随当前被选文件夹中是否存在有要查找的文件而改变]的代码 If Len(m_FindFileName) <> 0 Then sBuffer = Left$(sBuffer, InStr(sBuffer, vbNullChar) - 1) '截取中止符 If Right$(sBuffer, 1) <> "\" Then sBuffer = sBuffer & "\" '查找文件,以决定“确定”按钮是否有效 SendMessage hwnd, BFFM_ENABLEOK, 0, ByVal CLng(((Dir$(sBuffer & m_FindFileName) <> "") And 1&)) 'Debug.Print ((Dir$(sBuffer & m_FindFileName) <> "") And 1&) End If End If End Select BrowseCallbackProc = 0End Function' This function allows you to assign a function pointer to a variable. ' 此函数可以将函数指针分配给一个变量。 Private Function GetAddressofFunction(add As Long) As Long GetAddressofFunction = add End Function 窗体代码:Option ExplicitPrivate Sub Form_Load() Dim strGetDir As String Me.Show strGetDir = BrowseForFolder(Me, "Select A Directory", CurDir) If Len(strGetDir) = 0 Then Exit Sub 'user selected cancel '用户按下“取消”按钮 MsgBox strGetDir End SubPrivate Sub Form_Click() Dim strGetDir As String strGetDir = BrowseForFolder(Me, "Select A Directory", CurDir, "*.txt") If Len(strGetDir) = 0 Then Exit Sub 'user selected cancel '用户按下“取消”按钮 MsgBox strGetDir End Sub呵呵,楼主可以加分给我了吧!!!!!!
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 Long
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As LongPrivate 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 TypePrivate m_CurrentDirectory As String 'The current directory
'Public Function BrowseForFolder(owner As Form, Title As String, StartDir As String) As String
'Opens a Treeview control that displays the directories in a computer 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
.lpszTitle = lstrcat(szTitle, "")
.ulFlags = BIF_RETURNONLYFSDIRS + BIF_DONTGOBELOWDOMAIN + BIF_STATUSTEXT
.lpfnCallback = GetAddressofFunction(AddressOf BrowseCallbackProc) 'get address of function.
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 IfEnd FunctionPrivate 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 'Sugested by MS to prevent an error from
'propagating back into the calling process. 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 = 0End Function' This function allows you to assign a function pointer to a vaiable.
Private Function GetAddressofFunction(add As Long) As Long
GetAddressofFunction = add
End Function
窗体代码:
Option ExplicitPrivate Sub Form_Load() Dim strGetDir As String Me.Show strGetDir = BrowseForFolder(Me, "Select A Directory", CurDir)
If Len(strGetDir) = 0 Then Exit Sub 'user selected cancel MsgBox strGetDirEnd Sub这个不但可以实现楼上两位朋友的需要,还可以在对话框中实时显示你当时选中的目录哦!
让"确定"按钮的状态随当前被选文件夹中是否存在有要查找的文件而改变
就像系统安装驱动时那样的效果模块代码: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_ENABLEOK = (WM_USER + 101)
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, lParam As Any) 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 Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As LongPrivate 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 TypePrivate m_CurrentDirectory As String 'The current directory '当前目录
Private m_FindFileName As StringPublic Function BrowseForFolder(frm As Form, Title As String, StartDir As String, Optional FindFile As String) As String
'Opens a TreeView control that displays the directories in a computer
'打开一个目录数对话框显示计算机中的目录 Dim lpIDList As Long
Dim szTitle As String
Dim sBuffer As String
Dim tBrowseInfo As BrowseInfo
m_CurrentDirectory = StartDir & vbNullChar
m_FindFileName = FindFile szTitle = Title
With tBrowseInfo
.hWndOwner = frm.hwnd
.lpszTitle = lstrcat(szTitle, "")
.ulFlags = BIF_RETURNONLYFSDIRS + BIF_DONTGOBELOWDOMAIN + BIF_STATUSTEXT
'get address of function. '获取函数的地址。
.lpfnCallback = GetAddressofFunction(AddressOf BrowseCallbackProc)
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 IfEnd FunctionPrivate 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 'Suggested by MS to prevent an error from
'propagation back into the calling process. Select Case uMsg Case BFFM_INITIALIZED
Call SendMessage(hwnd, BFFM_SETSELECTION, 1, ByVal m_CurrentDirectory) Case BFFM_SELCHANGED
sBuffer = Space(MAX_PATH) ret = SHGetPathFromIDList(lp, sBuffer)
If ret = 1 Then
Call SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, ByVal sBuffer) '下面是[让"确定"按钮的状态随当前被选文件夹中是否存在有要查找的文件而改变]的代码
If Len(m_FindFileName) <> 0 Then
sBuffer = Left$(sBuffer, InStr(sBuffer, vbNullChar) - 1) '截取中止符
If Right$(sBuffer, 1) <> "\" Then sBuffer = sBuffer & "\"
'查找文件,以决定“确定”按钮是否有效
SendMessage hwnd, BFFM_ENABLEOK, 0, ByVal CLng(((Dir$(sBuffer & m_FindFileName) <> "") And 1&))
'Debug.Print ((Dir$(sBuffer & m_FindFileName) <> "") And 1&)
End If
End If End Select BrowseCallbackProc = 0End Function' This function allows you to assign a function pointer to a variable.
' 此函数可以将函数指针分配给一个变量。
Private Function GetAddressofFunction(add As Long) As Long
GetAddressofFunction = add
End Function
窗体代码:Option ExplicitPrivate Sub Form_Load()
Dim strGetDir As String Me.Show strGetDir = BrowseForFolder(Me, "Select A Directory", CurDir)
If Len(strGetDir) = 0 Then Exit Sub 'user selected cancel '用户按下“取消”按钮 MsgBox strGetDir
End SubPrivate Sub Form_Click()
Dim strGetDir As String strGetDir = BrowseForFolder(Me, "Select A Directory", CurDir, "*.txt")
If Len(strGetDir) = 0 Then Exit Sub 'user selected cancel '用户按下“取消”按钮 MsgBox strGetDir
End Sub呵呵,楼主可以加分给我了吧!!!!!!