Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Selecting a Subfolder from a Particular Folder

, 23 Feb 2010
Restricting the user to browse and select from a particular folder
// FoldersDialog.cpp: implementation of the CFoldersDialog class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "FoldersDialog.h"

TCHAR szDir[MAX_PATH];

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CFoldersDialog::CFoldersDialog()
{

}

CFoldersDialog::~CFoldersDialog()
{

}

LPITEMIDLIST ConvertPathToLpItemIdList(const char *pszPath)
{
	LPITEMIDLIST  pidl = NULL;
	LPSHELLFOLDER pDesktopFolder = NULL;
	OLECHAR       olePath[MAX_PATH];
	ULONG         chEaten;
	HRESULT       hr;

	if (SUCCEEDED(SHGetDesktopFolder(&pDesktopFolder)))
	{
		MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, pszPath, -1, olePath, MAX_PATH);
		hr = pDesktopFolder->ParseDisplayName(NULL, NULL, olePath, &chEaten, &pidl, NULL);
		pDesktopFolder->Release();
		return pidl;
	}
	return NULL;
}

int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData) 
{
	switch(uMsg) 
	{
		case BFFM_SELCHANGED: 
		{
		   // Set the status window to the currently selected path.
		   if (SHGetPathFromIDList((LPITEMIDLIST) lp, szDir)) 
			  SendMessage(hwnd,BFFM_SETSTATUSTEXT, 0, (LPARAM)szDir);
		   break;
		}
        case BFFM_INITIALIZED:
        {
            SendMessage(hwnd, BFFM_SETSELECTION, 1,(LPARAM) szDir);
        }
        break;

		default:
		   break;
	}
	return 0;
}

int CFoldersDialog::BrowseFolder(HWND hWnd, CString RootPath)
{
	BROWSEINFO bi;
	TCHAR szDir[MAX_PATH];
	LPITEMIDLIST pidl;
	LPMALLOC pMalloc;
	int nRet = 0;

	if (SUCCEEDED(SHGetMalloc(&pMalloc))) 
	{
		ZeroMemory(&bi,sizeof(bi));
		bi.hwndOwner = hWnd;
		bi.pszDisplayName = 0;
		bi.lpszTitle = 0;
        char *pszRootPath = new char[RootPath.GetLength() + 1];
        wcstombs(pszRootPath, RootPath, RootPath.GetLength());
		bi.pidlRoot = ConvertPathToLpItemIdList(pszRootPath);
		if (bi.pidlRoot == NULL)
			nRet = 1;
		bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
		bi.lpfn = BrowseCallbackProc;

		pidl = SHBrowseForFolder(&bi);
		if (pidl) 
		   SHGetPathFromIDList(pidl,szDir);
		else
			nRet = 2;
		pMalloc->Free(pidl); 
		pMalloc->Release();
	}
	return nRet;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ahamad Alisha
CEO GIPL
India India
Ahamad has done M.Tech. in Information Technology. He has 15 years software development experience. He started learning programming with GW-BASIC. He learnt COBOL, Pascal, Fortran, C, C++, Java and C#. He worked with DOS, Unix, Novel Netware and Windows platforms. He has been working on Microsoft technologies for the last 10 years. He worked with .NET, Visual C++, ATL COM/DCOM, Win SDK, MFC, WTL, Visual Basic, ASP, JavaScript, XML, OOAD and Rational Rose. He has written three books on Computer Science:
 
1. Programming in GW-BASIC, BPB Publications, New Delhi, 1993
2. Computer Science with C++, Allied Publishers, New Delhi, 1997
3. Introduction to C++, Allied Publishers, New Delhi. 1998
 
He enjoys playing badminton and musical keyboards in his spare time. He also participates in community activities. He can be reached by ahamadalisha@yahoo.com

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 24 Feb 2010
Article Copyright 2003 by Ahamad Alisha
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid