Click here to Skip to main content
13,446,408 members (36,552 online)
Click here to Skip to main content


38 bookmarked
Posted 26 Apr 2003

Selecting a Subfolder from a Particular Folder

, 23 Feb 2010
Restricting the user to browse and select from a particular folder
// BrowsePFDlg.cpp : implementation file

#include "stdafx.h"
#include "BrowsePF.h"
#include "BrowsePFDlg.h"
#include "FoldersDialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;

extern TCHAR szDir[MAX_PATH];

// CBrowsePFDlg dialog

CBrowsePFDlg::CBrowsePFDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBrowsePFDlg::IDD, pParent)
	m_RootFolder = _T("");
	m_ChosenFolder = _T("");
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

void CBrowsePFDlg::DoDataExchange(CDataExchange* pDX)
	DDX_Text(pDX, IDC_EDIT_ROOT_FOLDER, m_RootFolder);
	DDX_Text(pDX, IDC_EDIT_CHOSEN_FOLDER, m_ChosenFolder);


// CBrowsePFDlg message handlers

BOOL CBrowsePFDlg::OnInitDialog()

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	// TODO: Add extra initialization here
	return TRUE;  // return TRUE  unless you set the focus to a control

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CBrowsePFDlg::OnPaint() 
	if (IsIconic())
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CBrowsePFDlg::OnQueryDragIcon()
	return (HCURSOR) m_hIcon;

void CBrowsePFDlg::OnButtonFolder() 
	if (m_RootFolder.IsEmpty())
     AfxMessageBox(_T("Enter a valid root folder path"));
  wcscpy(szDir, m_RootFolder);
	HWND hWnd = GetSafeHwnd();
	CFoldersDialog objFD;
	if (objFD.BrowseFolder(hWnd, m_RootFolder) == 0)
	  m_ChosenFolder = szDir;

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.


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


About the Author

Ahamad Alisha
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

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03-2016 | 2.8.180314.2 | Last Updated 24 Feb 2010
Article Copyright 2003 by Ahamad Alisha
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid