Solved

Directory Picker in VC++ 6.0 with MFC

Posted on 2000-04-29
2
720 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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

743 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

12 Experts available now in Live!

Get 1:1 Help Now