Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Browse For Folder - with starting path

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Browse For Folder - with starting path

    There are various versions of the same published in this forum. The only one I found providing the starting path is from Edwin Knoppert, but it did not work for me.
    This one seems to work fine.

    [CODE]
    #IF NOT %DEF(%WINAPI)
    #INCLUDE "WIN32API.INC"
    #ENDIF
    FUNCTION cbBrowse(BYVAL hWin AS DWORD, _
    BYVAL uMsg AS DWORD, _
    BYVAL lParam AS DWORD, _
    BYVAL lpData AS DWORD) AS LONG


    TRY

    SELECT CASE uMsg
    CASE %BFFM_INITIALIZED

    SendMessage hWin, %BFFM_SETSELECTION, 1, BYVAL lpData
    SetWindowText hWin, BYVAL VARPTR(sBrowseTitle)

    END SELECT

    FUNCTION = 0
    CATCH
    handle errors here
    END TRY
    END FUNCTION
    FUNCTION strChooseFolder(hWndOwner AS LONG, strPrompt AS ASCIIZ, strTitle AS ASCIIZ) AS STRING
    DIM intNull AS INTEGER
    DIM lngIDList AS ITEMIDLIST PTR
    DIM lngResult AS LONG
    DIM strPath AS ASCIIZ * 280
    DIM lngPointer AS DWORD
    DIM udtBI AS BrowseInfo
    STATIC lastPath AS ASCIIZ * %MAX_PATH 'saves the last selection in memory
    'you may add a parameter to the function to pass the starting folder instead

    TRY
    sBrowseTitle = strTitle
    udtBI.hWndOwner = hWndOwner
    udtBI.pidlRoot = 0
    udtBI.lpszTitle = VARPTR(strPrompt)
    udtBI.ulFlags = %BIF_USENEWUI
    udtBI.lpfnCallback = CODEPTR(cbBrowse)
    IF LEN(lastPath) > 0 THEN
    udtBI.lParam = VARPTR(lastPath)
    END IF

    lngPointer = SHBrowseForFolder(udtBI)
    IF lngPointer THEN
    lngIDList = lngPointer
    lngResult = SHGetPathFromIDList(@lngIDList, strPath)
    CALL CoTaskMemFree(lngPointer )
    END IF
    IF LEN(strPath) > 0 THEN lastPath = strPath
    strChooseFolder = strPath
    CATCH
    'handle errors here
    END TRY
    END FUNCTION

    ------------------

  • #2
    ' This version of Peter's code is self-contained. It runs as a separate program. You can specify
    ' the path to display at start up - either the current path or another path specified by you.
    ' Other minor changes have been made to simplify things a little.
    '
    ' Best regards
    '
    ' Erik
    '
    ' P.S. October 4th: Added BYVAL in this statement: SHGetPathFromIDList BYVAL lngIDList, strPath

    Code:
    #COMPILE EXE
    #INCLUDE "win32api.inc"
    '
    FUNCTION cbBrowse(BYVAL hWin AS DWORD, BYVAL uMsg AS DWORD, BYVAL lParam AS DWORD, BYVAL lpData AS DWORD) AS LONG
        TRY
            SELECT CASE uMsg
                CASE %BFFM_INITIALIZED
                    SendMessage hWin, %BFFM_SETSELECTION, 1, BYVAL lpData
                    ' You do not need to set the caption. Your system sets the standard text automatically in your language.
            END SELECT
            FUNCTION = 0
        CATCH
    '        handle errors here
        END TRY
    END FUNCTION
    '
    FUNCTION strChooseFolder(BYVAL hWndOwner AS LONG, BYVAL strPrompt AS ASCIIZ * 300, BYREF lastPath AS ASCIIZ) AS STRING
        DIM lngIDList AS LONG
        DIM strPath AS ASCIIZ * %MAX_PATH
        DIM udtBI AS BROWSEINFO
        TRY
            udtBI.hWndOwner = hWndOwner
            udtBI.lpszTitle = VARPTR(strPrompt)
            udtBI.ulFlags = %BIF_USENEWUI
            udtBI.lpfnCallback = CODEPTR(cbBrowse)
            IF ISTRUE LEN(lastPath) THEN udtBI.lParam = VARPTR(lastPath)
            lngIDList = SHBrowseForFolder(udtBI)
            IF ISTRUE lngIDList THEN
                SHGetPathFromIDList BYVAL lngIDList, strPath
                CoTaskMemFree lngIDList
            END IF
            IF ISTRUE LEN(strPath) THEN lastPath = strPath
            FUNCTION = strPath
        CATCH
            'handle errors here
        END TRY
    END FUNCTION
    '
    CALLBACK FUNCTION dlgProc()
       LOCAL s AS STRING
       STATIC lastPath AS ASCIIZ * %MAX_PATH
       SELECT CASE CBMSG
          CASE %WM_INITDIALOG
              GetModuleFileName GetModuleHandle(""), lastPath, %MAX_PATH  ' Get current Path & file name
              lastPath = LEFT$(lastPath,INSTR(-1, lastPath, ANY "\:/"))   ' Get just the path without the file name
              ' lastPath = "C:\Programs\" ' Alternatively you could set the initial path directly.
              ' lastPath = "" ' or you could set it to nothing.
          CASE %WM_COMMAND
             IF CBCTL = 100 THEN
                s = strChooseFolder(CBHNDL, "select folder...", lastPath)
                IF ISTRUE LEN(s) THEN MSGBOX "Folder = " + s
             END IF
       END SELECT
    END FUNCTION
    '
    FUNCTION PBMAIN() AS LONG
       LOCAL hDlg AS LONG
       DIALOG NEW 0, "Browse for Folder", , , 200, 200, %WS_SYSMENU TO hDlg
       CONTROL ADD BUTTON, hDlg, 100, "Show &Folders", 60, 85, 80, 20
       DIALOG SHOW MODAL hDlg CALL dlgProc
    END FUNCTION


    [This message has been edited by Erik Christensen (edited October 04, 2005).]

    Comment

    Working...
    X