Click here to Skip to main content
15,887,989 members
Articles / Desktop Programming / WTL
Article

WTL CFileTreeCtrl class

Rate me:
Please Sign up or sign in to vote.
3.19/5 (13 votes)
22 Apr 20031 min read 70.3K   2.6K   26   8
Implements a tree control similar to the left hand side of Windows Explorer

Sample Image - wtlfiletreectrl.jpg

Credit and Acknowledgments

The original author of FileTree MFC control is PJ Naughter. This control and article can be found at http://www.codeproject.com/treectrl/filetree.asp

Overview

I needed a FileTree control to use in an ATL/WTL project and I decided to port MFC Control to WTL. The following ATL files contain the port:

  • FileFind.h/FileFind.cpp - Contain the CFileFind class and implementation for WTL which performs local file searches
  • WtlFileTreeCtrl.h/WtlFileTreeCtrl.cpp - Contain the CWtlFileTreeCtrl class and implementation for WTL

How to use the control in your WTL App:

  1. Create a WTL Project
  2. Design the dialog and add the Tree control
  3. Add the WtlFileTreeCtrl.h header file to your project
  4. Assign a CWtlFileTreeCtrl to your tree.
  5. In OnInitDialog(), subclass tree control (CWtlFileTreeCtrl) to ID using the SubclassWindow method.
  6. In your dialog, don't forget to add the macro REFLECT_NOTIFICATIONS which allows the CWtlFileTreeCtrl to get messages like TVN_ITEMEXPANDING.
#include "WtlFileTreeCtrl.h"

//...

class CMainDlg : public CDialogImpl<CMainDlg>
{
     BEGIN_MSG_MAP(CMainDlg)
          ...
          MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
          REFLECT_NOTIFICATIONS()
     END_MSG_MAP()
     ...
     LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, 
         LPARAM /*lParam*/, BOOL& /*bHandled*/);
     ...
     CWtlFileTreeCtrl m_Tree;
     ...
};

//...

LRESULT CMainDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, 
        LPARAM /*lParam*/, BOOL& /*bHandled*/) 

{
     ...
     m_Tree.SubclassWindow( ::GetDlgItem( m_hWnd, IDC_TREE1 ) );
     ...
}

Requirements

You will require the WTL Libraries, these can be downloaded from the microsoft site.

Note - This control uses the STL std::string class and the STL std::vector template class.

Contacting the Author

Ilya Solnyshkin, E-mail: isolnyshkin@yahoo.com, Web: http://www.printsniffer.com/

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
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralBug: GDI object leak Pin
Leon Oskam9-Aug-09 7:39
Leon Oskam9-Aug-09 7:39 
Hi Ilya,

I don't know if you are still maintaining this control but I figured I'd drop you a line just incase. There is a small bug in the current version of this control that causes it to leak GDI objects. You can check this by running the demo app, opening windows task manager and enabling the "GDI objects" column there. If you start browsing your hard drive with the file tree control you will see the amount of allocated GDI objects for this process will skyrocket and it just keeps climbing as you open more directories (especially directories containing a lot of items).

The problem lies in the GetIconIndex() and GetSelIconIndex() methods, which invoke SHGetFileInfo() with the SHGFI_ICON flag set. This causes SHGetFileInfo() to return an icon handle for each file which is stored in the hIcon member of the local instance of the SHFILEINFO struct but which is never actually freed (and since the sfi variable goes out of scope you lose the handle eventhough it remains allocated).

int CWtlFileTreeCtrl::GetIconIndex( const std::string sFilename )
{
	// Retreive the icon index for a specified file/folder
	SHFILEINFO sfi;
	ZeroMemory(&sfi, sizeof(SHFILEINFO));

	if( SHGetFileInfo( sFilename.c_str(), 0, &sfi, sizeof(SHFILEINFO), SHGFI_ICON|SHGFI_SMALLICON ) == 0 )
		return -1;
	return sfi.iIcon;
}


It should either be freed with DestroyIcon() or better yet the flags should be changed so that it no longer generates icon handles in the first place since the handles aren't actually used anywhere. I've changed it to the following line of code myself:

if (SHGetFileInfo(sFilename.c_str(), 0, &sfi, sizeof(SHFILEINFO), SHGFI_SYSICONINDEX | SHGFI_SMALLICON ) == 0 )


The same needs to be done for GetSelIconIndex(). Hope this is helpful to someone.

Regards,

-Leon
Generalbug Pin
Dmitry AE15-Nov-07 23:04
Dmitry AE15-Nov-07 23:04 
GeneralRe: bug Pin
toddma21-Sep-11 9:56
toddma21-Sep-11 9:56 
GeneralRe: bug Pin
toddma23-Sep-11 2:27
toddma23-Sep-11 2:27 
GeneralTo make it compile [modified] Pin
jsanjosem5-Mar-07 23:57
jsanjosem5-Mar-07 23:57 
Generalerror of link Pin
Member 6289183-Apr-05 23:48
Member 6289183-Apr-05 23:48 
GeneralRe: error of link Pin
Ilya Solnyshkin4-Apr-05 6:04
Ilya Solnyshkin4-Apr-05 6:04 
Generalbugfix Pin
evbersen16-Dec-03 0:58
evbersen16-Dec-03 0:58 

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.