Click here to Skip to main content
14,386,278 members

Tree Control Offering Drag and Drop, Folder Auto-expansion, New/edit/delete, and Button Move

Rate this:
4.57 (26 votes)
Please Sign up or sign in to vote.
4.57 (26 votes)
29 Mar 2010CPOL
A basic tree control that ties together all the functionality to make items fully editable and moveable.


This code ties together a number of things I really wanted my fully draggable/moveable tree control to be able to do, including adding new folders, renaming any items, and deleting any items. The particular things that this control allows are:

  • Full drag and drop functionality, including a drag image and indicator to show where the item will be dropped, i.e., to help differentiate between dropping an item in a folder or after the folder
  • Move Up/Down buttons for simple item navigation, including into and out of folders
  • New folders, renaming, and deletion, including deletion of entire folders


This code is based initially on Frederic My's freeware tree control at Thanks Frederic.

Using the Code

The extended tree control (CTreeCtrlDrag) is contained within the source files TreeCtrlDrag.cpp and .h.

To use the control, simply replace the CTreeCtrl declaration in your dialog with CTreeCtrlDrag. As it's derived from CTreeCtrl, it can be initialised as per normal. The dialog then needs to pass any user input down to the tree control as per the following code, which would be in the dialog class...

// Move tree item one place up
void CDragDropView::OnBtnUp()

// Move tree item one place down
void CDragDropView::OnBtnDown()

// Tree selection has changed, so check status of Up and Down buttons
void CDragDropView::OnTreeSelChanged(NMHDR* pNMHDR, LRESULT* pResult)

void CDragDropView::OnBtnNewFolder()

void CDragDropView::OnBtnRename()

void CDragDropView::OnBtnDelete()

In the application where I use this for real, I link each tree item to a data structure, so I need to know when items are deleted or moved. When a tree item is moved, it is in fact deleted and recreated in the target location. So I need to know what the old and new tree items are. This is done automatically within the CTreeCtrlDrag control, and takes the form of user defined messages posted back to the parent window (dialog). They would be handled in the calling dialog like this...

BEGIN_MESSAGE_MAP(CDlgQuickGraphOrganise, CDialog)


// Tree Callback Message Handlers
// A tree item has been moved successfully,
// so swap references to the old tree item with the new
// one. In other words, a tree item has been deleted
// and recreated somewhere else.
void CDlgQuickGraphOrganise::OnTreeMoveComplete(WPARAM wParam,
                                                LPARAM lParam)
    // Sanity check the move parameters
    ASSERT((hNew != NULL) && (hOld != NULL));

// A tree item has been deleted successfully,
// so add it to the Deleted Items list
void CDlgQuickGraphOrganise::OnItemDelete(WPARAM wParam,
                                          LPARAM lParam)

// If the user is trying to edit the root item,
// make a beep sound and cancel the edit by setting
// the pResult value to 1.
void CDlgQuickGraphSummary::OnBeginLabelEdit(NMHDR *pNMHDR,
                                             LRESULT *pResult)
    HTREEITEM hItem = pTVDispInfo->item.hItem;
    if (hItem == m_Tree.GetRootItem())
        // We don't allow the user to edit the root folder.
        *pResult = 1;

// We've successfully changed an item name - we may want to
// post-process the changed item.
void CDlgQuickGraphOrganise::OnItemRenamed(WPARAM wParam, LPARAM lParam)
    HTREEITEM hItem = (HTREEITEM)wParam;

In this instance, the user defined messages (e.g., UWM_TREEBEGINEDIT) are set in StdAfx.h.

Other points to note:

  • CTreeCtrlDrag needs to be able to ascertain whether a particular tree item in a folder could be empty, so it assumes the image used for folders is always the same, i.e., using the FOLDER_IMAGE define. If you're using an image list and the index of the folder image varies from use to use, you'll have to do something a bit more clever here.

Points of Interest

