CDirTreeCtrl for displaying or selecting Folders and Files | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Apr 29, 2000
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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
    }
    
    Advertisement

    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

    CodeGuru Logo

    CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

    Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

    Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.