Click here to Skip to main content
Click here to Skip to main content
Go to top

File tree control allowing selection of multiple files and folders

, 4 Jun 2007
Rate this:
Please Sign up or sign in to vote.
MFC tree control showing the files in a manner similar to the left-hand side of Windows Explorer, but allowing selection of multiple files and folders for further processing in your program

Sample Image - FileTreeCtrl.jpg

Introduction

The provided CFileTreeCtrl is the MFC tree control, showing files in a manner similar to the left-hand side of Windows Explorer. Contrary to Windows Explorer, however, it allows the selection of multiple files and folders for further processing in your program. You can do this using the check boxes to the left of every file and folder. When selecting a folder, you can specify whether to include or not include the files from the subfolders. At any time, you can request that the control give you the lists of selected files and folders. You can also get information on how many files are selected in any folder.

States of check boxes

The following states of check boxes are supported:

check03 the folder does not contain any files
check01 the folder or file is not selected
check02 the file is selected
check04 all the files from the folder are selected, including the files of some subfolders
check05 all the files from the folder are selected, including the files from all subfolders
check06 all the files from the folder are selected, excluding the files from all subfolders
check07 some files in the folder are selected, files from subfolders are not
check08 some files in the folder are selected, as well as from all subfolders
check09 some files in the folder are selected, as well as from some subfolders
check10 the files from all subfolders are selected, but no files in the folder itself
check11 the files from some subfolders are selected, but no files in the folder itself

You can toggle through the states of check boxes, clicking on the item with a mouse.

Implementation of CFileTreeCtrl

  • Add the standard Tree Control to your dialog box or form view. Make sure it has check boxes style enabled. Use the More Styles tab in Tree Control properties.
    Tree Control properties
  • Add a new bitmap resource into your project and name it IDB_STATE. The BMP file for this resource is included with the source files. It includes 16 images for different states of check boxes. 5 of them are empty, though.
  • Then you can derive a new class from CFileTreeCtrl if you don't want to include all of the files into the control, but only the files of a specific type. To do this, in your derived class you need to override the virtual function MatchExtension. See below for details.
  • After this, you need to use the Class Wizard to add a member variable for the Tree Control resource to your dialog class. Then go to the header file for the dialog and change the type of this variable in the AFX_DATA section from CTreeCtrl to the name of your CFileTreeCtrl derived class.

CFileTreeCtrl class members

Here, I will describe only the public members of CFileTreeCtrl and the virtual function MatchExtension. The function of others you can guess from the source code.

BOOL DisplayTree()

Call this function to fill your file tree control with files. Usually, it is done in OnInitDialog() or in OnInitialUpdate(). The function returns true, if successful.

void AddHidFolder(int nFolder, CString strPath="")

Call this function before calling DisplayTree() to tell the control not to display certain folders. You can specify either the CSIDL identifier of the special folder (using nFolder), or the full path to the folder (using strPath; in this case, nFolder should be 0). Usually, you don't want to show the Recycle Bin and the Control Panel. So, add these lines to your program before calling DisplayTree():

 // m_FileTree is the member variable for the Tree Control
 m_FileTree.AddHidFolder(CSIDL_BITBUCKET); 
 m_FileTree.AddHidFolder(CSIDL_CONTROLS);

CString GetInfo(HTREEITEM hItem)

Gets the string of information on how many files and folders are selected in the folder. This is indicated by hItem, the handle to the item of Tree Control. The string is returned in this format: "Files: 15; Dirs: 22; SelFiles: 0; SelDirs: 0; PartDirs: 3", where Files is the total number of files in a folder, Dirs is the total number of subfolders, SelFiles is the number of selected files, SelDirs is the number of fully selected subfolders and PartDirs is the number of partially selected subfolders. If the item is a file or folder that has not been expanded yet, all the elements are -1. For example, to get the info on the currently selected folder, you can use:

