Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / Win32

MultiSelection Tree Control

2.87/5 (7 votes)
27 Oct 2008CPOL1 min read 1   1.4K  
This tree control allows the user to select multiple tree items and enable selection through rubber banding.

Introduction

This tree control is a multl-select tree control. This allows the user to select items via rubber band selection. Even though there are a lot of multi-select tree controls published and floating in the web, I tried to make one that was simple to use.

Using the code

The user can simply use it like the CTreeCtrl. The CTreeCtrl can be replaced with the CCustomTreeCtrl. This allows some methods to change the style of the tree control like AddLineStyle(), AddLineAtRoot(), etc. It has a callback facility which calls the function when looping through the items. To loop through the items, a special function is provided, namely IterateItems(). This function takes the callback function as one of the parameters. The user can specify the start and end item for looping.

C++
/****************************************************************************
/DESCRIPTION:
/-----------
/        To iterate all the items and call the callback function for each item
/
/PARAM
/------
/        func[in]    -    Call back function to be called on each item
/        hStart[in]    -    Start node for the iteration
/        hEnd[in]    -    End node to stop the iteration
/        pInfo[in]    -    User parameter
/
/RESULT:
/-------
/        void
*/
void CCustomTreeCtrl::IterateItems( ScanCallBackFunc func,
                            HTREEITEM hStart /*= NULL*/,
                            HTREEITEM hEnd, /*= NULL*/
                            void* pInfo /*=NULL*/
                            )
{
    //If there is no start then take the root item
    if( !hStart )
        hStart = GetRootItem();

    ScanItems( func,hStart,hEnd,pInfo );
}
/***************************************************************************
/DESCRIPTION:
/-----------
/        To iterate all the items and call the callback function for each item
/
/PARAM
/------
/        func[in]    -    Call back function to be called on each item
/        hStart[in]    -    Start node for the iteration
/        hEnd[in]    -    End node to stop the iteration
/        pInfo[in]    -    User parameter
/
/RESULT:
/-------
/        void
*/
void CCustomTreeCtrl::ScanItems( ScanCallBackFunc func,
                            HTREEITEM hStart /*= NULL*/,
                            HTREEITEM hEnd, /*= NULL*/
                            void* pInfo /*= NULL*/
                            )
{
    
    //Loop from start till the end is reached
    while( hStart )
    {
        LOOPINFO lInfo;
        lInfo.pTree =  this;
        lInfo.hItem = hStart;
        lInfo.pParent = GetParent();
        lInfo.param   = pInfo;

        CRect rc;
        GetItemRect( hStart,&rc,FALSE );

        //Callback is called for the current item
        func( &lInfo );

        //Check whether we have reached iterating upto the end node
        if( hStart == hEnd )
            return;

        //Get the childern of the current item and call recursively
        HTREEITEM hChild = NULL;
        if( ItemHasChildren( hStart ) )
        {
            hChild = GetChildItem( hStart );
            ScanItems( func,hChild,hEnd,pInfo);
        }

        hStart = GetNextSiblingItem( hStart );
    }
}

Points of Interest

I have learnt a lot of new things while customizing the tree control, like how to create the drag image if there is multiple selection. I have taken some sample code from CodeProject articles to accomplish this. But I think I have made the multiple selection quite easy and I have provided lots of comments which will be very helpful to understand the code.

History

This is the first version of the code. Based on the user feedback and bug reports, I'll update the code and try to deliver the best. Please feel free to send your feedback.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)