Author Topic: VC++ - OpenFolder Dialog example  (Read 824 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
VC++ - OpenFolder Dialog example
« on: November 18, 2011, 03:23:42 pm »
It is often necessary to browse for a folder instead of a single file. It is required when there are multiple files needs to be handled under the single directory.
 
The function SHBrowseForFolder would make life easier for this purpose. BROWSEINFO struct would guide on the title for the browse folder dialog. SHGetPathFromIDList function would the return the actual path of the selected folder.
 
I have written the function BrowseFolder which return true, if the user has selected the folder. CString reference result would contain the selected folder by the user.

Code: (C++)
//Source Code Listing

bool BrowseFolder(const char *title, CString &result)
{
    BROWSEINFO brwinfo = { 0 };
    brwinfo.lpszTitle = _T("Select Your Source Directory");
    LPITEMIDLIST pitemidl = SHBrowseForFolder ( &brwinfo );
    if ( pitemidl == 0 )
        return false;
 
    // get the full path of the folder
    TCHAR path[MAX_PATH];
    if ( SHGetPathFromIDList ( pitemidl, path ) )
    {
        result = path;
    }
    IMalloc *pMalloc = 0;
    if ( SUCCEEDED( SHGetMalloc ( &pMalloc )) )
    {
        pMalloc->Free ( pitemidl );
        pMalloc->Release ( );
    }
 
    return true;
}

 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1