Click here to Skip to main content
15,881,139 members
Articles / Desktop Programming / MFC

Selecting a Subfolder from a Particular Folder

Rate me:
Please Sign up or sign in to vote.
4.56/5 (10 votes)
23 Feb 2010CPOL2 min read 89.4K   2.9K   39  
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__;
#endif

extern TCHAR szDir[MAX_PATH];

/////////////////////////////////////////////////////////////////////////////
// CBrowsePFDlg dialog

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

void CBrowsePFDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBrowsePFDlg)
	DDX_Text(pDX, IDC_EDIT_ROOT_FOLDER, m_RootFolder);
	DDX_Text(pDX, IDC_EDIT_CHOSEN_FOLDER, m_ChosenFolder);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CBrowsePFDlg, CDialog)
	//{{AFX_MSG_MAP(CBrowsePFDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_FOLDER, OnButtonFolder)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBrowsePFDlg message handlers

BOOL CBrowsePFDlg::OnInitDialog()
{
	CDialog::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;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// 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() 
{
	UpdateData(TRUE);
	if (m_RootFolder.IsEmpty())
    {
        AfxMessageBox(_T("Enter a valid root folder path"));
        return;
    }
    wcscpy(szDir, m_RootFolder);
	HWND hWnd = GetSafeHwnd();
	CFoldersDialog objFD;
	if (objFD.BrowseFolder(hWnd, m_RootFolder) == 0)
    {
	    m_ChosenFolder = szDir;
	    UpdateData(FALSE);
    }
}

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)


Written By
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

Comments and Discussions