// 모든 트리노드를 펼친다. void CPanelBase::ExpandAll(CTreeCtrl &treeCtrl) { HTREEITEM hRoot = treeCtrl.GetRootItem(); vector<HTREEITEM> items; items.reserve(treeCtrl.GetCount()); items.push_back(hRoot); while (!items.empty()) { HTREEITEM hItem = items.back(); items.pop_back(); treeCtrl.Expand(hItem, TVE_EXPAND); HTREEITEM hfirstChild = treeCtrl.GetChildItem(hItem); if (hfirstChild) { items.push_back(hfirstChild); while (HTREEITEM hNextItem = treeCtrl.GetNextSiblingItem(hfirstChild)) { items.push_back(hNextItem); hfirstChild = hNextItem; } } } treeCtrl.SelectSetFirstVisible(hRoot); }
HTREEITEM DLitbookTree::FindItem(CTreeCtrl& tree, HTREEITEM item, DWORD strText,int* findLevel,int* curLevel) { HTREEITEM hFind; if(item == NULL) return NULL; while(item != NULL) { if(*findLevel==*curLevel) { //只有在到达指定深度时,才比较 DWORD cc=tree.GetItemData(item); if(cc == strText) return item; } if(tree.ItemHasChildren(item)) { (*curLevel)++;//深度加一 item = tree.GetChildItem(item); hFind = FindItem(tree, item, strText,findLevel,curLevel); if(hFind) { return hFind; } else { item= tree.GetNextSiblingItem(tree.GetParentItem(item)); (*curLevel)--;//深度减一 } } else { item = tree.GetNextSiblingItem(item); if(item == NULL) return NULL; } } return item; }
HTREEITEM FindSiblingItem(const CTreeCtrl &rTree, const HTREEITEM hStartingItem, const string &rStr) { HTREEITEM hItem = hStartingItem; while( hItem != NULL ) { if( rTree.GetItemText( hItem ).Compare( rStr.c_str() ) == 0 ) break; else hItem = rTree.GetNextSiblingItem( hItem ); } return hItem; }
HTREEITEM GetTreeItemFromData(CTreeCtrl& treeCtrl, DWORD dwData, HTREEITEM hStartAtItem /*=NULL*/) { // Traverse from given item (or all items if hFromItem is NULL) HTREEITEM hItem; if ( hStartAtItem ) hItem=hStartAtItem; else hItem = treeCtrl.GetRootItem(); while ( hItem ) { if ( dwData == (DWORD)treeCtrl.GetItemData( hItem ) ) return hItem; // Get first child node HTREEITEM hNextItem = treeCtrl.GetChildItem( hItem ); if ( !hNextItem ) { // Get next sibling child hNextItem = treeCtrl.GetNextSiblingItem( hItem ); if ( !hNextItem ) { HTREEITEM hParentItem=hItem; while ( !hNextItem && hParentItem ) { // No more children: Get next sibling to parent hParentItem = treeCtrl.GetParentItem( hParentItem ); hNextItem = treeCtrl.GetNextSiblingItem( hParentItem ); } } } hItem = hNextItem; } return NULL; }
void CIISConfigHelper::RecurseTreeAndSortItems(CTreeCtrl& cTree, HTREEITEM htParent) { if (htParent == NULL) return; cTree.SortChildren(htParent); HTREEITEM htChild = cTree.GetChildItem(htParent); while (htChild != NULL) { RecurseTreeAndSortItems(cTree, htChild); htChild = cTree.GetNextSiblingItem(htChild); } }
void CDocSelectDlg::OnOK() { CTreeCtrl* pTree = (CTreeCtrl*)GetDlgItem(IDC_DOCLIST_TRE); HTREEITEM hItem = pTree->GetRootItem(); int i = 0; int nItemsDeleted = 0; while (hItem) { if (! pTree->GetCheck(hItem)) { m_aDocNames.RemoveAt(i - nItemsDeleted); nItemsDeleted++; i++; } hItem = pTree->GetNextSiblingItem(hItem); } CDialog::OnOK(); }
void CIISConfigHelper::RecurseTreeAndPopulateFromFileSystem(CTreeCtrl& cTree, HTREEITEM htParent, const std::set<CStringW>& IgnoreDirNames, int nMaxDepth) { if (htParent == NULL) return; std::vector<HTREEITEM> Children; HTREEITEM htChild = cTree.GetChildItem(htParent); while (htChild != NULL) { Children.push_back(htChild); htChild = cTree.GetNextSiblingItem(htChild); } CIISConfigHelper::PopulateTreeFromFileSystem(cTree, htParent, IgnoreDirNames, nMaxDepth); for (int i = 0; i < (int) Children.size(); i++) { RecurseTreeAndPopulateFromFileSystem(cTree, Children[i], IgnoreDirNames, nMaxDepth - 1); } }
HTREEITEM CPanelBase::FindTree( CTreeCtrl &treeCtrl, const wstring &findText ) { HTREEITEM hRoot = treeCtrl.GetRootItem(); vector<HTREEITEM> items; items.reserve(treeCtrl.GetCount()); wstring lowerText = findText; lowerCasew(lowerText); items.push_back(hRoot); while (!items.empty()) { HTREEITEM hItem = items.back(); items.pop_back(); CString itemText = treeCtrl.GetItemText(hItem); CString lowerItemText = lowerCasew((wstring)itemText).c_str(); const int idx = lowerItemText.Find(lowerText.c_str()); if (idx >= 0) return hItem; HTREEITEM hfirstChild = treeCtrl.GetChildItem(hItem); if (hfirstChild) { items.push_back(hfirstChild); while (HTREEITEM hNextItem = treeCtrl.GetNextSiblingItem(hfirstChild)) { items.push_back(hNextItem); hfirstChild = hNextItem; } } } return NULL; }
void CIISConfigHelper::PopulateTreeFromFileSystem(CTreeCtrl& cTree, HTREEITEM htParent, const std::set<CStringW>& IgnoreDirNames, int nMaxDepth) { if (htParent == NULL) return; // get the item data from the tree item IISURIItem* pParentItem = (IISURIItem*) cTree.GetItemData(htParent); if (pParentItem == NULL) { ASSERT(pParentItem != NULL); return; } // if the item is incomplete then we need to get it's file system path if (pParentItem->Type == IISURIItem::IncompleteFileSystemPath && pParentItem->sFileSystemPath.GetLength() <= 0) { // get the grand parent item HTREEITEM htGrandParent = cTree.GetParentItem(htParent); if (htGrandParent == NULL) { ASSERT(htGrandParent != NULL); return; } // get the grand parent item data IISURIItem* pGrandParentItem = (IISURIItem*) cTree.GetItemData(htGrandParent); if (pGrandParentItem == NULL) { ASSERT(pGrandParentItem != NULL); return; } // the grand parent MUST not be incomplete if (pGrandParentItem->Type == IISURIItem::IncompleteFileSystemPath) { ASSERT(pGrandParentItem->Type != IISURIItem::IncompleteFileSystemPath); return; } // get the item name CStringW sName = CStringW(cTree.GetItemText(htParent)); if (sName.GetLength() <= 0) { ASSERT(sName.GetLength() > 0); return; } // make the path to the parent item CPathT<CStringW> ItemFileSystemPath(pGrandParentItem->sFileSystemPath); ItemFileSystemPath.Append(sName); // assign the new file system path and set the type pParentItem->sFileSystemPath = (LPCWSTR) ItemFileSystemPath; pParentItem->Type = IISURIItem::FileSystem; } // if the item already has children then we need to build up a list of their names std::set<CStringW> KnownDirs; if (cTree.ItemHasChildren(htParent) == TRUE) { // loop through all the children HTREEITEM htChild = cTree.GetChildItem(htParent); while (htChild != NULL) { // get the child name CStringW sName = CStringW(cTree.GetItemText(htChild)); // we need lower case so we can compare easier sName.MakeLower(); // add the name to the known list KnownDirs.insert(sName); // move on to the next child item htChild = cTree.GetNextSiblingItem(htChild); } } #ifdef _DEBUG ATLTRACE2(L"CIISConfigHelper::PopulateTreeFromFileSystem() : searching '%s'\n", pParentItem->sFileSystemPath); #endif // create a search string CPathT<CStringW> FileSystemPath(pParentItem->sFileSystemPath); FileSystemPath.Append(L"*.*"); CStringW sSearchPath = (LPCWSTR) FileSystemPath; // start the search WIN32_FIND_DATAW FindData; HANDLE hFind = ::FindFirstFileW(sSearchPath, &FindData); if (hFind == INVALID_HANDLE_VALUE) { // TODO: empty return; } CArray<CStringW, LPCWSTR> saDirs; // guess that we will have 32 sub dirs (the array will grow if we have more) saDirs.SetSize(32); // keep track of the number of dirs we have actually found int nDirsFound = 0; do { if ((FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) continue; // eliminate . and .. from the search if (FindData.cFileName[0] == '.') { if (FindData.cFileName[1] == '\0') { continue; } else if (FindData.cFileName[1] == '.' && FindData.cFileName[2] == '\0') { continue; } } // convert to lower case CStringW sTempFileName = FindData.cFileName; sTempFileName.MakeLower(); // only add to the collection if we don't already know about it if (KnownDirs.find(sTempFileName) != KnownDirs.end()) continue; // if it's a known ignore dir name - then ignore it if (IgnoreDirNames.find(sTempFileName) != IgnoreDirNames.end()) continue; #ifdef _DEBUG ATLTRACE2(L"CIISConfigHelper::PopulateTreeFromFileSystem() : found '%s'\n", FindData.cFileName); #endif saDirs.SetAtGrow(nDirsFound, FindData.cFileName); // we have found one nDirsFound++; } while (::FindNextFileW(hFind, &FindData) == TRUE); // get rid of the find handle ::FindClose(hFind); hFind = NULL; if (nDirsFound <= 0) { return; } if (nMaxDepth <= 0) { cTree.InsertItem(EmptyNodeString, htParent); return; } for (int i = 0; i < nDirsFound; i++) { CString sDir(saDirs[i]); HTREEITEM htChild = cTree.InsertItem(sDir, htParent); // create the child file system path CPathT<CStringW> FileSystemPath(pParentItem->sFileSystemPath); FileSystemPath.Append(saDirs[i]); // we need an item data for this tree item IISURIItem* pChildItem = new IISURIItem; pChildItem->Type = IISURIItem::FileSystem; pChildItem->sFileSystemPath = (LPCWSTR) FileSystemPath; // make the child URI CStringW sChildURI = pParentItem->sURI; int nChildURILength = sChildURI.GetLength(); if (nChildURILength > 0 && sChildURI[nChildURILength - 1] != '/') sChildURI += '/'; // append the dir name sChildURI += saDirs[i]; // assign the URI to the item data pChildItem->sURI = sChildURI; // store the item data cTree.SetItemData(htChild, (DWORD_PTR) pChildItem); PopulateTreeFromFileSystem(cTree, htChild, IgnoreDirNames, nMaxDepth - 1); } }
void CChatDlg::LoadPicFiles(CTreeCtrl& nTreeCtrl, CString nFilePath, HTREEITEM nRoot) { // 判断nPicFolder是目录还是文件 // 如果是文件 // 直接将文件插入到树控件中 nTreeCtrl.InsertItem(nPicFolder,nRoot); // 如果是目录 // 获取nPicFolder的第一层目录 // 判断nRoot目录下是否已经有此层目录 // 如果有此层目录 // 递归插入其他 // 如果无此层目录 // 插入此层目录,然后递归插入其他 CString nSubFolder; //首层目录 CString nSubFilePath; //去掉首层目录后的文件名 BOOL IsExist = FALSE; int nIndex = -1; nIndex = nFilePath.Find("/"); if (nIndex >= 0) //目录 { nSubFolder = nFilePath.Left(nIndex); nSubFilePath = nFilePath.Right(nFilePath.GetLength() - nIndex - 1); HTREEITEM nSubRoot = NULL; if (nTreeCtrl.ItemHasChildren(nRoot)) nSubRoot = nTreeCtrl.GetChildItem(nRoot); CString str; BOOL bExist = FALSE; while (nSubRoot) { str = nTreeCtrl.GetItemText(nSubRoot); if (str.CompareNoCase(nSubFolder) == 0) { bExist = TRUE; break; } nSubRoot = nTreeCtrl.GetNextSiblingItem(nSubRoot); } if (!bExist) { nSubRoot = nTreeCtrl.InsertItem(nSubFolder, nRoot); LoadPicFiles(nTreeCtrl, nSubFilePath, nSubRoot); } else{ LoadPicFiles(nTreeCtrl, nSubFilePath, nSubRoot); } } else { nTreeCtrl.InsertItem(nFilePath, 1, 1, nRoot, 0); } }