Jari666
Jari666

Reputation: 73

Moving TreeView items in Windows

I've been working on a TreeView system using the WinAPI. I built wrappers around some functions which allow me to insert new items, remove items and move items in a control. However, moving items seems to be a real pain in the ass. I could not find any function in the MSDN (http://msdn.microsoft.com/en-us/library/aa925848.aspx) that could solve my problem.


Therefore, I had to create a dirty-hack which saves the attributes of an item, deletes the old item and inserts the item at the new location. Here's a little code snippet:

TVITEM tvitem;
char chrTextBuffer[33];

tvitem.mask = TVIF_TEXT;
tvitem.cchTextMax = sizeof(chrTextBuffer); // \0 termination included?
tvitem.pszText = chrTextBuffer;
tvitem.hItem = olditem; // different in the original code, don't want to post the entire function
TreeView_GetItem(GUIVars.Controls.GUI_Chat.ChannelTree, &tvitem);

The TVITEM struct is used to retrieve information about the item. I won't explain these structs in detail, because it's not important right now. Well, this information is now being inserted into a TV_INSERTSTRUCT and sent to the TreeView via

TreeView_Insert(mytreeview, &insertstruct);

The old item gets deleted:

TreeView_DeleteItem(mytreeview, olditem);

Obviously, this is quite dirty. Is there any other possibility to achieve the desired functionality? I am only using pure WinAPI in Pelles C for Windows (http://www.smorgasbordet.com/pellesc/), so libraries are no option.


Furthermore, I had to think about a way which allows to move all the children and the children's children too. Each item is saved in a structure (in the HTREEITEM Handle)

struct cTreeItem
{
    uint8 Type;
    uint8 Used;
    uint16 Depth;
    int32 ParentID;
    HTREEITEM Handle;
};

which provides information, especially the ParentID. I could just loop through all my items (which are all saved in a dynamically allocated growing array) and check if their ParentID matches one of the nodes that has to be moved until I reach the last level of item-depth, but a native alternative would be better of course.

I hope my questions are understandable, I need a way to move items in a TreeView, preferably also moving their children and children's children (...).


Edit: Btw, I found a lot of stuff solving this problem, but unfortunately only in C#, VB etc..

Upvotes: 3

Views: 893

Answers (1)

Raymond Chen
Raymond Chen

Reputation: 45172

The TreeView common control does not support moving items to a new parent. If you want to move an item relative to its siblings (but remain under the same parent), you can use TreeView_SortChildrenCB and pass a custom sort function that orders the children the way you want.

Upvotes: 4

Related Questions