CTreeCtrl: best way to handle check boxes?

I have a tree control with a matching CTreeCtrl object contained in a CPropertyPage-derived class.  The tree control has check boxes using the appropriate tree control style flag.  The idea is that the tree control represents a directory structure of files and which ones you want to select to put in a zip archive.  If you select a folder, then all files in that folder should become selected and vice-versa.  Except that I also have some files that must be excluded but I want to show them in the tree anyway for recognition that the file exists and has been excluded by global user options other than unchecking its checkbox in the tree control.

I want to manage these check boxes by doing the following things:

- Some items should have the check box disabled.  If I can't directly do an EnableWindow(FALSE) kind of thing on that checkbox or tree item, then I'll have to implement it simply by unchecking the box whenever it becomes checked.  But what is the best way?

- If a group becomes checked, then I want to go through and check all of its child items (except for some that don't apply like the ones that must remain unchecked at all times, which is logic that I can handle).  Likewise, if the group becomes unchecked, then I want to go through and uncheck all of its child items.

- If an item is unchecked and none of its sibling items are checked, then I want to uncheck its parent item (if it has one) and so on up the tree if applicable

I can handle the tree relationships pretty easily.  I'm pretty familiar with that sort of thing.  What I'm most curious about is the notification events and architecture I should go for to implement the check boxes...

Is there an event or notification that is ONLY for the check box state change?  Or do I have to respond to every click (TVN_CLICK) on the tree control and then check the state of every item in the tree to see if something changed?  If so does that mean I need to keep a completely parallel copy of the data represented by the tree in order to do this comparison?

Is it better to handle these events in the CPropertyPage class that contains the tree cotnrol, or should I derive a class from CTreeCtrl and handle it all in there?

Is there a way to do a tri-state checkbox in a tree control?  And is there a way to EnableWindow(FALSE) or the equivalent on just one item of a tree control, or just one item's check box?  I want to know the best possible way to represent 4 states of items in the tree: unchecked, partly checked (contains some items that are checked and some that are not), fully checked (either doesn't have children or all children are also checked), and uncheckable (disabled).  If there's no way to do everything I want, I'd rather suffer the limitations of the built-in checkbox support in the tree control than create my own set of checkbox images and write my own checkbox handling code to duplicate that functionality on my own.  That is to say that if I can't get all 4 states that I want using the built-in checkbox support, I'll settle for the default two states (checked and unchecked) and work around that.
Who is Participating?
mblatConnect With a Mentor Commented:
Best way to have 4 state checkbox I know of is not to use standard checkbox, but load different images for tree controll items depending on a state.  Images of cause can look like a check box...  It is pretty easy, so if 4 state is what you want - go this route and don't compromise with 2 state.

There are bunch of samples on how to do it here..


I personnaly ALWAYS prefer to derive class and handle this kind of situation in child control.  I find it much cleaner and easier to reuse later on...

I don;t think that you can disbale individual tree control item, but then again if you derive your class it is pretty easy to simulate.  See above for samples, especially ones that allow you to draw items in different colors.  You may also want to consider owner-draw tree control, even though it IS a lof of headache...

Hope it helps...

About a year ago I implemented something quite similar to what you described here.
I used, like mblat suggested, a set of 5 images (in my case I also had a disabled-checked image), and implemented the hiererchical functionality myself.

I don't know of a way to do it using the basic tree ctrl check-boxes (not tri-state nor disable item).

If you want I can give you the code, but it will take some time to generalize it.
risAuthor Commented:
Thanks, that link was very helpful.  I guess that's what I'll do (use custom images).  From the examples, it looks less difficult than I thought it would be.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.