Solved

Directory Picker in VC++ 6.0 with MFC

Posted on 2000-04-29
2
726 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

734 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