CDirTreeCtrl for displaying or selecting Folders and Files
Posted
by Michael Hofer
on April 29th, 2000
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.
How to use this control:
Use it in a Dialog:
To use the control in a Dialog do the following steps: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 KbDownload source - 5 Kb

Comments
network
Posted by overseb on 09/04/2006 11:52amIs it possible to see the directories which are on a network ? How to do this ? thx
ReplyComment
Posted by redhat on 09/14/2005 07:25amThe code do not work to me, why?
Posted by twisterh on 07/12/2005 11:53pmI 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?
ReplyForward slashes killing your root
Posted by Legacy on 10/08/2003 12:00amOriginally posted by: LuckY
ReplyAuto-scroll to show newly expanded root subtree
Posted by Legacy on 10/08/2003 12:00amOriginally posted by: LuckY
ReplyIn Commerical Product
Posted by Legacy on 09/27/2003 12:00amOriginally posted by: Robert
Can this code be freely used in a product?
Only the core components of course, not the demo.
~Robert
ReplyProblem: I can't subclass with just one dialog...
Posted by Legacy on 12/15/2002 12:00amOriginally posted by: julio
ReplyCan I use this code on my product?
Posted by Legacy on 11/11/2002 12:00amOriginally 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,
Replymagda.
THANKS
Posted by Legacy on 09/19/2002 12:00amOriginally posted by: amartin
Great
ReplyThanks, it's a very good code.
Posted by Legacy on 07/22/2002 12:00amOriginally posted by: Dennis L.
Thanks, it's a very good code.
ReplyIt helped me in my developing and I added a drag&drop methods.
I think it can be really usefull for beginner programmer cause it has a large functionality.
Loading, Please Wait ...