Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. TreeView_GetParent

TreeView_GetParent

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structuresquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NullStream
    wrote on last edited by
    #1

    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

    M 1 Reply Last reply
    0
    • N NullStream

      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

      M Offline
      M Offline
      Mike Upton
      wrote on last edited by
      #2

      HTREEITEM 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 that HTREEITEM is a handle that can be used to fill a TVITEM structure via GetItem). HTREEITEM is not a pointer to a TVITEM. While iterating through a tree (using GetParent, GetChild, GetPrevSibling, GetNextSibling etc.), you should use HTREEITEMs. In fact, most of the TreeView messages use an HTREEITEM to identify which item you're talking about. If you need to obtain more information about the item the HTREEITEM is referring to, use TreeView_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)

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups