DirTreeCtrl.cpp
上传用户:shouhua
上传日期:2014-12-06
资源大小:5685k
文件大小:7k
源码类别:

杀毒

开发平台:

Visual C++

  1. // DirTreeCtrl.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "AntiEvilTools.h"
  5. #include "DirTreeCtrl.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. MyList   *Gogogo2;
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CDirTreeCtrl
  14. CDirTreeCtrl::CDirTreeCtrl()
  15. {
  16. }
  17. CDirTreeCtrl::~CDirTreeCtrl()
  18. {
  19. }
  20. BEGIN_MESSAGE_MAP(CDirTreeCtrl, CTreeCtrl)
  21. //{{AFX_MSG_MAP(CDirTreeCtrl)
  22. ON_NOTIFY_REFLECT(TVN_ITEMEXPANDED, OnItemexpanded)
  23. ON_WM_CREATE()
  24. ON_WM_RBUTTONDOWN()
  25. //}}AFX_MSG_MAP
  26. END_MESSAGE_MAP()
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CDirTreeCtrl message handlers
  29. void CDirTreeCtrl::Initialize(MyList   *list)
  30. {
  31. LPITEMIDLIST itemlist;
  32. m_list=list;
  33. char buf[256];
  34. int  iComputerIcon;
  35. DeleteAllItems();
  36. SetImageList (NULL,0);
  37. SHGetSpecialFolderLocation (this->m_hWnd, CSIDL_DESKTOP, &itemlist);
  38. SHGetPathFromIDList(itemlist, buf ); 
  39. m_DesktopPath=buf;
  40. m_ImageList.Create( GetSystemMetrics(SM_CXSMICON), 
  41.           GetSystemMetrics(SM_CYSMICON), 
  42.   ILC_COLOR24, 50, 50);
  43. m_ImageList.SetBkColor( GetSysColor(COLOR_WINDOW) );
  44. iComputerIcon = m_ImageList.Add(ExtractIcon( AfxGetApp()->m_hInstance, "shell32.dll", 15));
  45. m_DrivesRoot = InsertItems("我的电脑", TVI_ROOT, true, iComputerIcon, iComputerIcon);
  46. }
  47. HTREEITEM CDirTreeCtrl::AddItem(const char * path, HTREEITEM parent, bool isDir, int iconEx, int iconEx2)
  48. {
  49.     SHFILEINFO shinfo, shinfo_sel;
  50. SHGetFileInfo( path, NULL, 
  51.    &shinfo, 
  52.    sizeof(shinfo), 
  53.    SHGFI_DISPLAYNAME | SHGFI_ICON | SHGFI_SMALLICON);
  54. SHGetFileInfo( path, NULL, 
  55.    &shinfo_sel, 
  56.    sizeof(shinfo_sel), 
  57.    SHGFI_DISPLAYNAME |  SHGFI_ICON | SHGFI_OPENICON | SHGFI_SMALLICON);
  58. int icon    = iconEx !=-1 ? iconEx  : m_ImageList.Add(shinfo.hIcon);
  59.     int iconsel = iconEx2!=-1 ? iconEx2 : m_ImageList.Add(shinfo_sel.hIcon);
  60. SetImageList (&m_ImageList,LVSIL_NORMAL);
  61. return InsertItems ( shinfo.szDisplayName, parent, isDir, icon, iconsel);
  62. }
  63. void CDirTreeCtrl::OnItemexpanded(NMHDR* pNMHDR, LRESULT* pResult) 
  64. {
  65. NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
  66. TV_ITEM itm= pNMTreeView->itemNew;
  67. if (itm.state & TVIS_EXPANDED)
  68. ExpandBranch(itm.hItem);
  69. else
  70. {
  71. DeleteAllChild(itm.hItem);
  72. }
  73. *pResult = 0;
  74. }
  75. void CDirTreeCtrl::DeleteAllChild(HTREEITEM itm, bool isEmpty)
  76. {
  77. HTREEITEM child;
  78. child= GetChildItem(itm);
  79. while (child)
  80. {
  81.         if (GetChildItem(child))
  82.             DeleteAllChild(GetChildItem(child), isEmpty);
  83.         DeleteItem (child);
  84.         SetImageList(&m_ImageList,TVSIL_NORMAL);
  85. child= GetChildItem(itm);
  86. }
  87. if (isEmpty)
  88. InsertItem( "",itm);; 
  89. }
  90. void CDirTreeCtrl::ExpandBranch(HTREEITEM parent)
  91. {
  92.     bool bOne=false;
  93.     int index=0;
  94. if(parent==m_DrivesRoot)
  95. {
  96. char *ptr;
  97.      DeleteAllChild(parent,false);
  98. GetLogicalDriveStrings(500,m_DrivesName);
  99. ptr=m_DrivesName;
  100. while (*ptr)
  101. {
  102.             bOne=true;
  103. AddItem(ptr, m_DrivesRoot, true);
  104. ptr+=strlen(ptr)+1;
  105. }
  106. }
  107. else
  108. {
  109. m_list->DeleteAllItems();
  110.   m_strFullPath = "";
  111.         CString pathWildcard;
  112.         m_strFullPath=GetPathFromHere(m_strFullPath, parent);
  113.         DeleteAllChild(parent,false);
  114.         pathWildcard=m_strFullPath+"*.*";
  115.         CFileFind finder;
  116.         BOOL bWorking = finder.FindFile(pathWildcard);
  117.         while (bWorking)
  118.             {
  119.             bWorking = finder.FindNextFile();
  120.             if ( finder.IsDirectory() && !finder.IsDots() )
  121.                 {
  122.                 bOne=true;
  123.                 HTREEITEM itm=AddItem (m_strFullPath+finder.GetFileName(), parent );
  124.                 InsertItem("",itm);
  125.                 }
  126.             }
  127.         bWorking = finder.FindFile(pathWildcard);
  128.         while (bWorking)
  129.             {
  130.             bWorking = finder.FindNextFile();
  131.             if ( !finder.IsDirectory())
  132.                 {
  133.                 bOne=true;
  134. m_list->InsertItem(index, finder.GetFileName());
  135. m_list->SetItemText(index,1,m_strFullPath+finder.GetFileName());
  136. m_list->SetItem(index, 0, LVIF_TEXT | LVIF_IMAGE,finder.GetFileName(), 
  137. Gogogo2->GetFileIcon(m_strFullPath+finder.GetFileName()), 0, 0, 0);
  138.                 index++;
  139.                 //AddItem (m_strFullPath+finder.GetFileName(), parent );
  140.                 }
  141.             }
  142. }
  143.     if (!bOne) InsertItem( "",parent);
  144. }
  145. HTREEITEM CDirTreeCtrl::InsertItems(const char * name, HTREEITEM parent, bool isDir, int icon,int iconOpen)
  146. {
  147. HTREEITEM hitm;
  148. hitm=InsertItem( name, icon, iconOpen, parent);
  149. if (isDir) InsertItem( "",hitm);
  150. return hitm;
  151. }
  152. CString CDirTreeCtrl::GetPathFromHere(CString s, HTREEITEM itm)
  153. {
  154.     HTREEITEM parent   = GetParentItem(itm);
  155.     HTREEITEM itmChild = GetChildItem(itm);
  156. if(!itmChild) return GetPathFromHere(s,parent);
  157.     if (itm==m_DrivesRoot) return ""; 
  158. if (parent==m_DrivesRoot)
  159. {
  160. HTREEITEM child;
  161. char *ptr=m_DrivesName;
  162. child=GetChildItem(parent);
  163. while (child)
  164. {
  165. if (child==itm)
  166. {
  167. s=CString(ptr)+s;
  168. return s;
  169. }
  170. ptr+=strlen(ptr)+1;
  171. child=GetNextItem(child, TVGN_NEXT);
  172. }
  173. }
  174. else if (itm==m_DesktopRoot)
  175. {
  176. s=m_DesktopPath+'\'+s;
  177. return s;
  178. }
  179.     else
  180.     {
  181.         if (GetItemText(itm)!= CString(""))
  182.             s=GetItemText(itm)+'\'+s;
  183.         s=GetPathFromHere(s,parent);
  184.     }
  185.     if (!itmChild && s.GetLength()>0)
  186.         s=s.Left (s.GetLength()-1);
  187. return s;
  188. }
  189. CString CDirTreeCtrl::GetFullPath(HTREEITEM item)
  190. {
  191.     if (GetItemText(item)==CString("")) return "";
  192.     else return GetPathFromHere("",item);
  193. }
  194. bool CDirTreeCtrl::SetFolder(CString path_in)
  195. {
  196.     char path[MAX_PATH];
  197.     char *delimiters="\";
  198.     char *token;
  199.     char *pathStart=path;
  200.     HTREEITEM current=m_DrivesRoot;
  201.     strcpy(path,path_in);
  202.     Expand(current,TVE_EXPAND );
  203.     token=strtok (path,delimiters);
  204.     while (token!=NULL)
  205.         {
  206.         current=DirToItem(current, token);
  207.         if (!current)
  208.             {
  209.             return false;
  210.             }   
  211.         Expand(current,TVE_EXPAND );
  212.         token=strtok(NULL,delimiters);
  213.         }
  214.     SelectItem(current);
  215.     return true;
  216. }
  217. HTREEITEM CDirTreeCtrl::DirToItem(HTREEITEM current, char *dir)
  218. {
  219.     if (current==m_DrivesRoot)
  220.         {
  221.         HTREEITEM child;
  222.         CString szDir(dir);
  223.         szDir+="\";
  224.         szDir.MakeUpper();
  225. char *ptr=m_DrivesName;
  226. child=GetChildItem(current);
  227. while (child)
  228.     {
  229. if (szDir==CString(ptr))
  230.      {
  231. return child;
  232.     }
  233. ptr+=strlen(ptr)+1;
  234. child=GetNextItem(child, TVGN_NEXT);
  235.     }
  236.         }
  237.     else
  238.         {
  239.         HTREEITEM child;
  240.         CString szChildText;
  241.         CString szDir(dir);
  242.         szDir.MakeUpper();
  243. child=GetChildItem(current);
  244. while (child)
  245.     {
  246.             szChildText=GetItemText(child);
  247.             szChildText.MakeUpper();
  248. if ( szChildText==szDir)
  249.      {
  250. return child;
  251.     }
  252. child=GetNextItem(child, TVGN_NEXT);
  253.     }
  254.         }
  255.     return NULL;
  256. }
  257. int CDirTreeCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  258. {
  259. if (CTreeCtrl::OnCreate(lpCreateStruct) == -1)
  260. return -1;
  261. Initialize(m_list);
  262. return 0;
  263. }