DirTreeCtrl.cpp
上传用户:shouhua
上传日期:2014-12-06
资源大小:5685k
文件大小:7k
- // DirTreeCtrl.cpp : implementation file
- //
- #include "stdafx.h"
- #include "AntiEvilTools.h"
- #include "DirTreeCtrl.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- MyList *Gogogo2;
- /////////////////////////////////////////////////////////////////////////////
- // CDirTreeCtrl
- CDirTreeCtrl::CDirTreeCtrl()
- {
- }
- CDirTreeCtrl::~CDirTreeCtrl()
- {
- }
- BEGIN_MESSAGE_MAP(CDirTreeCtrl, CTreeCtrl)
- //{{AFX_MSG_MAP(CDirTreeCtrl)
- ON_NOTIFY_REFLECT(TVN_ITEMEXPANDED, OnItemexpanded)
- ON_WM_CREATE()
- ON_WM_RBUTTONDOWN()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CDirTreeCtrl message handlers
- void CDirTreeCtrl::Initialize(MyList *list)
- {
- LPITEMIDLIST itemlist;
- m_list=list;
- char buf[256];
- int iComputerIcon;
- DeleteAllItems();
- SetImageList (NULL,0);
- SHGetSpecialFolderLocation (this->m_hWnd, CSIDL_DESKTOP, &itemlist);
- SHGetPathFromIDList(itemlist, buf );
- m_DesktopPath=buf;
- m_ImageList.Create( GetSystemMetrics(SM_CXSMICON),
- GetSystemMetrics(SM_CYSMICON),
- ILC_COLOR24, 50, 50);
- m_ImageList.SetBkColor( GetSysColor(COLOR_WINDOW) );
- iComputerIcon = m_ImageList.Add(ExtractIcon( AfxGetApp()->m_hInstance, "shell32.dll", 15));
- m_DrivesRoot = InsertItems("我的电脑", TVI_ROOT, true, iComputerIcon, iComputerIcon);
- }
- HTREEITEM CDirTreeCtrl::AddItem(const char * path, HTREEITEM parent, bool isDir, int iconEx, int iconEx2)
- {
- SHFILEINFO shinfo, shinfo_sel;
- SHGetFileInfo( path, NULL,
- &shinfo,
- sizeof(shinfo),
- SHGFI_DISPLAYNAME | SHGFI_ICON | SHGFI_SMALLICON);
- SHGetFileInfo( path, NULL,
- &shinfo_sel,
- sizeof(shinfo_sel),
- SHGFI_DISPLAYNAME | SHGFI_ICON | SHGFI_OPENICON | SHGFI_SMALLICON);
- int icon = iconEx !=-1 ? iconEx : m_ImageList.Add(shinfo.hIcon);
- int iconsel = iconEx2!=-1 ? iconEx2 : m_ImageList.Add(shinfo_sel.hIcon);
- SetImageList (&m_ImageList,LVSIL_NORMAL);
- return InsertItems ( shinfo.szDisplayName, parent, isDir, icon, iconsel);
- }
- void CDirTreeCtrl::OnItemexpanded(NMHDR* pNMHDR, LRESULT* pResult)
- {
- NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
- TV_ITEM itm= pNMTreeView->itemNew;
- if (itm.state & TVIS_EXPANDED)
- ExpandBranch(itm.hItem);
- else
- {
- DeleteAllChild(itm.hItem);
- }
-
- *pResult = 0;
- }
- void CDirTreeCtrl::DeleteAllChild(HTREEITEM itm, bool isEmpty)
- {
- HTREEITEM child;
- child= GetChildItem(itm);
- while (child)
- {
-
- if (GetChildItem(child))
- DeleteAllChild(GetChildItem(child), isEmpty);
- DeleteItem (child);
- SetImageList(&m_ImageList,TVSIL_NORMAL);
- child= GetChildItem(itm);
- }
- if (isEmpty)
- InsertItem( "",itm);;
- }
- void CDirTreeCtrl::ExpandBranch(HTREEITEM parent)
- {
- bool bOne=false;
- int index=0;
- if(parent==m_DrivesRoot)
- {
- char *ptr;
- DeleteAllChild(parent,false);
- GetLogicalDriveStrings(500,m_DrivesName);
- ptr=m_DrivesName;
- while (*ptr)
- {
- bOne=true;
- AddItem(ptr, m_DrivesRoot, true);
- ptr+=strlen(ptr)+1;
- }
- }
- else
- {
- m_list->DeleteAllItems();
- m_strFullPath = "";
- CString pathWildcard;
- m_strFullPath=GetPathFromHere(m_strFullPath, parent);
- DeleteAllChild(parent,false);
- pathWildcard=m_strFullPath+"*.*";
- CFileFind finder;
- BOOL bWorking = finder.FindFile(pathWildcard);
- while (bWorking)
- {
- bWorking = finder.FindNextFile();
- if ( finder.IsDirectory() && !finder.IsDots() )
- {
- bOne=true;
- HTREEITEM itm=AddItem (m_strFullPath+finder.GetFileName(), parent );
- InsertItem("",itm);
- }
- }
- bWorking = finder.FindFile(pathWildcard);
- while (bWorking)
- {
- bWorking = finder.FindNextFile();
- if ( !finder.IsDirectory())
- {
- bOne=true;
- m_list->InsertItem(index, finder.GetFileName());
- m_list->SetItemText(index,1,m_strFullPath+finder.GetFileName());
- m_list->SetItem(index, 0, LVIF_TEXT | LVIF_IMAGE,finder.GetFileName(),
- Gogogo2->GetFileIcon(m_strFullPath+finder.GetFileName()), 0, 0, 0);
- index++;
- //AddItem (m_strFullPath+finder.GetFileName(), parent );
- }
- }
- }
- if (!bOne) InsertItem( "",parent);
- }
- HTREEITEM CDirTreeCtrl::InsertItems(const char * name, HTREEITEM parent, bool isDir, int icon,int iconOpen)
- {
- HTREEITEM hitm;
- hitm=InsertItem( name, icon, iconOpen, parent);
- if (isDir) InsertItem( "",hitm);
- return hitm;
- }
- CString CDirTreeCtrl::GetPathFromHere(CString s, HTREEITEM itm)
- {
- HTREEITEM parent = GetParentItem(itm);
- HTREEITEM itmChild = GetChildItem(itm);
- if(!itmChild) return GetPathFromHere(s,parent);
- if (itm==m_DrivesRoot) return "";
- if (parent==m_DrivesRoot)
- {
- HTREEITEM child;
- char *ptr=m_DrivesName;
- child=GetChildItem(parent);
- while (child)
- {
- if (child==itm)
- {
- s=CString(ptr)+s;
- return s;
- }
- ptr+=strlen(ptr)+1;
- child=GetNextItem(child, TVGN_NEXT);
- }
- }
- else if (itm==m_DesktopRoot)
- {
- s=m_DesktopPath+'\'+s;
- return s;
- }
- else
- {
- if (GetItemText(itm)!= CString(""))
- s=GetItemText(itm)+'\'+s;
- s=GetPathFromHere(s,parent);
- }
- if (!itmChild && s.GetLength()>0)
- s=s.Left (s.GetLength()-1);
- return s;
- }
- CString CDirTreeCtrl::GetFullPath(HTREEITEM item)
- {
- if (GetItemText(item)==CString("")) return "";
- else return GetPathFromHere("",item);
- }
- bool CDirTreeCtrl::SetFolder(CString path_in)
- {
- char path[MAX_PATH];
- char *delimiters="\";
- char *token;
- char *pathStart=path;
- HTREEITEM current=m_DrivesRoot;
- strcpy(path,path_in);
- Expand(current,TVE_EXPAND );
- token=strtok (path,delimiters);
- while (token!=NULL)
- {
- current=DirToItem(current, token);
- if (!current)
- {
- return false;
- }
- Expand(current,TVE_EXPAND );
- token=strtok(NULL,delimiters);
- }
- SelectItem(current);
- return true;
- }
- HTREEITEM CDirTreeCtrl::DirToItem(HTREEITEM current, char *dir)
- {
- if (current==m_DrivesRoot)
- {
- HTREEITEM child;
- CString szDir(dir);
- szDir+="\";
- szDir.MakeUpper();
- char *ptr=m_DrivesName;
- child=GetChildItem(current);
- while (child)
- {
- if (szDir==CString(ptr))
- {
- return child;
- }
- ptr+=strlen(ptr)+1;
- child=GetNextItem(child, TVGN_NEXT);
- }
- }
- else
- {
- HTREEITEM child;
- CString szChildText;
- CString szDir(dir);
- szDir.MakeUpper();
- child=GetChildItem(current);
- while (child)
- {
- szChildText=GetItemText(child);
- szChildText.MakeUpper();
- if ( szChildText==szDir)
- {
- return child;
- }
- child=GetNextItem(child, TVGN_NEXT);
- }
- }
- return NULL;
- }
- int CDirTreeCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CTreeCtrl::OnCreate(lpCreateStruct) == -1)
- return -1;
- Initialize(m_list);
- return 0;
- }