m_strText = m_FileTree.GetInfo(m_FileTree.GetSelectedItem());

int GetSelectedFiles(CString* &SelFiles)

Returns the number of the selected files. The SelFiles parameter points to the array of strings containing the full names of all selected files. The memory for the array is allocated inside the GetSelectedFiles, but you need to delete it yourself using delete[] SelFiles.

int GetSelectedFolders(CString* &SelFolders)

Returns the number of the selected folders that have not been expanded yet. The SelFolders parameter points to the array of strings containing the full names of the selected folders without the closing "\". The first character of the returned strings indicate the type of the selection: "f" means only files within the folder (check06), "F" means only subfolders (check10) and "A" means all the files and all the subfolders (check05). The memory for the array is allocated inside GetSelectedFolders, but you need to delete it yourself using delete[] SelFolders.

CString GetSelectedFile()

Returns the string containing the full name of the currently selected item in the File Tree Control.

virtual bool MatchExtension(CString file)

Override this virtual function in your CFileTreeCtrl derived class in case you don't want to include all the files into the control, but only the files of a specific type. The function gets the name of the file; it should return true if the file should be included and false otherwise. For example, if you want to show only EXE files, do the following:

bool CMyFileTreeCtrl::MatchExtension(CString file)
{
  CString strTemp = file.Right(4);
  if (!strTemp.CollateNoCase(".EXE")) return TRUE;
  return FALSE;
}

If you do not want to show files at all, but only folders, override the function as follows:

bool CMyFileTreeCtrl::MatchExtension(CString file)
{
   return FALSE;
}

Acknowledgement

I would like to thank Johnson Zhou and PJ Naughter. I used some portions of their code in my class. I have also used one of the samples in MSDN and took the major portions of code from there, but now, unfortunately, I do not remember exactly which sample I used. I could have probably used the code of some other members of CodeProject, too. If you see your code in my source, tell me and I will include you in the Acknowledgement section. Thanks to all the members and the staff of the Code Project! You are all doing the wonderful job of making programming fun!

History

  • 19 January, 2006 -- Original version posted
  • 4 June, 2007 -- Article edited and posted to the main CodeProject.com article base

License

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

Share

About the Author

Victor Ricklefs

Kazakstan Kazakstan
No Biography provided

