Link to home
Start Free TrialLog in
Avatar of galneweinhaw
galneweinhaw

asked on

How to open a File Browse Dialog

How can I create a File browsing dialog that returns the directory chosen?

I tried this:

http://www.codeproject.com/dialog/cfolderdialog.asp?df=100&forumid=3533&select=957565

but can't get it to work (errors are posted at the bottom of that page)

thanks!
Avatar of jkr
jkr
Flag of Germany image

Usually, you'd use 'SHBrowseForFolder()' to do that, e.g.

#include <shlobj.h>

void PickFolder ( CString& strFolder)
{

    LPMALLOC    pMalloc;    /* Gets the Shell's default allocator */

    if  (   ::SHGetMalloc   (   &pMalloc)   ==  NOERROR)
        {
            BROWSEINFO      bi;
            char            pszBuffer   [   MAX_PATH];
            LPITEMIDLIST    pidl;

            // Get help on BROWSEINFO struct - it's got all the bit settings.
            bi.hwndOwner        =   GetSafeHwnd();
            bi.pidlRoot         =   NULL;
            bi.pszDisplayName   =   pszBuffer;
            bi.lpszTitle        =   _T("Select a Folder");
            bi.ulFlags          =   0;
            bi.lpfn             =   NULL;
            bi.lParam           =   0;

            // This next call issues the dialog box.
            if  (   pidl    =   ::SHBrowseForFolder (   &bi))
                {
                    if  (   ::SHGetPathFromIDList   (   pidl, pszBuffer))
                        {
                            // At this point pszBuffer contains the selected folder */
                                          strFolder = pszBuffer;
                         }

                    // Free the PIDL allocated by SHBrowseForFolder.
                    pMalloc->Free   (   pidl);
                }

            // Release the shell's allocator.        
            pMalloc->Release();    
        }    
}
Avatar of galneweinhaw
galneweinhaw

ASKER

That works great!

is there any way for me to specify the folder the browse window starts in? (without restricting the browsing by using a root)

thanks
http://www.codeproject.com/dialog/folderbrowser.asp ("How to show a dialog to let users browse for and select a folder") describes that issu epretty well, and better than I could do.
SOLUTION
Avatar of Nass89
Nass89
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I tried the codeproject thing but it's giving me this error...

Compiling...
UpdateIESDlg.cpp
O:\ENGINEERING\SHARED\PHOTOMET\PROGRAMS\OTHER PROGRAMS\UPDATEIES\UpdateIES\UpdateIESDlg.cpp(302) : error C2440: '=' : cannot convert from 'int (__stdcall CUpdateIESDlg::*)(struct HWND__ *,unsigned int,long,long)' to 'int (__stdcall *)(struct HWND__
*,unsigned int,long,long)'
        There is no context in which this conversion is possible
O:\ENGINEERING\SHARED\PHOTOMET\PROGRAMS\OTHER PROGRAMS\UPDATEIES\UpdateIES\UpdateIESDlg.cpp(312) : warning C4390: ';' : empty controlled statement found; is this the intent?
Error executing cl.exe.
Creating browse info file...

UpdateIES.exe - 1 error(s), 1 warning(s)


The first occurs here:
bi.lpfn = BrowseCallbackProc;

The warning here:
        if(SUCCEEDED(SHGetMalloc(&pMalloc)) && pMalloc);
        pMalloc->Free(pidl);  
        pMalloc->Release();


thanks for the help.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks to both of you for the help

unfortunately I am still getting this error using the code above:


error C2440: '=' : cannot convert from 'int (__stdcall CUpdateIESDlg::*)(struct HWND__ *,unsigned int,long,long)' to 'int (__stdcall *)(struct HWND__ *,unsigned int,long,long)' There is no context in which this conversion is possible


It is occuring at this line:
bi.lpfn             =   BrowseCallbackProc;



Also getting the error:
error C2660: 'SendMessageA' : function does not take 4 parameters

from lines:
SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)szDir);
//and
SendMessage(hwnd,BFFM_SETSTATUSTEXT,0,(LPARAM)szDir);

thanks again.
>>error C2440: '=' : cannot convert from

The browse callback cannot be a non-static member of CUpdateIESDlg - either make that not a member function or declare it as 'static'.

>>error C2660: 'SendMessageA' : function does not take 4 parameters

The compiler thinks you are referring to CWnd::SendMessage() - use

::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)szDir);
//and
::SendMessage(hwnd,BFFM_SETSTATUSTEXT,0,(LPARAM)szDir);

to resolve this ambiguity.
Thanks jkr,

working great.

How do I post a follow-up question?

My directory name is so long it is wrapping and getting hidden behind the tree-view.