|
-
Dec 18th, 2002, 02:32 AM
#1
Thread Starter
Hyperactive Member
2 questions regarding SHBrowseForFolder API: Resolved
How to automatically display default path (for example, when calling the API, the selected folder would be c:\Program Files\), second question, how to disable OK button and only enable it when certain file types (for example, .exe or .com or .bat) are located in the folder user selected.
Thanks
Last edited by Dmitri K; Dec 18th, 2002 at 04:09 PM.
-
Dec 18th, 2002, 07:13 AM
#2
Frenzied Member
The answer to the second is that "BrowseForFolder" doesn't know anything about files in the folder. To do that type of analysis, you will need to use the CommonDialogue, which allows a mask of file type to search on.
To allow the "BrowseForFolder" to start from a particular place, you need a bit more code: This procedure sends a message to the browsing window, telling it to start the display where the parameter specifies.
In order to implement this, certain API and function definitions are required in a code module:
VB Code:
Public Declare Function SHBrowseForFolder Lib "shell32.dll" _
(lpBrowseInfo As BROWSEINFO) As Long
Public Declare Function SHGetPathFromIDList Lib "shell32.dll" _
(ByVal pidl As Long, ByVal pszPath As String) As Long
Public Type BROWSEINFO
howner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
lImage As Long
End Type
Public bi As BROWSEINFO
Public pidl As Long
'release the memory used by the browse for folder
Public Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal pv As Long)
Public Const LMEM_FIXED = &H0
Public Const LMEM_ZEROINIT = &H40
Public Const LPTR = (LMEM_FIXED Or LMEM_ZEROINIT)
'send a message to the browse for folder window
Public Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Public Const BFFM_INITIALIZED = 1
Public Const BFFM_SELECTIONCHANGED = 2
'allocate and free space for the folder parameter
‘ that is to be passed to browse for folder
Public Declare Function LocalAlloc Lib "kernel32" _
(ByVal uFlags As Long, _
ByVal uBytes As Long) As Long
Public Declare Function LocalFree Lib "kernel32" _
(ByVal hMem As Long) As Long
Public Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(pDest As Any, _
pSource As Any, _
ByVal dwLength As Long)
Public Const MAX_PATH = 260
Public Const WM_USER = &H400
Public Const BFFM_SETSELECTIONA As Long = (WM_USER + 102)
Public Const BFFM_SETSELECTIONW As Long = (WM_USER + 103)
Public Function BrowseCallbackProcStr(ByVal hWnd As Long, _
ByVal uMsg As Long, _
ByVal lParam As Long, _
ByVal lpData As Long) As Long
'Called from the browse for folder window
'Sets the initial path to whatever has already been set
Select Case uMsg
Case BFFM_INITIALIZED
Call SendMessage(hWnd, BFFM_SETSELECTIONA, _
True, ByVal lpData)
Case Else:
End Select
End Function
Public Function FARPROC(ByVal pfn As Long) As Long
'A dummy procedure that receives and
' returns the return value of the AddressOf operator.
'Used to get a pointer (AddressOf) to the call back routine.
FARPROC = pfn
End Function
As well as the definitions above, the actual code to do the calling of the Browse For Folder window is required. This code sets the initial folder required, although it is the call back routine that actually makes the window select the specified directory.
VB Code:
Dim lpSelPath As Long
Dim sPath As String * MAX_PATH
Dim pidl as long
' Get the folder required.
' Allocate it in some memory, with a pointer to it
sPath = “C:\Program Files”
lpSelPath = LocalAlloc(LPTR, Len(sPath) + 1)
CopyMemory ByVal lpSelPath, ByVal sPath, Len(sPath) + 1
With bi
If IsNumeric(hWnd) Then .howner = hWnd
.pidlRoot = 0
.lpfn = FARPROC(AddressOf BrowseCallbackProcStr)
.lParam = lpSelPath
.lpszTitle = "Select a Registered File folder:" & Chr$(0)
End With
pidl = SHBrowseForFolder(bi)
If pidl Then
If SHGetPathFromIDList(ByVal pidl, ByVal gMyFolder) Then
gMyFolder = Left(Trim(gMyFolder), _
Len(Trim(gMyFolder)) - 1)
End If
Call CoTaskMemFree(pidl)
End If
Call LocalFree(lpSelPath)
' gMyFolder now holds the path and folder actually selected
-
Dec 18th, 2002, 02:10 PM
#3
Frenzied Member
If you would've actually understood the coding you copied straight out of MVPS.org you'd know that you can send a message to HWND received in the callback proc to disable the OK button.
You'll received a BFFM_SELCHANGED message in the callback proc whenever a different folder is selected. From there you can get the name of the folder that's currently highlighted, scan it for whatever file types you don't want, and if you find what you don't want, use "SendMessage" to send a BFFM_ENABLEOK to HWND.
Set the lParam to 0 to disable, non-zero to enable.
-
Dec 18th, 2002, 03:05 PM
#4
Thread Starter
Hyperactive Member
Shawn, I actually did tinker with those constants a while ago, but than something came up and I focused on a bigger problem. I'll try again.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|