DirTree.cpp
上传用户:wpp2016
上传日期:2010-02-01
资源大小:1250k
文件大小:9k
源码类别:

Telnet服务器

开发平台:

Visual C++

  1. // DirTreeCtrl.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "shlobj.h"
  5. #include "DirTree.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CDirTree
  13. //
  14. // By Nicola Delfino 1999 delfinon@altavista.net
  15. //
  16. // 
  17. // THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. 
  18. // THE AUTHOR DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, 
  19. // INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
  20. // PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 
  21. // ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, 
  22. // CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, 
  23. // 
  24. ////////////////////////////////////////////////////////////////////////////
  25. CDirTree::CDirTree()
  26. {
  27. }
  28. CDirTree::~CDirTree()
  29. {
  30. }
  31. BEGIN_MESSAGE_MAP(CDirTree, CTreeCtrl)
  32. //{{AFX_MSG_MAP(CDirTree)
  33. ON_NOTIFY_REFLECT(TVN_ITEMEXPANDED, OnItemexpanded)
  34. ON_WM_CREATE()
  35. //}}AFX_MSG_MAP
  36. END_MESSAGE_MAP()
  37. /////////////////////////////////////////////////////////////////////////////
  38. // CDirTree message handlers
  39. void CDirTree::Initialize(bool clear)
  40. {
  41. LPITEMIDLIST itemlist;
  42. char buf[300];
  43. int desktopIcon;
  44. int mycomputerIcon;
  45.     int ret;
  46.     if (clear)
  47.     DeleteAllItems();
  48. SetImageList (NULL,0);
  49. SHGetSpecialFolderLocation (this->m_hWnd, CSIDL_DESKTOP, &itemlist);
  50. SHGetPathFromIDList(itemlist, buf ); 
  51. m_desktop_path=buf;
  52. m_img.Create( GetSystemMetrics(SM_CXSMICON), 
  53.           GetSystemMetrics(SM_CYSMICON), 
  54.   ILC_COLOR24, 50, 50);
  55. m_img.SetBkColor( GetSysColor(COLOR_WINDOW) );
  56.     CBitmap bmp;
  57.     CBitmap *old;
  58.     CBitmap *newbmp;
  59.     CDC dc;
  60.     dc.CreateCompatibleDC(NULL);
  61.     bmp.CreateBitmap (32,32,1,24,NULL);
  62.     old=dc.SelectObject (&bmp);
  63.     dc.FillSolidRect(0,0,31,31,GetSysColor(COLOR_WINDOW));
  64.     newbmp=dc.SelectObject (old);
  65.     dc.DeleteDC ();
  66.     ret=m_img.Add(newbmp,COLORREF(0x00));
  67. desktopIcon=    m_img.Add(ExtractIcon( AfxGetApp()->m_hInstance, "shell32.dll", 34));
  68. mycomputerIcon= m_img.Add(ExtractIcon( AfxGetApp()->m_hInstance, "shell32.dll", 15));
  69. m_drives_root=AddItem_Core("Drives", TVI_ROOT, true, mycomputerIcon, mycomputerIcon);
  70. // m_desktop_root=AddItem(m_desktop_path, TVI_ROOT, true, desktopIcon, desktopIcon);
  71. }
  72. HTREEITEM CDirTree::AddItem(const char * path, HTREEITEM parent, bool isDir, int iconEx, int iconEx2)
  73. {
  74.     SHFILEINFO shinfo, shinfo_sel;
  75. SHGetFileInfo( path, NULL, 
  76.    &shinfo, 
  77.    sizeof(shinfo), 
  78.    SHGFI_DISPLAYNAME | SHGFI_ICON | SHGFI_SMALLICON);
  79. SHGetFileInfo( path, NULL, 
  80.    &shinfo_sel, 
  81.    sizeof(shinfo_sel), 
  82.    SHGFI_DISPLAYNAME |  SHGFI_ICON | SHGFI_OPENICON | SHGFI_SMALLICON);
  83. int icon    = iconEx!=-1 ? iconEx : m_img.Add(shinfo.hIcon);
  84.     int iconsel = iconEx2!=-1 ? iconEx2 : m_img.Add(shinfo_sel.hIcon);
  85. SetImageList (&m_img,LVSIL_NORMAL);
  86. return AddItem_Core ( shinfo.szDisplayName, parent, isDir, icon, iconsel);
  87. }
  88. void CDirTree::OnItemexpanded(NMHDR* pNMHDR, LRESULT* pResult) 
  89. {
  90. NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
  91. TV_ITEM itm= pNMTreeView->itemNew;
  92. if (itm.state & TVIS_EXPANDED)
  93. {
  94. SetRedraw(FALSE);
  95. ExpandBranch(itm.hItem);
  96. SetRedraw(TRUE);
  97. }
  98. else
  99. {
  100. SetRedraw(FALSE);
  101. DeleteAllChild(itm.hItem);
  102. SetRedraw(TRUE);
  103. }
  104. *pResult = 0;
  105. }
  106. void CDirTree::DeleteAllChild(HTREEITEM itm, bool isEmpty)
  107. {
  108. HTREEITEM child;
  109. child= GetChildItem(itm);
  110. while (child)
  111. {
  112. int img1, img2;
  113.         if (GetChildItem(child))
  114.             DeleteAllChild(GetChildItem(child), isEmpty);
  115.         GetItemImage(child,img1,img2);
  116.         if (img2!=0 && img2!=img1) 
  117.     {
  118.             m_img.Remove(img2);
  119.             
  120.             RefreshTreeImages(m_drives_root, img2);
  121.             RefreshTreeImages(m_desktop_root, img2);
  122.             }
  123. if (img1!= 0)
  124.             {
  125.             m_img.Remove(img1);
  126.             RefreshTreeImages(m_drives_root, img1);
  127.             RefreshTreeImages(m_desktop_root, img1);
  128.             }
  129.         DeleteItem (child);
  130.         SetImageList(&m_img,TVSIL_NORMAL);
  131. child= GetChildItem(itm);
  132. }
  133. if (isEmpty)
  134. InsertItem( "",itm);; 
  135. }
  136. void CDirTree::ExpandBranch(HTREEITEM parent)
  137. {
  138.     bool bOne=false;
  139. if(parent==m_drives_root)
  140. {
  141. char *ptr;
  142.      DeleteAllChild(parent,false);
  143. GetLogicalDriveStrings(500,m_drives_name);
  144. ptr=m_drives_name;
  145. while (*ptr)
  146. {
  147.             bOne=true;
  148. AddItem(ptr, m_drives_root, true);
  149. ptr+=strlen(ptr)+1;
  150. }
  151. }
  152. else
  153. {
  154.         CString fullpath,pathWildcard;
  155.         fullpath=GetPathFromHere(fullpath, parent);
  156.         DeleteAllChild(parent,false);
  157.         pathWildcard=fullpath+"*.*";
  158.         CFileFind finder;
  159.         BOOL bWorking = finder.FindFile(pathWildcard);
  160.         while (bWorking)
  161.             {
  162.             bWorking = finder.FindNextFile();
  163.             if ( finder.IsDirectory() && !finder.IsDots() )
  164.                 {
  165.                 bOne=true;
  166.                 HTREEITEM itm=AddItem (fullpath+finder.GetFileName(), parent );
  167.                 InsertItem("",itm);
  168.                 }
  169.             }
  170.         
  171.         bWorking = finder.FindFile(pathWildcard);
  172.         while (bWorking)
  173.             {
  174.             bWorking = finder.FindNextFile();
  175.             if ( !finder.IsDirectory() && MatchExtension(finder.GetFileName()) )
  176.                 {
  177.                 bOne=true;
  178.                 AddItem (fullpath+finder.GetFileName(), parent );
  179.                 }
  180.             }
  181. }
  182.     
  183.     if (!bOne) InsertItem( "",parent);
  184. }
  185. HTREEITEM CDirTree::AddItem_Core(const char * name, HTREEITEM parent, bool isDir, int icon,int iconOpen)
  186. {
  187. HTREEITEM hitm;
  188. hitm=InsertItem( name, icon, iconOpen, parent);
  189. if (isDir) InsertItem( "",hitm);
  190. return hitm;
  191. }
  192. CString CDirTree::GetPathFromHere(CString s, HTREEITEM itm)
  193. {
  194.     HTREEITEM parent=GetParentItem(itm);
  195.     HTREEITEM itmChild=GetChildItem(itm);
  196.     if (itm==m_drives_root) return "";
  197.         
  198. if (parent==m_drives_root)
  199. {
  200. HTREEITEM child;
  201. char *ptr=m_drives_name;
  202. child=GetChildItem(parent);
  203. while (child)
  204. {
  205. if (child==itm)
  206. {
  207. s=CString(ptr)+s;
  208. return s;
  209. }
  210. ptr+=strlen(ptr)+1;
  211. child=GetNextItem(child, TVGN_NEXT);
  212. }
  213. }
  214. else if (itm==m_desktop_root)
  215. {
  216. s=m_desktop_path+'\'+s;
  217. return s;
  218. }
  219.     else
  220.     {
  221.         if (GetItemText(itm)!= CString(""))
  222.             s=GetItemText(itm)+'\'+s;
  223.         s=GetPathFromHere(s,parent);
  224.     }
  225.     // remove the last '' if it isn't a directory
  226.     if (!itmChild && s.GetLength()>0)
  227.         s=s.Left (s.GetLength()-1);
  228. return s;
  229. }
  230. /* if image number is more than img number -- */
  231. void CDirTree::RefreshTreeImages(HTREEITEM item,int img)
  232. {
  233.     int iStandard, iOpen;
  234.     HTREEITEM itmCurrent;
  235.        
  236.     itmCurrent=GetChildItem(item);
  237.     
  238.     while (itmCurrent)
  239.         {
  240.         if (GetItemImage(itmCurrent,iStandard,iOpen))
  241.             {
  242.             if (iStandard>img) iStandard--;
  243.             if (iOpen>img) iOpen--;
  244.             SetItemImage(itmCurrent,iStandard, iOpen);
  245.             }
  246.         if (ItemHasChildren( itmCurrent)!=0)
  247.             {
  248.             RefreshTreeImages(itmCurrent, img);
  249.             }
  250.         itmCurrent=GetNextSiblingItem(itmCurrent);
  251.         }
  252. }
  253. bool CDirTree::MatchExtension(CString file)
  254. {
  255.     //return file.Right(4)==CString(".exe");
  256. return TRUE;
  257. }
  258. CString CDirTree::GetFullPath(HTREEITEM item)
  259. {
  260.     if (GetItemText(item)==CString("")) return "";
  261.     else return GetPathFromHere("",item);
  262. }
  263. bool CDirTree::SetPath(CString path_in)
  264. {
  265.     char path[MAX_PATH];
  266.     char *delimiters="\";
  267.     char *token;
  268.     char *pathStart=path;
  269.     HTREEITEM current=m_drives_root;
  270.     strcpy(path,path_in);
  271.     Expand(current,TVE_EXPAND );
  272.     token=strtok (path,delimiters);
  273.     while (token!=NULL)
  274.         {
  275.         current=Dir2Item(current, token);
  276.         if (!current)
  277.             {
  278.             return false;
  279.             }
  280.         
  281.         Expand(current,TVE_EXPAND );
  282.         token=strtok(NULL,delimiters);
  283.         }
  284.     SelectItem(current);
  285.     return true;
  286. }
  287. HTREEITEM CDirTree::Dir2Item(HTREEITEM current, char *dir)
  288. {
  289.     if (current==m_drives_root)
  290.         {
  291.         HTREEITEM child;
  292.         CString szDir(dir);
  293.         szDir+="\";
  294.         szDir.MakeUpper();
  295. char *ptr=m_drives_name;
  296. child=GetChildItem(current);
  297. while (child)
  298.     {
  299. if (szDir==CString(ptr))
  300.      {
  301. return child;
  302.     }
  303. ptr+=strlen(ptr)+1;
  304. child=GetNextItem(child, TVGN_NEXT);
  305.     }
  306.         }
  307.     else
  308.         {
  309.         HTREEITEM child;
  310.         CString szChildText;
  311.         CString szDir(dir);
  312.         szDir.MakeUpper();
  313. child=GetChildItem(current);
  314. while (child)
  315.     {
  316.             szChildText=GetItemText(child);
  317.             szChildText.MakeUpper();
  318. if ( szChildText==szDir)
  319.      {
  320. return child;
  321.     }
  322. child=GetNextItem(child, TVGN_NEXT);
  323.     }
  324.         }
  325.     return NULL;
  326. }
  327. int CDirTree::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  328. {
  329. if (CTreeCtrl::OnCreate(lpCreateStruct) == -1)
  330. return -1;
  331. Initialize();
  332. return 0;
  333. }