Click here to Skip to main content
Licence 
First Posted 5 Sep 2000
Views 218,075
Bookmarked 73 times

Directory and network browsing using a tree control

By | 5 Sep 2000 | Article
An article explaining browsing my computer and the network using a TreeCtrl

Sample Image - PathPicker.gif

Introduction

This dialog class displays a directory tree similar to the one found in the "Find in Files" Browse button as shown in the image above.

I have noticed several applications requiring a user to select a path that often use CFileDialog which does not fit the task very well. A better option is to use a Tree control but I have not found any that allow access to the network other than via mapped drives. Both methods lack the style that "Find in Files" provides. I have also spent some time trying to use SHBrowseForFolder but to date have had little success.

Why not just use SHBrowseForFolder.

Although SHBrowseForFolder provides all this functionality and more, it does come with the following drawbacks:-

  • Documentation is difficult to understand and lacks good examples.
  • Syntax is very cryptic.
  • Many lines of code plus callbacks are necessary to do simple tasks such as set the initial path.
  • The SHBrowseForFolder dialog cannot be customized to any great extent. For example, changing the dialog's layout or adding a control showing information about the selected directory is not possible.

How to use it.

I have developed this code into a dialog for easy implementation. The source is written such as to put the dialog object into a single class so it can be hacked out and used in any willing developers code. To use:-

  1. Copy the source files DlgGetPath.cpp and DlgGetPath.h to your project directory and add them to your project.
  2. From within your project, open the TryNetworkTree.rc file and drag the Bitmap (IDB_FILE_TYPES) and dialog (IDD_GET_PATH) into your project.
  3. Add the mpr.lib library in, to be linked into your application. To do this select 'Project->Settings' to open the project settings dialog. Select the 'Link' tab and from the drop down box select the 'General' category. In the 'Object/Link modules' edit box, type in mpr.lib
  4. Add the following code where you want to activate the picker dialog, for instance within a button handler.
#include "DlgGetPath.h"      //Definition to get path
...
void CBlarOnButtonBrowse() 
{
	CDlgGetPath dlgPath( this );
	dlgPath.SetPath( _T("C:\\Temp") );
	if( dlgPath.DoModal() == IDOK )
	{
		//Do some cool stuff with it here
		//SetDlgItemText( IDC_EDIT_SEARCH_IN, dlgPath.GetPath() );
	}
}

Using selection event.

When the user selects a branch in the tree, it is possible to catch this event and use the selected path to perform some action. For example, Windows Explorer uses the selection of a branch in the left pane to display the directory in the right pane. To do this, place code at the end of CDlgGetPath::OnSelchangedTree at around line 391. The path is available and is displayed at the top of the dialog for this sample.

	//
	//TODO add code here to process users selection
	//
	ASSERT( GetDlgItem( IDC_STATIC_NOTE ) );
	SetDlgItemText( IDC_STATIC_NOTE, sPath );

Enhancements.

  • Display the recycle bin appropriately.
  • Display the contents of the recycle bin with out S- numbers.
  • Sort items when added except for 'Entire Network'.
  • Check on a more complex network.
  • Determine why icon disappears from the ALT-TAB bar when active?
  • Handle WNetEnumResource for more than 16384 bytes of data.
  • It would be nice to read the system icons with SHGetFileInfo and use them.

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

About the Author

John McTainsh



United States United States

Member

