Click here to Skip to main content
15,867,957 members
Articles / Desktop Programming / Win32

MultiSelection Tree Control

Rate me:
Please Sign up or sign in to vote.
2.87/5 (7 votes)
27 Oct 2008CPOL1 min read 38.2K   1.4K   11   5
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)


Written By
Software Developer (Senior)
India India
I'm working as Senior software Engineer since 7 years and interested in MFC and COM programming.

Comments and Discussions

 
QuestionDoes not work for me... Pin
dliviu16-Apr-14 1:01
dliviu16-Apr-14 1:01 
AnswerRe: Does not work for me... Pin
Babu_Abdulsalam4-Jun-14 21:34
Babu_Abdulsalam4-Jun-14 21:34 
GeneralRe: Does not work for me... Pin
dliviu5-Jun-14 0:33
dliviu5-Jun-14 0:33 
QuestionMore stable version Pin
Babu_Abdulsalam3-Feb-13 16:12
Babu_Abdulsalam3-Feb-13 16:12 
AnswerRe: More stable version Pin
dliviu30-Apr-14 1:41
dliviu30-Apr-14 1:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.