Click here to Skip to main content
15,891,529 members
Articles / Desktop Programming / MFC

CDirectoryChangeWatcher - ReadDirectoryChangesW all wrapped up

Rate me:
Please Sign up or sign in to vote.
4.92/5 (114 votes)
11 May 2002 2.2M   39.4K   365  
This class wraps up ReadDirectoryChangesW.
/* 
DESCRIPTION:
	CFolderDialog  - Folder Selection Dialog Class

NOTES:
	Copyright(C) Armen Hakobyan, 2002
	mailto:armenh@cit.am
	
VERSION HISTORY:
	24 Mar 2002 - First release
*/

#include "stdafx.h"
#include "FolderDialog.h"

#ifndef BFFM_VALIDATEFAILED
#ifndef UNICODE
#define BFFM_VALIDATEFAILED 3
#else
#define BFFM_VALIDATEFAILED 4	
#endif
#endif //!BFFM_VALIDATEFAILED


// CFolderDialog

IMPLEMENT_DYNAMIC(CFolderDialog, CDialog)
CFolderDialog::CFolderDialog(LPCTSTR lpszTitle /*NULL*/,
							 LPCTSTR lpszFolderName /*NULL*/,
							 CWnd* pParentWnd /*NULL*/,
							 UINT uFlags /*BIF_RETURNONLYFSDIRS*/)
			 : CCommonDialog(pParentWnd)
			 , m_hWnd(NULL)			
{
	m_szSelectedPath[0]	= '\0';
	m_szFolderPath[0]	= '\0';
	m_szFolderDisplayName[0]= '\0';
	
	if(lpszFolderName != NULL)
		lstrcpy(m_szSelectedPath, lpszFolderName);
	
	// Fill
	::ZeroMemory(&m_bi, sizeof(BROWSEINFO)); 
	m_bi.hwndOwner = pParentWnd->GetSafeHwnd();
	m_bi.pidlRoot = NULL;
	m_bi.pszDisplayName = m_szFolderDisplayName;
	m_bi.lpszTitle = lpszTitle;
	m_bi.ulFlags = uFlags;
	m_bi.lpfn = BrowseCallbackProc;
	m_bi.lParam = (LPARAM)this;
}

CFolderDialog::~CFolderDialog(void)
{
}

BEGIN_MESSAGE_MAP(CFolderDialog, CCommonDialog)
END_MESSAGE_MAP()

// CFolderDialog message handlers

int CFolderDialog::DoModal(void)
{
	ASSERT_VALID(this);	
	ASSERT(m_bi.lpfn != NULL);
		
	m_bi.hwndOwner = PreModal();	
	INT_PTR nRet   = -1;
	LPITEMIDLIST pItemIDList = ::SHBrowseForFolder(&m_bi);

	if(pItemIDList != NULL)
	{
		if(::SHGetPathFromIDList(pItemIDList, m_szFolderPath))
		{
			IMalloc* pMalloc = NULL;
			if(SUCCEEDED(::SHGetMalloc(&pMalloc)))
			{
				pMalloc->Free(pItemIDList);
				pMalloc->Release();
			}
			nRet = IDOK;
		}
		else
		{
			nRet = IDCANCEL;
		}
	}
	PostModal();
	return nRet;	
}

// Overridables:

void CFolderDialog::OnInitialized(void)
{
	if(lstrlen(m_szSelectedPath))
		SetSelection(m_szSelectedPath);
}

int CFolderDialog::OnValidateFailed(LPCTSTR lpstrFolderPath)
{
	int nRet = AfxMessageBox(_T("The path entered is not valid! Continue ?"), 
				MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON2);
	
	// Return 1 = continue, 0 = EndDialog
	return((nRet == IDYES) ? 0 : 1);
}

void CFolderDialog::OnSelChanged(LPITEMIDLIST /*pItemIDList*/)
{
}

// Callback function used with the SHBrowseForFolder function. 

INT CALLBACK CFolderDialog::BrowseCallbackProc(HWND hWnd, 
							UINT uMsg, LPARAM lParam, LPARAM lpData)
{
	CFolderDialog* pThis = (CFolderDialog*)(lpData);
	pThis->m_hWnd = hWnd;
	int nRet = 0;

	switch(uMsg)
	{
	case BFFM_INITIALIZED:
		pThis->OnInitialized();
		break;
	case BFFM_SELCHANGED:
		pThis->OnSelChanged((LPITEMIDLIST)lParam);
		break;
	case BFFM_VALIDATEFAILED:
		nRet = pThis->OnValidateFailed((LPCTSTR)lParam);
		break;
	}
	pThis->m_hWnd = NULL;
	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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions