Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ MFC
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 22-Apr-11 2:15am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
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.
  Permalink  
Comments
sukhraj Judge at 23-Apr-11 3:07am
   
Hi, thanks for solution, but can you give me any sample project?
mbue at 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 at 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 at 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.
santosh dhanawade at 4-May-11 5:42am
   
My five++ for answer in code
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  

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

  Print Answers RSS
0 OriginalGriff 390
1 Sergey Alexandrovich Kryukov 329
2 Afzaal Ahmad Zeeshan 244
3 BillWoodruff 210
4 Maciej Los 192
0 OriginalGriff 5,560
1 DamithSL 4,476
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 4 May 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100