CTreeCtrl and CTreeView (Windows Explorer)

Posted on 2009-04-10
Last Modified: 2013-11-20
I'm using VS 6.0 and MFC and working on creating a FTP client program where my main window has three splitter windows. The first one far to the left uses a TreeView structure and the next two windows over are both ListView structures.  The first splitter window (TreeView) should display only local folders on the C: drive which I have set as the default drive when the program starts up. This same window should allow users to navigate around these folders. The middle splitter window is a ListView will displays the contents (folders and files) of the selected or highlighted folder from the TreeView. The far right splitter window displays the folders and files of the remote FTP site.

My delima is two folds. First how do I implement the TreeView splitter window which allows users to navigate the folders and only folders (no files in this view) to sub folders just like Windows explorer of the C:\ drive. I am populating this TreeView window with some code but it does not work in that it does not show the folders correctly once refreshed.  I have a drop down combo box (not implemented) which allows users to change the selected drive to the other logical available drives on the system.  I have tried writing recursive code for this TreeView window since there are going to be a limited number of recursive calls but my recursive function does not work. I would be happy to paste the preliminary code if it would help. Another words I take the selected node and recurse up the ladder until I reach the top or root node. Once there my function returns and rebuilds the TreeView but only inserting child/parent nodes until we are back to the selected node then at which I want to be able to display all sibling nodes of the one which was selected.

Secondly how do I best inform/call the middle ListView structure once the user selects a folder from the TreeView window ? Can I use custom SendMessage function ? Would this be an elegant method on how to do this ? I had issues compiling when I tried declaring pointers inside the TreeView and ListView pointing to the other class/object.  Hence my reason for suggesting SendMessage function.

I am open to any suggestions and advice. I have also attached my entire project for VS6.0.

Code can be found here
Then click on the Download link at the top left. 

Open in new window

Question by:VicMe
  • 3
  • 2
  • 2
LVL 49

Expert Comment

ID: 24120039
As to the second part, the MFC Document/View architecure has a standard way to do this. In your CDocument-derived object, call:
Call this when the selection changes in the tree view. For instance:
    (in CMyTreeView)
    GetDocument()->UpdateAllViews( this, MYHINT_FOLDER_SEL_CHANGED );
Then your List View will get an OnUpdate call. That's where you will check to see if the document has changed and do anything that makes the view look correct.
In similar projects, I made my "document" an internal representation of the disk hierarchy -- in one case a single CStringArray with one item for every folder, in another case, my "document" was a more complex linked-list structure... but the idea is the same.

Author Comment

ID: 24120626
Thanks Dan. I got the second part working in my project. Appreciate the reply. Any chance you can take a look at the code posted to find out why the first part does not work ?
LVL 49

Accepted Solution

DanRollins earned 500 total points
ID: 24123931
There is some good example code here:
    populating tree control from text file
Including links to two projects that include "populating the tree from disk" logic:

Be aware that if you use a recursive function, it may take a very long time to populate the entire tree.  Hard disks these days are huge.  Even if you only seek the names of folders, it might take several minutes.  Try the DOS command
   TREE C:\
to see what I mean.  Windows needs to do a FindFirst/FindNext sequence.  The first time you run it, it is rather slow.  The second time, the folders are in cache and it's faster.
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Expert Comment

ID: 24124223
Thanks again for the advice. I have decided to abandon the recursion for now partly because II have found the following article/code which I am hoping to port over.

There is a class called CDirTreeCtrl : public CTreeCtrl that this author has created that does all of the populating that I could possible need within the CTreeView. What I need help in now is how do I go about integrating this class along with the class CSortStringArray which the author appears to have linked to the CDirTreeCtrl ?

I have also taken a look at the following article which the author (different) talks about implementing a CTreeView or CListView using a SDI application also.

If you can possible step me through all of this because I am confused about on how to integrate this class. There is a method called GetTreeCtrl(); which is part of the frameworks CTreeView do I use this or do I create or declare my own ? Please keep in mind that my application if you see the project attachment uses a 4 way splitter first view is dereived from CTreeView and the next two are CListView and there is a smaller one also which uses the standard View which the project uses when created.

Thanks again

Expert Comment

ID: 24124398
Guess what Dan ?

I got it working by using both the links you had posted and the one I had above posted. But what really got me frustrated and very unsure of what I was doing was that the handler  ON_NOTIFY_REFLECT(TVN_ITEMEXPANDED, OnItemexpanded) implemented in the CDirTreeCtrl class  was not being called by the framework so my TreeCtrl was not being populated at all except for the root node/drive.

I found the work around by reading the below article at CodeGuru and following the suggestions by Microsoft. I guess I was close but really appreciate the links you gave. I downloaded both of them and followed the code closely.;en-us;148533

Author Closing Comment

ID: 31569057
Thanks Dan again. Sorry for the wrong user name posting. I forgot that I had two accounts. Cheers
LVL 49

Expert Comment

ID: 24124462
I'm glad that your own research was able to focus in th right places.  Thanks for the grade and the points.  If you run into related problems,  be sure to click the "Ask Related Question" (in this thread) so that I will be notified.
-- Dan

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question