While I've been coding for 4 or so years, I know I've still heaps to learn. Please, if you see any ways to improve this control or my style in general, all constructive feedback would be hugely appreciated. Note - this is only with reference to TreeCtrlDrag.cpp and .h, which are the files I've specifically worked on.


  • 21 Jan 2010 - Initial release
  • 26 Jan 2010 - Fixed a memory leak in TidyUpEndOfDrag() - thanks Tom
  • 26 Jan 2010 - Added a check for duplicate items when moving/renaming. When setting up the tree, call m_Tree.SetNoDuplicates(TRUE), to turn on checking for duplicate items in the current tree folder. See the additional code wherever a move occurs and after renaming (in OnEndLabelEdit()).
  • 17 Feb 2010 - Fixed a bug in return messages for movement of folder using the up and down buttons where the message was only being returned for the parent item, not any sub-items. Added a static to display the messages being received by the parent dialog. Added double click response to calling dialog to process a double click on a non-tree item. Added callback for right mouse click to allow calling dialog to context menu options.
  • 29 Mar 2010 - Fixed a bug in the control


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


About the Author

Phil Outram
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

GeneralBottom item Pin
redgreenloko25-Aug-11 9:08
Memberredgreenloko25-Aug-11 9:08 
BugBug Pin
redgreenloko24-Aug-11 6:46
Memberredgreenloko24-Aug-11 6:46 
GeneralRe: Bug Pin
Phil Outram24-Aug-11 23:15
MemberPhil Outram24-Aug-11 23:15 
QuestionDialog Class Pin
Member 789338710-Aug-11 8:55
MemberMember 789338710-Aug-11 8:55 
AnswerRe: Dialog Class Pin
Phil Outram10-Aug-11 23:12
MemberPhil Outram10-Aug-11 23:12 
GeneralGreat control Pin
Vincent_RICHOMME6-Aug-10 0:44
MemberVincent_RICHOMME6-Aug-10 0:44 
GeneralMultiple Tree Drag n' Drop Pin
RakeshManohar4-Mar-10 1:18
MemberRakeshManohar4-Mar-10 1:18 
GeneralRe: Multiple Tree Drag n' Drop Pin
Phil Outram29-Mar-10 22:56
MemberPhil Outram29-Mar-10 22:56 
GeneralMemory leak Pin
tom-kaltofen26-Jan-10 6:14
Membertom-kaltofen26-Jan-10 6:14 
GeneralRe: Memory leak Pin
Phil Outram26-Jan-10 7:49
MemberPhil Outram26-Jan-10 7:49 
GeneralRe: Memory leak Pin
Victor Nijegorodov26-Jan-10 23:50
MemberVictor Nijegorodov26-Jan-10 23:50 
GeneralRe: Memory leak Pin
PoreZ6-Mar-10 8:28
MemberPoreZ6-Mar-10 8:28 
GeneralRe: Memory leak Pin
Phil Outram29-Mar-10 3:39
MemberPhil Outram29-Mar-10 3:39 
GeneralMy vote of 2 Pin
Alexandre GRANVAUD22-Jan-10 0:31
MemberAlexandre GRANVAUD22-Jan-10 0:31 
nothing to learn from it
GeneralRe: My vote of 2 PinPopular
Phil Outram26-Jan-10 8:34
MemberPhil Outram26-Jan-10 8:34 
GeneralRe: My vote of 2 PinPopular
SteveKing26-Jan-10 22:54
MemberSteveKing26-Jan-10 22:54 
GeneralRe: My vote of 2 Pin
RakeshManohar4-Mar-10 1:22
MemberRakeshManohar4-Mar-10 1:22 
GeneralRe: My vote of 2 Pin
KenJohnson29-Mar-10 13:03
MemberKenJohnson29-Mar-10 13:03 
GeneralRe: My vote of 2 Pin
Mukit, Ataul24-May-11 18:16
MemberMukit, Ataul24-May-11 18:16 

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.

Posted 21 Jan 2010


69 bookmarked