Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Check boxes in tree views

I need help using check boxes in a CTreeCtrl and CTreeView. I have figured
out how to display the
check boxes, and I am able to use SetCheck() and GetCheck() to communicate
with the check boxes.
But I can't figure out if there are other ways to interact with the check
boxes.

I want the check boxes to work in such a way that checking a parent item
also causes all the child items
to be checked. Most programs that have tree views with check boxes work that
way, but that
functionality doesn't seem to be built into the tree view. I also want the
program to be notified
when a box is checked, so that the objects corresponding to the tree items
can be notified that
they have been selected or deselected.

I have tried using ON_WM_LBUTTONUP() to call a function that determines whether
an item has
been checked, but it doesn't work very well. The function isn't called if
I single-click on the check box.
I have to double-click the item, or single-click somewhere else in order
to call the function.

Does the CTreeView generate any messages that I can intercept notifying Windows
or the CTreeCtrl
that an item in the tree has been checked? Or is there some other method
that is more commonly
used with check boxes?

Thanks,
Cliff Lewis
[1381 byte] By [Cliff Lewis] at [2007-11-10 12:51:12]
# 1 Re: Check boxes in tree views
Hello,
The following example function illustrates how to set an item's check state.

BOOL TreeView_SetCheckState(HWND hwndTreeView, HTREEITEM hItem, BOOL fCheck)
{
TVITEM tvItem;

tvItem.mask = TVIF_HANDLE | TVIF_STATE;
tvItem.hItem = hItem;
tvItem.stateMask = TVIS_STATEIMAGEMASK;

/*
Since state images are one-based, 1 in this macro turns the check off,
and
2 turns it on.
*/
tvItem.state = INDEXTOSTATEIMAGEMASK((fCheck ? 2 : 1));

return TreeView_SetItem(hwndTreeView, &tvItem);
}

The following example function illustrates how to retrieve an item's check
state.

BOOL TreeView_GetCheckState(HWND hwndTreeView, HTREEITEM hItem)
{
TVITEM tvItem;

// Prepare to receive the desired information.
tvItem.mask = TVIF_HANDLE | TVIF_STATE;
tvItem.hItem = hItem;
tvItem.stateMask = TVIS_STATEIMAGEMASK;

// Request the information.
TreeView_GetItem(hwndTreeView, &tvItem);

// Return zero if it's not checked, or nonzero otherwise.
return ((BOOL)(tvItem.state >> 12) -1);
}

These samples are from Platform SDK help, you can use CTreeCtrl::Get/SetItem
or CTreeCtrl::Get/SetItemState functions.
> I want the check boxes to work in such a way that checking a parent item
also causes all the child items

You should set item states for all child items recursively...

> I also want the program to be notified

Maybe handling TVN_SELCHANGED would do it... You can also examine what
happend when user mouseclicks via HitTest method.

There are some articles at CodeGuru (www.codeguru.com/treeview) explaining
and showing several usefull techniques...

HTH,

ChainsaW
Stanislav Simicek at 2007-11-11 20:41:41 >
# 2 Re: Check boxes in tree views
It appears that what I want to do is intercept the BN_CLICKED message sent
to the tree control parent window. But I am having a hard time understanding
the documentation for that message. Unlike dialog boxes, the check box in
the tree view doesn't have an identifier that I define. I need to know how
to determine which item in the tree control has been clicked. Once I do that,
I think I can process the message with an ON_COMMAND(BN_CLICKED, MyFunction)
message map.

The documentation for BN_CLICKED lists two parameters associated with that
message. How do I read them?

Thanks,
Cliff Lewis
Cliff Lewis at 2007-11-11 20:42:48 >