I starting writing code when Apple IIe was all the rage (1982?). Graduated Mechanical Engineering in New Zealand, so I could build Robots. Took up Commerical diving in Asia for a few years to make some quick money (Spent it all). I have been writing C/C++/Assembly and building underwater robots since MS QuickC/Quick Assembler in 1989. I currently live in Australia and work mosly with MS VisualC.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralTanks Pinmemberpeterpc070122:17 20 Mar '12  
QuestionAdding "Create New Folder" Button Pinmembervoytello22:41 22 Mar '06  
Generalto view files Pinmemberleovinci21:40 9 Dec '04  
GeneralSHBrowseForFolder PinsussAnonymous23:20 17 Mar '04  
GeneralVery nice control !!! Pinmemberyooyo3d6:53 9 Jan '04  
GeneralGreat dialog, one tiny remark+sollution PinmemberFreddyH4:37 14 Nov '03  
GeneralBug !!!!! Pinmemberyannickn8:03 30 Oct '03  
GeneralRe: Bug !!!!! PinmemberFreddyH4:58 14 Nov '03  
GeneralFix for Fatal Error on 'Cancel'. Pinmemberbraddenn5:08 3 Sep '03  
GeneralDoes the job perfectly PinsussEdgecrusher14:31 11 May '03  
GeneralIcons Pinsusspr0phet3:23 8 May '03  
QuestionHow to display a turecolor image or icon in the treectrl? PinmemberJack_Cai3:37 24 Apr '03  
GeneralGreat work shame it cant view files Pinmembercraigbennett3:24 12 Apr '03  
GeneralBug. PinsussLee W. Spencer11:42 12 Nov '02  
GeneralRe: Bug. PinmemberDon Grout11:20 26 Feb '03  
GeneralRe: Bug. Pinmembermintguy14:11 23 Jul '04  
GeneralUrgent Help!!! PinmemberIvano22:40 31 Jul '02  
GeneralSorting this PinmemberJudd4:50 23 May '02  
QuestionHow to use it in Embedded VC Pinmembermingming_li17:24 21 May '02  
GeneralSHBrowseForFolder.. PinmemberPhilip Patrick22:12 6 Feb '02  
Yes it can be customized. And I use it all the time, working just fine. Of course, when you do your own, you have more options to customize it. But why not to make your own Windows so? Smile | :)
But have to say, you have done a good work, and for one who need more customizations and/or have to handle all code, your one is better.
BTW, I'll copy here an example of using SHBrowseForFolder from one of my projects. Usage is simple enough Smile | :)
	BROWSEINFO bi;
	ZeroMemory(&bi, sizeof(BROWSEINFO));
	TCHAR szDiplayName[MAX_PATH];
	ZeroMemory(szDiplayName, MAX_PATH);
	LPITEMIDLIST lpStartFolder = NULL;
	SHGetSpecialFolderLocation(GetSafeHwnd(), CSIDL_DRIVES, &lpStartFolder);
 
	bi.hwndOwner = GetSafeHwnd();
	bi.pszDisplayName = szDiplayName;
	bi.lpszTitle = _T("Choose a folder");
	bi.ulFlags = BIF_USENEWUI;
	bi.pidlRoot = lpStartFolder;
 
	LPITEMIDLIST pIDList = SHBrowseForFolder(&bi);
	LPMALLOC pMalloc = NULL;
	SHGetMalloc(&pMalloc);
	if(pIDList != NULL){
		SHGetPathFromIDList(pIDList, m_csLocation.GetBuffer(MAX_PATH));
		m_csLocation.ReleaseBuffer();
		pMalloc->Free(pIDList);
	}
	pMalloc->Free(lpStartFolder);
	pMalloc->Release();

 
Philip Patrick
"Two beer or not two beer?" (Shakesbeer)
Web-site: www.saintopatrick.com
GeneralRe: SHBrowseForFolder.. PinmemberNaeim Semsarilar18:16 6 Mar '02  
GeneralRe: SHBrowseForFolder.. PinmemberJohn McTainsh22:58 6 Mar '02  
GeneralRe: SHBrowseForFolder.. PinmemberNaeim Semsarilar1:28 7 Mar '02  
GeneralUsing SHBrowseForFolder with specified initial folder PinmemberNaeim Semsarilar2:31 7 Mar '02  
GeneralRe: Using SHBrowseForFolder with specified initial folder PinmemberAnonymous4:36 13 Mar '02  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120528.1 | Last Updated 6 Sep 2000
Article Copyright 2000 by John McTainsh
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid