CDirTreeCtrl for displaying or selecting Folders and Files

Environment: Visual C++ 6.0 SP3

This DirTreeCtrl allows a user to display folders and Filenames(optional).
The control uses the system image list for displaying the icons for the items in
the DirTreeCtrl.

You must not manage the Icon-Resources int the ImageList. The items sorted first by
foldernames and then (if selected) by filenames.

  • You can obtain the the full PathName of an Item.
  • You can set a start path before displaying the TreeItems.
  • You can select a subpath.
  • How to use this control:

    Use it in a Dialog:

    To use the control in a Dialog do the following steps:

  • Create a CTreeCtrl in your Dialog resource.

  • Define a member variable for the CDirTreeCtrl in the Dialog header file.
  • CDirTreeCtrl m_DirTree;
    

    Insert the Following Code in your OnInitDialog function:

    BOOL CTreeDialog::OnInitDialog() 
    {
     CDialog::OnInitDialog();
     TCHAR  szWorkDir[MAX_PATH];
        
     // TODO: Add extra initialization here
     // Here we subclass our CDirTreeCtrl
     if ( !m_TreeCtrl.m_hWnd )
     {
      if ( m_TreeCtrl.SubclassDlgItem( IDC_TREE1, this ) )
      {           
       m_TreeCtrl.DisplayTree( NULL /*Display all*/, 
                               TRUE /* TRUE = Display Files*/ );
    
       _getcwd( szWorkDir, 256 );
       // set the Path to the current Work-Directory
       m_TreeCtrl.SetSelPath( szWorkDir );
      }
     }
        
     return TRUE; // return TRUE unless you set the focus to a control
     // EXCEPTION: OCX Property Pages should return FALSE
    }
    

    Using the CDirTreeCtrl in a CView

    To use this control in a CView class do the following steps:
    • Define a CTreeCtrl member in your CView header file.
    • Define a ID_TREECTRL identifier in your CView header file.
    • In the class wizard implement the create function for the CView class.
    • Modify the CView::Create function listed below
    • In the class wizard implement the WM_SIZE function.
    • Modify the CView::OnSize function listed below.
    BOOL CLeftView::Create(LPCTSTR lpszClassName, 
                           LPCTSTR lpszWindowName, 
                           DWORD dwStyle, 
                           const RECT& rect, 
                           CWnd* pParentWnd, 
                           UINT nID, 
                           CCreateContext* pContext) 
    {
     // TODO: Add your specialized code here and/or call the base class
        
     BOOL  bRet;
    
     bRet = CWnd::Create(lpszClassName, 
                         lpszWindowName, 
                         dwStyle, 
                         rect, 
                         pParentWnd, 
                         nID, 
                         pContext);
    
     // this part creates the TreeCtrl and use the CLeftView
     // as his parent Window
        
     if ( m_DirTree.m_hWnd == NULL && bRet )
     {
      bRet = m_DirTree.Create(WS_CHILD | TVS_LINESATROOT | 
                              TVS_HASBUTTONS | WS_VISIBLE | 
                              TVS_HASLINES, 
                              CRect(0, 0, 0, 0), 
                              this
                              ID_TREECTRL );
      if ( bRet )
       m_DirTree.DisplayTree( NULL, TRUE );
     }
    
     return bRet;
    }
    
    void CLeftView::OnSize(UINT nType, int cx, int cy) 
    {
     CView::OnSize(nType, cx, cy);
    
     // TODO: Add your message handler code here
     if ( m_DirTree.m_hWnd )
      m_DirTree.SetWindowPos( NULL, 0, 0, cx, cy, SWP_NOZORDER | SWP_NOMOVE );
    }
    

    Downloads

    Download demo project - 55 Kb
    Download source - 5 Kb


    Comments

    • Professor

      Posted by Geof Price on 04/15/2014 08:48am

      Can you tell me how to expand all the items in the tree on creation of the tree?

      Reply
    • network

      Posted by overseb on 09/04/2006 11:52am

      Is it possible to see the directories which are on a network ? How to do this ? thx

      Reply
    • Comment

      Posted by redhat on 09/14/2005 07:25am

      Hi Michael!  I find your article very useful especially on my search using tree view control.  Could you provide me some help on tree view control displaying or selecting data from a database?  Thank you.

      Reply
    • The code do not work to me, why?

      Posted by twisterh on 07/12/2005 11:53pm

      I downloaded the entire project and rebuilt it. Got nothing both CView as well as Dialog box as I executed it. I did it with XP and VC++ 6.0. Can anyone help me out?

      Reply
    • Forward slashes killing your root

      Posted by Legacy on 10/08/2003 12:00am

      Originally posted by: LuckY

      When calling DisplayTree() with a path as the first
      
      argument, if there are any forward slashes (/) instead of
      backslashes (\) the tree will be dead. It took me a while
      to figure out that this was my problem. So I just added
      the line below to replace forwards with backslashes. The
      problem, if you're curious is that the tree showed only
      the subdirectories of the path I was passing and I couldn't
      expand them.

      In the DisplayTree() function, insert the indicated line:
      {
      m_strRoot = strRoot;

      m_strRoot.Replace('/', '\\');// <-- ADD THIS LINE

      if ( m_strRoot.Right(1) != '\\' )
      m_strRoot += "\\";
      HTREEITEM hParent = AddItem( TVI_ROOT, m_strRoot );
      DisplayPath( hParent, strRoot );
      }

      Reply
    • Auto-scroll to show newly expanded root subtree

      Posted by Legacy on 10/08/2003 12:00am

      Originally posted by: LuckY

      One thing that was bugging me was when I expanded the network drive at the bottom of the tree, the subtree expanded out below the bottom of the window but the view didn't change. I added the two lines in the code below to scroll the expanding root item to the top of the window.
      
      

      void CDirTreeCtrl::OnItemexpanded(NMHDR* pNMHDR, LRESULT* pResult)
      {
      NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
      CString strPath;

      if ( pNMTreeView->itemNew.state & TVIS_EXPANDED )
      {
      //UINT uTest = TVIS_EXPANDEDONCE;
      ExpandItem( pNMTreeView->itemNew.hItem, TVE_EXPAND );

      // ADD these two lines
      if (GetParentItem(pNMTreeView->itemNew.hItem) == NULL)// scroll to show the
      Select(pNMTreeView->itemNew.hItem, TVGN_FIRSTVISIBLE);// newly expanded items

      // ...

      Reply
    • In Commerical Product

      Posted by Legacy on 09/27/2003 12:00am

      Originally posted by: Robert

      Can this code be freely used in a product?

      Only the core components of course, not the demo.

      ~Robert

      Reply
    • Problem: I can't subclass with just one dialog...

      Posted by Legacy on 12/15/2002 12:00am

      Originally posted by: julio

      I have tried to use this class without using the document/view framework, just using a Dialog as a main window. 
      
      I think it happens a problem in the line:

      if ( !m_TreeCtrl.m_hWnd )
      it always return false. How get I get the handle in order to subclass? is it possible

      Thanks,

      J. Gomes

      Reply
    • Can I use this code on my product?

      Posted by Legacy on 11/11/2002 12:00am

      Originally posted by: magda wurzl

      Hi,

      Is it possible to use this code on my product?

      Is there a copyright that I should include on the source code?

      Thank you,
      magda.

      Reply
    • THANKS

      Posted by Legacy on 09/19/2002 12:00am

      Originally posted by: amartin

      Great

      Reply
    • Loading, Please Wait ...

    Leave a Comment
    • Your email address will not be published. All fields are required.

    Top White Papers and Webcasts

    • Live Event Date: August 20, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT When you look at natural user interfaces as a developer, it isn't just fun and games. There are some very serious, real-world usage models of how things can help make the world a better place – things like Intel® RealSense™ technology. Check out this upcoming eSeminar and join the panel of experts, both from inside and outside of Intel, as they discuss how natural user interfaces will likely be getting adopted in a wide variety …

    • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

    Most Popular Programming Stories

    More for Developers

    Latest Developer Headlines

    RSS Feeds