Click here to Skip to main content
15,860,861 members
Articles / Desktop Programming / MFC

Directory and Network Browsing Using a Tree Control

Rate me:
Please Sign up or sign in to vote.
4.93/5 (38 votes)
5 Sep 2000 284.2K   9K   78   47
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.
C++
#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.

C++
//
//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.


Written By
United States United States
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.

Comments and Discussions

 
GeneralIcons Pin
lone ranger8-May-03 3:23
lone ranger8-May-03 3:23 
QuestionHow to display a turecolor image or icon in the treectrl? Pin
Jack_Cai24-Apr-03 3:37
Jack_Cai24-Apr-03 3:37 
GeneralGreat work shame it cant view files Pin
craigbennett12-Apr-03 3:24
craigbennett12-Apr-03 3:24 
GeneralBug. Pin
Lee W. Spencer12-Nov-02 11:42
sussLee W. Spencer12-Nov-02 11:42 
GeneralRe: Bug. Pin
Don Grout26-Feb-03 11:20
Don Grout26-Feb-03 11:20 
GeneralRe: Bug. Pin
mintguy23-Jul-04 14:11
mintguy23-Jul-04 14:11 
GeneralUrgent Help!!! Pin
Ivano31-Jul-02 22:40
Ivano31-Jul-02 22:40 
GeneralSorting this Pin
Kyudos23-May-02 4:50
Kyudos23-May-02 4:50 
QuestionHow to use it in Embedded VC Pin
mingming_li21-May-02 17:24
mingming_li21-May-02 17:24 
GeneralSHBrowseForFolder.. Pin
Philip Patrick6-Feb-02 22:12
professionalPhilip Patrick6-Feb-02 22:12 
GeneralRe: SHBrowseForFolder.. Pin
Naeim Semsarilar6-Mar-02 18:16
Naeim Semsarilar6-Mar-02 18:16 
GeneralRe: SHBrowseForFolder.. Pin
John McTainsh6-Mar-02 22:58
John McTainsh6-Mar-02 22:58 
GeneralRe: SHBrowseForFolder.. Pin
Naeim Semsarilar7-Mar-02 1:28
Naeim Semsarilar7-Mar-02 1:28 
GeneralUsing SHBrowseForFolder with specified initial folder Pin
Naeim Semsarilar7-Mar-02 2:31
Naeim Semsarilar7-Mar-02 2:31 
GeneralRe: Using SHBrowseForFolder with specified initial folder Pin
13-Mar-02 4:36
suss13-Mar-02 4:36 
GeneralGreat! Thanks! Pin
TeleStar25-Sep-02 15:01
TeleStar25-Sep-02 15:01 
GeneralDoes this code generate a crash or I use it incorrectly ? Pin
snakekaa21-Oct-02 5:44
snakekaa21-Oct-02 5:44 
GeneralRe: Using SHBrowseForFolder with specified initial folder Pin
snakekaa21-Oct-02 6:04
snakekaa21-Oct-02 6:04 
GeneralRe: SHBrowseForFolder.. Pin
Philip Patrick15-May-02 6:35
professionalPhilip Patrick15-May-02 6:35 
GeneralPerfect Pin
jesmithjr21-Jan-02 5:14
jesmithjr21-Jan-02 5:14 
GeneralNice but no My Documents Pin
Swinefeaster28-Dec-01 13:54
Swinefeaster28-Dec-01 13:54 
GeneralFiles inside the folders Pin
30-Jul-01 22:56
suss30-Jul-01 22:56 
GeneralRequires help Pin
9-Jul-01 20:59
suss9-Jul-01 20:59 
GeneralUrgent Help Pin
18-Mar-01 19:35
suss18-Mar-01 19:35 
GeneralIdentifying Unix machines connected to LAN Pin
11-Dec-00 19:45
suss11-Dec-00 19:45 

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

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