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()
    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,

    // 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 |
    CRect(0, 0, 0, 0),
    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 );


    Download demo project - 55 Kb

    Download source - 5 Kb

    More by Author

    Must Read