Re: Dir Browser - Can't close it

From: Stefan Merwitz (nospam_at_thisserverdoesntexist.com)
Date: 08/16/04


Date: Mon, 16 Aug 2004 17:50:15 +0200

The following source is a snippet from my clsFileOperation class from my
website (www.VBTricks.de.vu). The folder selection window is shown
modally, so there won't be the problems you described.

Paste this code into a module (there might be some declares and
constants missing, but you can find them in the APIViewer or on the net):

'-------------------
'Folderselection
Private 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 Type

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 Sub CoTaskMemFree Lib "ole32" (ByVal pv As Long)

Private Const BIF_RETURNONLYFSDIRS = &H1
Private Const BIF_USENEWUI = &H40
Private Const BIF_STATUSTEXT = &H4
Private Const BIF_EDITBOX = &H10

'for c-strings
Private Declare Function LocalAlloc Lib "kernel32" _
(ByVal uFlags As Long, ByVal uBytes As Long) As Long
Private Declare Function LocalFree Lib "kernel32" _
  (ByVal hMem As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias _
  "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal dwLength As Long)

Private Const LMEM_FIXED = &H0
Private Const LMEM_ZEROINIT = &H40
Private Const LPTR = (LMEM_FIXED Or LMEM_ZEROINIT)

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 Const BFFM_INITIALIZED = 1

Private Const WM_USER = &H400
Private Const BFFM_SETSELECTIONA As Long = (WM_USER + 102)

Public Function BrowseCallbackProcStr(ByVal hWnd As Long, _
  ByVal uMsg As Long, ByVal lParam As Long, ByVal lpData As Long) As Long
On Error Resume Next
Select Case uMsg
Case BFFM_INITIALIZED
     'set startdir
     SendMessage hWnd, BFFM_SETSELECTIONA, True, ByVal lpData
End Select
End Function

Public Function FARPROC(pfn As Long) As Long
'return value of AddressOf as long
FARPROC = pfn
End Function

Public Function SelectFolder(ByVal lOwnerHWND As Long, _
  Optional ByVal sStartFolder As String, _
  Optional ByVal sCaption As String) As String
   Dim BInfo As BROWSEINFO, DlgTitle As String
   Dim pid As Long, lpSelPath As Long, SelPath As String

   With BInfo
       .hwndOwner = lOwnerHWND
       .lpszTitle = sCaption
       .pidlRoot = 0
       If sStartFolder <> "" Then
           .lpfnCallback = FARPROC(AddressOf BrowseCallbackProcStr)
           lpSelPath = LocalAlloc(LPTR, Len(sStartFolder) + 1)
           CopyMemory ByVal lpSelPath, ByVal sStartFolder, _
               Len(sStartFolder) + 1
           .lParam = lpSelPath
       End If

       .ulFlags = BIF_USENEWUI Or BIF_EDITBOX Or BIF_RETURNONLYFSDIRS
   End With

   pid = SHBrowseForFolder(BInfo)

   If pid Then
       SelPath = Space$(255)
       Call SHGetPathFromIDList(pid, SelPath)
       SelPath = Left$(SelPath, InStr(SelPath, Chr$(0)) - 1)
       Call CoTaskMemFree(pid)
   Else
       SelPath = ""
   End If
   SelectFolder = SelPath
   Call LocalFree(lpSelPath)
End Function
'-----------------------

Hope this helps,

Stefan

P.S.: The original class includes the file open and save dialogs
(without using any OCX's) and some other useful functions (delete, move
and copy with the explorer progress window, show the properties of a
file, show the 'open with' dialog...).

___________________________________www.VBTricks.de.vu
the free resource for Visual Basic, Gambas and Pascal
components, tips & complete projects

www: http://www.VBTricks.de.vu
mail: vbtricks <at> my-mail <dot> ch
_____________________________________________________



Relevant Pages

  • Re: Microsoft Common Dialog control, version 6.0
    ... Private Declare Function lstrlen Lib "kernel32" _ ... Lib "kernel32" (ByVal lpPathName As String) As ... Private Const OFN_ALLOWMULTISELECT As Long = &H200 ... Dim strFile As String ...
    (microsoft.public.excel.programming)
  • Re: export to a text file
    ... Private Declare Function lstrlen Lib "kernel32" _ ... Lib "kernel32" (ByVal lpPathName As String) As Long ... Private Const OFN_ALLOWMULTISELECT As Long = &H200 ... Dim strFile As String ...
    (microsoft.public.excel.programming)
  • Verifying if a page exists on a website using VB
    ... "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal ... Private Declare Function InternetConnect Lib "wininet.dll" Alias ... Private Const HTTP_QUERY_STATUS_CODE = 19 ...
    (microsoft.public.vb.winapi)
  • Re: How to detect previous instance of my App?
    ... Private Const PROCESS_QUERY_INFORMATION = 1024 ... Private Type PROCESS_MEMORY_COUNTERS ... Private Declare Function CloseHandle Lib "kernel32" ...
    (microsoft.public.vb.winapi)
  • Re: Are Metafiles the Spawn of Satan?
    ... Private Declare Function GetWinMetaFileBits& Lib "gdi32" _ ... Private Const CF_ENHMETAFILE As Long = 14 ...
    (microsoft.public.vb.winapi.graphics)