Results 1 to 4 of 4

Thread: 2 questions regarding SHBrowseForFolder API: Resolved

  1. #1

    Thread Starter
    Hyperactive Member Dmitri K's Avatar
    Join Date
    Sep 2002
    Location
    West Palm Beach, FL
    Posts
    444

    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.

  2. #2
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    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:
    1. Public Declare Function SHBrowseForFolder Lib "shell32.dll" _
    2. (lpBrowseInfo As BROWSEINFO) As Long
    3. Public Declare Function SHGetPathFromIDList Lib "shell32.dll" _
    4. (ByVal pidl As Long, ByVal pszPath As String) As Long
    5. Public Type BROWSEINFO
    6.     howner As Long
    7.     pidlRoot As Long
    8.     pszDisplayName As String
    9.     lpszTitle As String
    10.     ulFlags As Long
    11.     lpfn As Long
    12.     lParam As Long
    13.     lImage As Long
    14. End Type
    15. Public bi As BROWSEINFO
    16. Public pidl As Long
    17.  
    18. 'release the memory used by the browse for folder
    19. Public Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal pv As Long)
    20. Public Const LMEM_FIXED = &H0
    21. Public Const LMEM_ZEROINIT = &H40
    22. Public Const LPTR = (LMEM_FIXED Or LMEM_ZEROINIT)
    23.  
    24. 'send a message to the browse for folder window
    25. Public Declare Function SendMessage Lib "user32" _
    26.    Alias "SendMessageA" _
    27.    (ByVal hWnd As Long, _
    28.    ByVal wMsg As Long, _
    29.    ByVal wParam As Long, _
    30.    lParam As Any) As Long
    31. Public Const BFFM_INITIALIZED = 1
    32. Public Const BFFM_SELECTIONCHANGED = 2
    33.  
    34. 'allocate and free space for the folder parameter
    35. ‘ that is to be passed to browse for folder
    36. Public Declare Function LocalAlloc Lib "kernel32" _
    37.    (ByVal uFlags As Long, _
    38.     ByVal uBytes As Long) As Long
    39. Public Declare Function LocalFree Lib "kernel32" _
    40.    (ByVal hMem As Long) As Long
    41. Public Declare Sub CopyMemory Lib "kernel32" _
    42.    Alias "RtlMoveMemory" _
    43.    (pDest As Any, _
    44.     pSource As Any, _
    45.     ByVal dwLength As Long)
    46.  
    47. Public Const MAX_PATH = 260
    48. Public Const WM_USER = &H400
    49. Public Const BFFM_SETSELECTIONA As Long = (WM_USER + 102)
    50. Public Const BFFM_SETSELECTIONW As Long = (WM_USER + 103)
    51.  
    52.  
    53. Public Function BrowseCallbackProcStr(ByVal hWnd As Long, _
    54.                                    ByVal uMsg As Long, _
    55.                                    ByVal lParam As Long, _
    56.                                    ByVal lpData As Long) As Long
    57. 'Called from the browse for folder window
    58. 'Sets the initial path to whatever has already been set
    59.    Select Case uMsg
    60.       Case BFFM_INITIALIZED
    61.          Call SendMessage(hWnd, BFFM_SETSELECTIONA, _
    62. True, ByVal lpData)
    63.          Case Else:
    64.    End Select
    65. End Function
    66.  
    67. Public Function FARPROC(ByVal pfn As Long) As Long
    68.   'A dummy procedure that receives and
    69.   '   returns the return value of the AddressOf operator.
    70.   'Used to get a pointer (AddressOf) to the call back routine.
    71.    FARPROC = pfn
    72. 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:
    1. Dim lpSelPath As Long
    2. Dim sPath As String * MAX_PATH
    3. Dim pidl as long
    4.  
    5. ' Get the folder required.
    6. ' Allocate it in some memory, with a pointer to it
    7.     sPath = “C:\Program Files”     
    8.     lpSelPath = LocalAlloc(LPTR, Len(sPath) + 1)
    9.     CopyMemory ByVal lpSelPath, ByVal sPath, Len(sPath) + 1
    10.  
    11.     With bi
    12.         If IsNumeric(hWnd) Then .howner = hWnd
    13.         .pidlRoot = 0
    14.         .lpfn = FARPROC(AddressOf BrowseCallbackProcStr)
    15.         .lParam = lpSelPath
    16.         .lpszTitle = "Select a Registered File folder:" & Chr$(0)
    17.     End With
    18.  
    19.     pidl = SHBrowseForFolder(bi)
    20.     If pidl Then
    21.         If SHGetPathFromIDList(ByVal pidl, ByVal gMyFolder) Then
    22.             gMyFolder = Left(Trim(gMyFolder), _
    23. Len(Trim(gMyFolder)) - 1)
    24.         End If
    25.         Call CoTaskMemFree(pidl)
    26.     End If
    27.     Call LocalFree(lpSelPath)
    28.  
    29.  
    30. ' gMyFolder now holds the path and folder actually selected

  3. #3
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    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.

  4. #4

    Thread Starter
    Hyperactive Member Dmitri K's Avatar
    Join Date
    Sep 2002
    Location
    West Palm Beach, FL
    Posts
    444
    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
  •  



Click Here to Expand Forum to Full Width