Comments and Discussions

 
Bugbug at CFileTreeCtrl::FillTreeView PinmemberSinanGokkaya2-Jan-14 1:55 
GeneralMy vote of 1 Pinmemberstaffan_v12-Oct-12 2:31 
GeneralRe: My vote of 1 PinmemberVictor Ricklefs12-Oct-12 8:19 
QuestionSee files created after the applications is started Pinmembererau7111-Jun-12 10:11 
QuestionUnable to get selected folder list when selecting a sub folder first and then its parent folder PinmemberamaCoder9-Jan-12 7:33 
Generalgood Pinmember09dzxue5-Mar-11 5:31 
GeneralMy vote of 5 Pinmemberleechoohyoung12-Jan-11 18:47 
GeneralMy vote of 5 PinmemberMember 14304118-Oct-10 20:15 
GeneralCan't hide control panel in the Window7 & vista Pinmemberxw32784-Jan-10 20:48 
GeneralRe: Can't hide control panel in the Window7 & vista PinmemberVictor Ricklefs5-Jan-10 4:32 
Generalthank you very much Pinmembera1188826-May-09 22:33 
GeneralThank you! Pinmemberlzd200216835-Dec-08 16:39 
GeneralWhen Running FileTreeDialog PinmemberMember 287643917-Sep-08 9:53 
GeneralRe: When Running FileTreeDialog PinmemberVictor Ricklefs17-Sep-08 22:52 
GeneralRe: When Running FileTreeDialog PinmemberMember 287643918-Sep-08 1:09 
GeneralRe: When Running FileTreeDialog PinmemberMember 287643918-Sep-08 5:41 
GeneralRe: When Running FileTreeDialog PinmemberVictor Ricklefs18-Sep-08 6:59 
GeneralRe: When Running FileTreeDialog PinmemberMember 287643918-Sep-08 9:38 
GeneralRe: When Running FileTreeDialog PinmemberNagender19-Feb-09 5:02 
Questionquestion about multiple selection files & folders (not expanded) Pinmemberzikzik22-May-08 5:58 
AnswerRe: question about multiple selection files & folders (not expanded) PinmemberVictor Ricklefs23-May-08 4:48 
QuestionVery very nice...what about correct nr of sel.files? Pinmemberrootdial13-Jun-07 12:24 
AnswerRe: Very very nice...what about correct nr of sel.files? PinmemberVictor Ricklefs13-Jun-07 17:49 
GeneralRe: Very very nice...what about correct nr of sel.files? Pinmemberrootdial13-Jun-07 19:20 
GeneralRe: Very very nice...what about correct nr of sel.files? PinmemberVictor Ricklefs14-Jun-07 6:07 
GeneralMany thanks [modified] But grandchild! Pinmemberhuyvqson30-May-07 0:11 
GeneralRe: Many thanks [modified] But grandchild! PinmemberVictor Ricklefs12-Jun-07 4:34 
GeneralRe: Many thanks [modified] But grandchild! Pinmemberhuyvqson12-Jun-07 16:37 
QuestionControl in ActiveX Pinmemberdp6320-May-07 3:59 
AnswerRe: Control in ActiveX PinmemberVictor Ricklefs28-May-07 7:11 
QuestionGreat! How to add .lnk, .pif PinmemberPierre Couderc4-Nov-06 5:25 
Questiongreat stuff Pinmemberrudozkv16-Oct-06 5:25 
GeneralUseful and a geat time saver, many thanks PinmemberJerry Evans15-Sep-06 6:42 
Generalvery good Pinmembermicrobit27-Aug-06 4:11 
Questionnice work, do you do consulting? PinmemberShawn Javid22-Aug-06 9:10 
QuestionHow to save selected files and reload them Pinmembermmakrzem5-Apr-06 15:57 
AnswerRe: How to save selected files and reload them PinmemberAble B.28-Jul-07 0:32 
GeneralRe: How to save selected files and reload them Pinmembermmakrzem28-Jul-07 1:59 
Generalvery very very useful! Pinmemberzsabo20-Mar-06 16:22 
GeneralError when cliked on the removable drive PinmemberKaren0_08-Mar-06 19:58 
GeneralRe: Error when cliked on the removable drive PinmemberVictor Ricklefs9-Mar-06 6:05 
GeneralRe: Error when cliked on the removable drive PinmemberKaren0_09-Mar-06 20:54 
GeneralRe: Error when cliked on the removable drive Pinmemberzsabo20-Mar-06 16:15 
GeneralGreat control,but How can i Serialize the "CMap SelFiles" to a file PinmemberJimOcean30-Jan-06 2:23 
QuestionRe: Great control,but How can i Serialize the "CMap SelFiles" to a file PinmemberVictor Ricklefs30-Jan-06 4:41 
AnswerRe: Great control,but How can i Serialize the "CMap SelFiles" to a file PinmemberJimOcean30-Jan-06 6:54 
AnswerRe: Great control,but How can i Serialize the "CMap SelFiles" to a file PinmemberVictor Ricklefs3-Feb-06 5:49 
GeneralNice control, but several bugs... PinmemberKochise30-Jan-06 2:13 
AnswerRe: Nice control, but several bugs... PinmemberVictor Ricklefs30-Jan-06 5:04 
GeneralRe: Nice control, but several bugs... PinmemberKochise30-Jan-06 21:18 

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.

| Advertise | Privacy | Mobile
Web03 | 2.8.140921.1 | Last Updated 4 Jun 2007
Article Copyright 2006 by Victor Ricklefs
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid