TreeView_GetParent
-
I feel like such a choad. Out of all the Win32 Common Controls the TreeView control has given me the most grief and the documentation is not very helpful. In this instance (of a long series of them) I'm having a problem with the TreeView_GetParent function. Basically I'm trying to follow a leaf in the tree back to it's root. And do so via the following function:
void ConstructPath(TVITEM *item,char *buffer) { // Follow parentage. HTREEITEM back = TreeView_GetParent(Tree,item->hItem); if(back == NULL) { MessageBox(MainWindow,"doesn't have parent","recursed",0); } else { TVITEM *node = (TVITEM *)back; // problem must be here. char name[_MAX_FNAME]; memset(&name,0,_MAX_FNAME); node->mask |= TVIF_TEXT | TVIF_CHILDREN ; node->pszText = (char *)&name; node->cchTextMax = _MAX_FNAME; TreeView_GetItem(Tree,node); MessageBox(MainWindow,name,"recursed",0); ConstructPath((TVITEM*)back,buffer); // Recurse with parent node. } }
When run this function screws up the display of the control and returns the absolutely wrong nodes in the tree. Now I'm going to hazzard an educated guess that "TVITEM *node = (TVITEM *)back;" is the problem as back is an HTREEITEM that I'm casting to a TVITEM (which TreeView_GetParent requires yet returns a HTREEITEM. I cannot find the definition of HTREEITEM in the help system and hoping that GetParent was written with recursion in mind the two types might not have been far off (I know bad guess X| ). Can anyone tell me what the relationshipo is between TVITEM and HTREEITEM and how I can convert one into the other (or at least point me to some clear documentation)? :confused: Sean -
I feel like such a choad. Out of all the Win32 Common Controls the TreeView control has given me the most grief and the documentation is not very helpful. In this instance (of a long series of them) I'm having a problem with the TreeView_GetParent function. Basically I'm trying to follow a leaf in the tree back to it's root. And do so via the following function:
void ConstructPath(TVITEM *item,char *buffer) { // Follow parentage. HTREEITEM back = TreeView_GetParent(Tree,item->hItem); if(back == NULL) { MessageBox(MainWindow,"doesn't have parent","recursed",0); } else { TVITEM *node = (TVITEM *)back; // problem must be here. char name[_MAX_FNAME]; memset(&name,0,_MAX_FNAME); node->mask |= TVIF_TEXT | TVIF_CHILDREN ; node->pszText = (char *)&name; node->cchTextMax = _MAX_FNAME; TreeView_GetItem(Tree,node); MessageBox(MainWindow,name,"recursed",0); ConstructPath((TVITEM*)back,buffer); // Recurse with parent node. } }
When run this function screws up the display of the control and returns the absolutely wrong nodes in the tree. Now I'm going to hazzard an educated guess that "TVITEM *node = (TVITEM *)back;" is the problem as back is an HTREEITEM that I'm casting to a TVITEM (which TreeView_GetParent requires yet returns a HTREEITEM. I cannot find the definition of HTREEITEM in the help system and hoping that GetParent was written with recursion in mind the two types might not have been far off (I know bad guess X| ). Can anyone tell me what the relationshipo is between TVITEM and HTREEITEM and how I can convert one into the other (or at least point me to some clear documentation)? :confused: SeanHTREEITEM
is a handle to an item in the tree (similar to the simpler index value that can be used with list controls).TVITEM
is a structure that can be filled with information about an item in the tree. You can't cast between one and the other, because they are not related (other than thatHTREEITEM
is a handle that can be used to fill aTVITEM
structure viaGetItem
).HTREEITEM
is not a pointer to aTVITEM
. While iterating through a tree (using GetParent, GetChild, GetPrevSibling, GetNextSibling etc.), you should useHTREEITEM
s. In fact, most of the TreeView messages use anHTREEITEM
to identify which item you're talking about. If you need to obtain more information about the item theHTREEITEM
is referring to, useTreeView_GetItem
. If hTree is the handle to the tree control you're working with, and hItem is the item you want information about, the following code could be used://buffer for the item text
char name[_MAX_FNAME];
//TVITEM structure to fill
TVITEM itemInfo={0};
//HTREEITEM of the item you want info about
itemInfo.hItem = hItem;
//mask that specifies the information you want to obtain
itemInfo.mask = TVIF_TEXT | TVIF_CHILDREN
//If you've specified TVIF_TEXT, you must make sure the character buffer
//is set up correctly.
itemInfo.pszText=name;
itemInfo.cchTextMax=_MAX_FNAME;
//Now call GetItem to fill the structure with the requested information
TreeView_GetItem(hTree, &itemInfo);After that code, the
name
array will contain the text of the item (up to _MAX_FNAME-1 characters), and itemInfo.cChildren will be 0, 1, or I_CHILDREN_CALLBACK.
"We are the knights who say Ni" (The Knights Who Say Ni)