Solved

Directory Picker in VC++ 6.0 with MFC

Posted on 2000-04-29
2
721 Views
Last Modified: 2008-02-01
I want to get a directory through a dialog. There is a standard dialog that allows you to select a file, but not only a directory. How can I manage that?
0
Comment
Question by:ToniVR
2 Comments
 
LVL 1

Expert Comment

by:cbeattie
ID: 2762638
I'm not in front of MSVC++ right now to walk you through it exactly, but it is very easy.  One of the menu items is something along the lines of Insert ActiveX compoment (Insert | ActiveX mabye).  There is an ActiveX component named something along the lines of FolderPicker or FolderDialog.  Just insert that ActiveX component into your project and then instantiate an object of that type.  I won't submit this as an answer becuase I haven't given you the precise details , but if it works for you, I would appreciate it if you used the "Accept comment as answer" feature.  Thanks.
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 50 total points
ID: 2763334
Here's a working example.  Very useful for OnClick handler of [Browse] button.


//====================================================
// Pick Dir
//
static char szPickDir[MAX_PATH];
//---------------------------------------------------------------
int CALLBACK
BrowseCallbackProc(HWND hwnd,UINT uMsg,LPARAM lp, LPARAM pData)
{
      char szTmpDir[MAX_PATH];
      switch( uMsg ) {
            case BFFM_INITIALIZED:
           SendMessage(hwnd,BFFM_SETSELECTION,TRUE,(LPARAM)szPickDir); // WParam is TRUE since you are passing a path. It would be FALSE if you were passing a pidl.
        break;
        case BFFM_SELCHANGED:  // Set the status window to the currently selected path.
           if ( SHGetPathFromIDList((LPITEMIDLIST) lp ,szTmpDir) ) {
              SendMessage(hwnd,BFFM_SETSTATUSTEXT,0,(LPARAM)szTmpDir);
           }
        break;
        default:
            break;
    }
      return 0;
}

int PickDir( CString& sDir )
{
      int iRet= IDCANCEL;
      BROWSEINFO bi;
      char szDir[MAX_PATH];
      LPITEMIDLIST pidl;
      LPMALLOC pMalloc;

      if ( sDir > "" ) {
            lstrcpyn( szPickDir, (LPCSTR)sDir, sizeof(szPickDir) ) ;
      } else {
            GetCurrentDirectory( sizeof(szPickDir), szPickDir );
      }
      sDir="";

      if ( SUCCEEDED(SHGetMalloc(&pMalloc)) ) {
            ZeroMemory(&bi,sizeof(bi));
            bi.hwndOwner = NULL;
            bi.pszDisplayName = 0;
            bi.pidlRoot = 0;
            bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
            bi.lpfn = BrowseCallbackProc;

            pidl = SHBrowseForFolder(&bi);
            if (pidl) {
                  if (SHGetPathFromIDList(pidl,szDir)) {
                        sDir= szDir;
                        iRet= IDOK;
                  }
                  pMalloc->Free(pidl); pMalloc->Release();
            }
      }
      return ( iRet );
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now