Click here to Skip to main content
15,882,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am using cshelltree control with check boxes. But I am unable to find which nodes check boxes are selected. I want to get path of nodes which are selected.
Please help me as soon as possible.

Thanks and Regards,
Sukhraj Singh
Posted

Little code snippet for you:
TreeWalk.h
unsigned int scopy(TCHAR* dst,const unsigned int cbdst,const unsigned int pos,const TCHAR* src);

class vWalkTree
{
public: // vWalkTree
  virtual int  Enter(TVITEM& tvi) = 0;
  virtual int  Leave(TVITEM& tvi) = 0;
  virtual int  BeginWalk(TVITEM& tvi) = 0;
  virtual int  EndWalk(TVITEM& tvi) = 0;

};

void WalkTree(vWalkTree& walk,HWND htree,HTREEITEM hroot=0);

TreeWalk.cpp
#include "stdafx.h"
#include "TreeWalk.h"

unsigned int scopy(TCHAR* dst,const unsigned int cbdst,const unsigned int pos,const TCHAR* src)
{
  unsigned int  ix,len = _tcslen(src);
  if((pos+len)<cbdst)
  {
    for(ix=0;ix<len;ix++) dst[pos+ix]=src[ix]; dst[pos+ix]=0;
    return pos+len;
  }
  return 0;
}


static void __WalkTree(vWalkTree& walk,HWND htree,TVITEM& tvi)
{
  if(TreeView_GetItem(htree,&tvi))
  {
    if(walk.Enter(tvi))
    {
      TVITEM      tvic = tvi;
      for
      (
        tvic.hItem=TreeView_GetChild(htree,tvi.hItem);
        tvic.hItem;
        tvic.hItem=TreeView_GetNextSibling(htree,tvic.hItem)
      )
      {
        __WalkTree(walk,htree,tvic);
      }
      walk.Leave(tvi);
    }
  }
}

void WalkTree(vWalkTree& walk,HWND htree,HTREEITEM hroot)
{
  if(IsWindow(htree))
  {
    TVITEM  tvi; memset(&tvi,0,sizeof(tvi));
    if(walk.BeginWalk(tvi))
    {
      if(hroot)
      {
        tvi.hItem = hroot;
        __WalkTree(walk,htree,tvi);
      }
      else
      {
        for
        (
          tvi.hItem=TreeView_GetRoot(htree);
          tvi.hItem;
          tvi.hItem=TreeView_GetNextSibling(htree,tvi.hItem)
        )
        {
          __WalkTree(walk,htree,tvi);
        }
      }
      walk.EndWalk(tvi);
    }
  }
}

In your code:
C#
class tree : public vWalkTree
  {
    static int  __ischecked(TVITEM& tvi)
    {
      return (tvi.state & TVIS_STATEIMAGEMASK)==INDEXTOSTATEIMAGEMASK(2);
    }
    static int  __isselected(TVITEM& tvi)
    {
      return TVIS_SELECTED & tvi.state;
    }
  public: // vWalkTree
    virtual int  Enter(TVITEM& tvi)
    {
      unsigned int  len;
      if(__ischecked(tvi))
      {
        TRACE(__TEXT("%s\r\n"),_path);
      }
      len = scopy(tvi.pszText,tvi.cchTextMax,_tcslen(tvi.pszText),__TEXT("\\"));
      push(top()+len);
      tvi.pszText    = (TCHAR*)_path + top();
      tvi.cchTextMax = (sizeof(_path)/sizeof(_path[0])) - top();
      return 1;
    }
    virtual int  Leave(TVITEM& tvi)
    {
      pop();
      tvi.pszText    = (TCHAR*)_path + top();
      tvi.cchTextMax = (sizeof(_path)/sizeof(_path[0])) - top();
      tvi.pszText[0] = 0;
      return 1;
    }
    virtual int  BeginWalk(TVITEM& tvi)
    {
      tvi.mask       = TVIF_STATE|TVIF_TEXT;
      tvi.state      = 0;
      tvi.stateMask  = TVIS_SELECTED|TVIS_STATEIMAGEMASK;
      tvi.pszText    = _path + top();
      tvi.cchTextMax = (sizeof(_path)/sizeof(_path[0])) - top();
      return 1;
    }
    virtual int  EndWalk(TVITEM& tvi)
    {
      return 1;
    }
    tree(const TCHAR* root)
    {
      _istack = 0;
      push(scopy(_path,sizeof(_path)/sizeof(_path[0]),0,root));
    }
  protected:
    unsigned int    push(unsigned int h){ return _istack<(sizeof(_astack)/sizeof(_astack[0]))?_astack[_istack++]=h:0; }
    unsigned int    pop(){ return 0<_istack?_astack[--_istack]:0; }
    unsigned int    top(){ return 0<_istack?_astack[_istack-1]:0; }
  private:
    unsigned int    _astack[256];
    unsigned int    _istack;
    TCHAR           _path[_MAX_PATH+_MAX_FNAME*0x400];
  };
void TraceTree()
{
  tree  t(__TEXT("E:\\"));
  WalkTree(t,_tree);
}

Good luck.
 
Share this answer
 
Comments
sukhraj Judge 23-Apr-11 3:07am    
Hi, thanks for solution, but can you give me any sample project?
mbue 23-Apr-11 7:13am    
Sorry, i havent the mfcplus with cshelltree. this code works with CTreeCtrl this is a base class of cshelltree, so far as i know.
Regards
sukhraj Judge 4-May-11 5:35am    
Hi,
Can I set specific folder as a root of shell tree. I want to set folder like tree start with folder abc.
mbue 4-May-11 8:55am    
You can give the root path as a parameter for the class constructor. to start at the corresponding tree node (HTREEITEM), you have to find it in the control and give it as paramter for the walk function. so you can walk from every starting point in your tree.
Regards.
[no name] 4-May-11 5:42am    
My five++ for answer in code
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900