//****************************************** // 获取结点的子孙结点数 //****************************************** int GetDesendants(HSTREEITEM hItem) { int nRet=0; HSTREEITEM hChild=GetChildItem(hItem); while(hChild) { nRet += 1+GetDesendants(hChild); hChild=GetNextSiblingItem(hChild); } return nRet; }
int KGTreeCtrl::ExpandItem(HTREEITEM hTreeItem) { if (hTreeItem) { Expand(hTreeItem, TVE_EXPAND); ExpandItem(GetChildItem(hTreeItem)); ExpandItem(GetNextSiblingItem(hTreeItem)); } return true; }
BOOL CCJShellTree::SearchTree(HTREEITEM treeNode, CString szSearchName, FindAttribs attr) { LPTVITEMDATA lptvid; //Long pointer to TreeView item data LPSHELLFOLDER lpsf2=NULL; char drive[_MAX_DRIVE]; char dir[_MAX_DIR]; char fname[_MAX_FNAME]; char ext[_MAX_EXT]; BOOL bRet=FALSE; HRESULT hr; CString strCompare; szSearchName.MakeUpper(); while(treeNode && bRet==FALSE) { lptvid=(LPTVITEMDATA)GetItemData(treeNode); if (lptvid && lptvid->lpsfParent && lptvid->lpi) { hr=lptvid->lpsfParent->BindToObject(lptvid->lpi, 0,IID_IShellFolder,(LPVOID *)&lpsf2); if (SUCCEEDED(hr)) { ULONG ulAttrs = SFGAO_FILESYSTEM; lptvid->lpsfParent->GetAttributesOf(1, (const struct _ITEMIDLIST **)&lptvid->lpi, &ulAttrs); if (ulAttrs & (SFGAO_FILESYSTEM)) { if(SHGetPathFromIDList(lptvid->lpifq,strCompare.GetBuffer(MAX_PATH))) { switch(attr) { case type_drive: m_shell.SplitPath(strCompare,drive,dir,fname,ext); strCompare=drive; break; case type_folder: strCompare = GetItemText(treeNode); break; } strCompare.MakeUpper(); if(strCompare == szSearchName) { EnsureVisible(treeNode); SelectItem(treeNode); bRet=TRUE; } } } lpsf2->Release(); } } treeNode = GetNextSiblingItem(treeNode); } return bRet; }
void CCWTreeCtrl::CollapseBranch(HTREEITEM hItem) { // Courtesy of Zafir Anjumfrom www.codeguru.com if (ItemHasChildren(hItem)) { Expand(hItem, TVE_COLLAPSE); hItem = GetChildItem(hItem); do { CollapseBranch(hItem); } while((hItem = GetNextSiblingItem(hItem)) != NULL); } }
int KGTreeCtrl::ShrinkItem(HTREEITEM hTreeItem) { if (hTreeItem) { Expand(hTreeItem, TVE_TOGGLE); ShrinkItem(GetChildItem(hTreeItem)); ShrinkItem(GetNextSiblingItem(hTreeItem)); } return true; }
//************************************************ // 获取子结点数量 //************************************************ int GetChildrenCount(HSTREEITEM hItem) { int nRet=0; HSTREEITEM hChild=GetChildItem(hItem); while(hChild) { nRet++; hChild=GetNextSiblingItem(hChild); } return nRet; }
/* ================ CPathTreeCtrl::SearchTree Search the three using the search string. Adds the matched tree items to the result tree. Returns the number of items added to the result tree. ================ */ int CPathTreeCtrl::SearchTree( treeItemCompare_t compare, void *data, CPathTreeCtrl &result ) { idPathTreeStack stack, searchStack; HTREEITEM item, child; idStr name; int id, numItems; numItems = 0; result.DeleteAllItems(); stack.PushRoot( NULL ); item = GetRootItem(); searchStack.PushRoot( item ); id = 0; while( searchStack.Num() > 0 ) { for ( child = GetChildItem( item ); child; child = GetChildItem( child ) ) { searchStack.Push( item, GetItemText( item ) ); item = child; } name = searchStack.TopName(); name += GetItemText( item ); id = GetItemData( item ); if ( compare( data, item, name ) ) { result.AddPathToTree( name, id, stack ); numItems++; } for ( item = GetNextSiblingItem( item ); item == NULL; ) { item = GetNextSiblingItem( searchStack.TopItem() ); searchStack.Pop(); if ( searchStack.Num() <= 0 ) { return numItems; } } } return numItems; }
HTREEITEM CTreeFileCtrl::CopyBranch(HTREEITEM htiBranch, HTREEITEM htiNewParent, HTREEITEM htiAfter) { HTREEITEM hNewItem = CopyItem(htiBranch, htiNewParent, htiAfter); HTREEITEM hChild = GetChildItem(htiBranch); while (hChild != NULL) { //recursively transfer all the items CopyBranch(hChild, hNewItem); hChild = GetNextSiblingItem(hChild); } return hNewItem; }
BOOL CTreeFileCtrl::HasChildWithText(HTREEITEM hParent, const CString& sText) { HTREEITEM hChild = GetChildItem(hParent); while (hChild) { CString sItemText = GetItemText(hChild); if (sItemText.CompareNoCase(sText) == 0) return TRUE; hChild = GetNextSiblingItem(hChild); } return FALSE; }
HTREEITEM CZoneTree::MoveChildItem(HTREEITEM hItem, HTREEITEM htiNewParent, HTREEITEM htiAfter) { TV_INSERTSTRUCT tvstruct; HTREEITEM hNewItem; CString sText; int i; // get information of the source item tvstruct.item.hItem = hItem; tvstruct.item.mask = TVIF_CHILDREN | TVIF_HANDLE | TVIF_IMAGE | TVIF_SELECTEDIMAGE; GetItem(&tvstruct.item); sText = GetItemText( hItem ); tvstruct.item.cchTextMax = sText.GetLength(); tvstruct.item.pszText = sText.LockBuffer(); //insert the item at proper location tvstruct.hParent = htiNewParent; tvstruct.hInsertAfter = htiAfter; tvstruct.item.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT; hNewItem = InsertItem(&tvstruct); sText.ReleaseBuffer(); GetItemImage(hNewItem, i, i); switch(i) { case OBJECT_DATA: ConvertObjToIndex(hItem, hNewItem, htiNewParent); break; case OBJ_INDEX: CopyObjIndex(hItem, hNewItem, htiNewParent); break; case MOB_DATA: ConvertMobToIndex(hItem, hNewItem, htiNewParent); break; case MOB_INDEX: CopyMobIndex(hItem, hNewItem, htiNewParent); break; } SetItemState(hNewItem,GetItemState(hItem,TVIS_STATEIMAGEMASK),TVIS_STATEIMAGEMASK); HTREEITEM hItemChild = GetChildItem(hItem); while (hItemChild != NULL) { HTREEITEM hItemNextChild = GetNextSiblingItem(hItemChild); MoveChildItem(hItemChild, hNewItem, TVI_LAST); hItemChild = hItemNextChild; } return hNewItem; }
HTREEITEM CIMMain::FindUserITEM(CString szName) { try { HTREEITEM rootHti,hti; for(rootHti=GetChildItem(TVI_ROOT);rootHti!=NULL;rootHti=GetNextSiblingItem(rootHti)) { for(hti=GetChildItem(rootHti);hti!=NULL;hti=GetNextSiblingItem(hti)) { if(GetItemText(hti)== szName) return hti; } } } catch (...) { TCHAR smsg[1000]={0}; wsprintf(smsg,"IM_TRY_CATCH : [%s]",__FUNCTION__); } return NULL; }
LRESULT MFTreeView::OnItemExpanding(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/) { bool result = true; // TRUE == Dont Expand/collapse LPNMTREEVIEW pnmtv = (LPNMTREEVIEW)pnmh; MFTreeViewItem* pItem = (MFTreeViewItem*)(pnmtv->itemNew.lParam); if (pItem) { if (pnmtv->action == TVE_COLLAPSE) { // TODO: Make this a recursive algorithm incase we have open subnodes. WTL::CTreeItem item(pnmtv->itemNew.hItem, this); WTL::CTreeItem child = item.GetChild(); while (!child.IsNull()) { MFTreeViewItem* pChild = (MFTreeViewItem*)child.GetData(); child.SetData(NULL); if (pChild) { delete pChild; } child = GetNextSiblingItem(child); } SendMessage(m_hWnd, TVM_EXPAND, TVE_COLLAPSE | TVE_COLLAPSERESET, (LPARAM) pnmtv->itemNew.hItem); result = false; } else if (pnmtv->action == TVE_EXPAND && pItem->HasChildren()) { HTREEITEM hPrevItem = pnmtv->itemNew.hItem; std::vector<MFTreeViewItem*> children = pItem->GetChildren(); for (unsigned int i = 0; i < children.size(); ++i) { TVINSERTSTRUCT tvis = { 0 }; tvis.hParent = pnmtv->itemNew.hItem; tvis.hInsertAfter = pItem->SortChildren() ? TVI_SORT : hPrevItem; tvis.item.mask = TVIF_CHILDREN | TVIF_PARAM | TVIF_TEXT; tvis.item.pszText = LPSTR_TEXTCALLBACK; //tvis.item.iImage = nImage; //tvis.item.iSelectedImage = nSelectedImage; //tvis.item.state = nState; //tvis.item.stateMask = nStateMask; tvis.item.cChildren = I_CHILDRENCALLBACK; tvis.item.lParam = (LPARAM) children[i]; hPrevItem = InsertItem(&tvis); } result = false; } } return result ? TRUE : FALSE; }
void CTangramHtmlTreeExWnd::DeleteChildren(HTREEITEM hParent) { HTREEITEM hChild = GetChildItem(hParent); HTREEITEM hNext; while (hChild != NULL) { hNext = GetNextSiblingItem(hChild); DeleteItem(hChild); hChild = hNext; } }
void EXWaitingTreeCtrl::DeleteChildren(HTREEITEM hParent) { HTREEITEM hChild = GetChildItem(hParent); HTREEITEM hNext; while (hChild != NULL) { hNext = GetNextSiblingItem(hChild); DeleteItem(hChild); hChild = hNext; } }
HTREEITEM CTreeCtrlEx::ItemFromData(DWORD dwData, HTREEITEM hStartAtItem/*=NULL*/) const { // Traverse all items in tree control HTREEITEM hItem; if ( hStartAtItem ) hItem = hStartAtItem; else hItem = GetRootItem(); while ( hItem ) { if ( dwData == (DWORD)GetItemData( hItem ) ) return hItem; // Get first child node HTREEITEM hNextItem = GetChildItem( hItem ); if ( !hNextItem ) { // Get next sibling child hNextItem = GetNextSiblingItem( hItem ); if ( !hNextItem ) { HTREEITEM hParentItem=hItem; while ( !hNextItem && hParentItem ) { // No more children: Get next sibling to parent hParentItem = GetParentItem( hParentItem ); hNextItem = GetNextSiblingItem( hParentItem ); } } } hItem = hNextItem; } return NULL; }
void CMyTreeCtrl::DeleteItemData( HTREEITEM hItem ) { if( hItem == (HTREEITEM)NULL ) return; PSrvrData pData = (PSrvrData)GetItemData( hItem ); if( NULL != pData ) { safe_delete( pData ); SetItemData( hItem, NULL ); } if( TRUE == ItemHasChildren( hItem ) ) { DeleteItemData( GetChildItem( hItem ) ); DeleteItemData( GetNextSiblingItem( hItem ) ); } else { DeleteItemData( GetNextSiblingItem( hItem ) ); } }
HTREEITEM KGTreeCtrl::FindNextItembySubString(CString strText, HTREEITEM hTreeItem) { //没有数据返回NULL int nCount = GetCount(); if ( !nCount ) return NULL; HTREEITEM FindNode = NULL; if (!hTreeItem) hTreeItem = GetRootItem(); if (ItemHasChildren(hTreeItem)) { FindNode = GetChildItem(hTreeItem); FindNode = FindItembySubString(strText,FindNode); KG_PROCESS_SUCCESS(FindNode); } FindNode = GetNextSiblingItem(hTreeItem); if (FindNode) { FindNode = FindItembySubString(strText,FindNode); KG_PROCESS_SUCCESS(FindNode); } while((hTreeItem = GetParentItem(hTreeItem)) != 0) { FindNode = GetNextSiblingItem(hTreeItem); if (FindNode) { FindNode = FindItembySubString(strText,FindNode); KG_PROCESS_SUCCESS(FindNode); } } return NULL; Exit1: return FindNode; }
HTREEITEM CTreeListCtrl::GetTreeItem(int nItem) { HTREEITEM m_ParentItem = GetRootItem(); int m_nCount = 0; while((m_ParentItem!=NULL) && (m_nCount<nItem)) { m_nCount ++ ; GetNextSiblingItem(m_ParentItem); } return m_ParentItem; }
int CTreeListCtrl::GetListItem(HTREEITEM hItem) { HTREEITEM m_ParentItem = GetRootItem(); int m_nCount = 0; while((m_ParentItem!=NULL) && (m_ParentItem!=hItem)) { m_nCount ++ ; GetNextSiblingItem(m_ParentItem); } return m_nCount; }
void CDirectoryTreeCtrl::MarkChilds(HTREEITEM hChild,bool mark) { CheckChanged(hChild, mark); SetCheck(hChild,mark); Expand(hChild, TVE_TOGGLE); // VQB - make sure tree has entries HTREEITEM hChild2; hChild2 = GetChildItem(hChild); while( hChild2 != NULL) { MarkChilds(hChild2,mark); hChild2 = GetNextSiblingItem( hChild2 ); } Expand(hChild, TVE_TOGGLE); // VQB - restore tree to initial disposition }
HTREEITEM FolderTree::FindServersNode(HTREEITEM hFindFrom) const { if (m_bDisplayNetwork) { //Try to find some "servers" in the child items of hFindFrom HTREEITEM hChild = GetChildItem(hFindFrom); while (hChild) { FolderTreeItemInfo* pItem = (FolderTreeItemInfo*) GetItemData(hChild); if (pItem->m_pNetResource) { //Found a share if (pItem->m_pNetResource->dwDisplayType == RESOURCEDISPLAYTYPE_SERVER) return hFindFrom; } //Get the next sibling for the next loop around hChild = GetNextSiblingItem(hChild); } //Ok, since we got here, we did not find any servers in any of the child nodes of this //item. In this case we need to call ourselves recursively to find one hChild = GetChildItem(hFindFrom); while (hChild) { HTREEITEM hFound = FindServersNode(hChild); if (hFound) return hFound; //Get the next sibling for the next loop around hChild = GetNextSiblingItem(hChild); } } //If we got as far as here then no servers were found. return NULL; }
// Recursively sorts a node and its children void CMyTreeCtrl::SortNodeAndChildren(HTREEITEM hItem) { // Sort the node SortChildren(hItem); // Determine if this item has children if (ItemHasChildren(hItem)) { // Get the first child item HTREEITEM hChild=GetChildItem(hItem); // Sort the child SortNodeAndChildren(hChild); // Sort the rest of the children HTREEITEM hSibling=GetNextSiblingItem(hChild); while (hSibling) { SortNodeAndChildren(hSibling); hSibling=GetNextSiblingItem(hSibling); } } }
HTREEITEM CTreeCtrlEx::CopyBranch(HTREEITEM htiBranch, HTREEITEM htiNewParent, HTREEITEM htiAfter) { HTREEITEM hChild; HTREEITEM hNewItem = CopyItem( htiBranch,htiNewParent,htiAfter ); hChild = GetChildItem( htiBranch ); while( hChild != NULL ) { CopyBranch( hChild,hNewItem,htiAfter ); hChild = GetNextSiblingItem( hChild ); } return hNewItem; }
void CCWTreeCtrl::OnCollapseAll() { // Courtesy of Zafir Anjum from www.codeguru.com // Updated to test for zero entries! HTREEITEM hItem = GetRootItem(); if (hItem == NULL) return; SetRedraw(FALSE); do { CollapseBranch(hItem); } while((hItem = GetNextSiblingItem(hItem)) != NULL); SetRedraw(TRUE); }
void LocalsTree::refresh(){ if( !envron || !frames.size() ) return; HTREEITEM item=GetChildItem( TVI_ROOT ); int n=0; for( n=0;n<frames.size();++n ){ if( !item || item!=frames[n].item ) break; item=GetNextSiblingItem( item ); } while( item ){ HTREEITEM next=GetNextSiblingItem( item ); DeleteItem( item ); item=next; } for( ;n<frames.size();++n ){ item=frames[n].item=InsertItem( frames[n].func,TVI_ROOT,TVI_LAST ); if( n<frames.size()-1 ) refreshFrame( frames[n] ); } refreshFrame( frames.back() ); }
//**************************************************** // 获取当前结点的下一个结点 // HSTREEITEM hItem:当前结点 // return:当前结点的下一个结点 // remark:如果当前结点有子结点,则返回自己的第一个子结点, // 否则如果有向下的兄弟结点,则返回自己向下兄弟结点、 // 否则搜索自己的父结点的向下兄弟结点 //**************************************************** HSTREEITEM GetNextItem(HSTREEITEM hItem) { if(hItem==STVI_ROOT) return (HSTREEITEM)m_hRootFirst; HSTREEITEM hRet=GetChildItem(hItem); if(hRet) return hRet; HSTREEITEM hParent=hItem; while(hParent) { hRet=GetNextSiblingItem(hParent); if(hRet) return hRet; hParent=GetParentItem(hParent); } return NULL; }
void CDirectoryTreeCtrl::MarkChilds(HTREEITEM hChild, bool mark) { CheckChanged(hChild, mark); SetCheck(hChild,mark); Expand(hChild, TVE_TOGGLE); HTREEITEM hChild2; hChild2 = GetChildItem(hChild); while( hChild2 != NULL) { MarkChilds(hChild2,mark); hChild2 = GetNextSiblingItem( hChild2 ); } Expand(hChild, TVE_TOGGLE); }
HTREEITEM KGTreeCtrl::GetLastSibingItem(HTREEITEM hTreeItem) { HTREEITEM hResultItem = NULL; HTREEITEM hNextItem = NULL; KG_PROCESS_ERROR(hTreeItem); hNextItem = hTreeItem; while (hNextItem) { hResultItem = hNextItem; hNextItem = GetNextSiblingItem(hNextItem); } Exit0: return hResultItem; }
// Returns the number of children of this group int CCWTreeCtrl::CountChildren(HTREEITEM hStartItem) const { // Walk the Tree! int num = 0; if (hStartItem != NULL && ItemHasChildren(hStartItem)) { HTREEITEM hChildItem = GetChildItem(hStartItem); while (hChildItem != NULL) { if (ItemHasChildren(hChildItem)) { num += CountChildren(hChildItem); } else { num++; } hChildItem = GetNextSiblingItem(hChildItem); } } return num; }
void CDirectoryTreeCtrl::OnTvnKeydown(NMHDR *pNMHDR, LRESULT *pResult) { LPNMTVKEYDOWN pTVKeyDown = reinterpret_cast<LPNMTVKEYDOWN>(pNMHDR); switch(pTVKeyDown->wVKey) { case VK_SPACE: case VK_RETURN: { HTREEITEM hItem = GetSelectedItem(); if(hItem) { HTREEITEM tItem = GetFirstVisibleItem(); CheckChanged(hItem, !GetCheck(hItem)); if(m_bCtrlPressed) { Expand(hItem, TVE_TOGGLE); HTREEITEM hChild; hChild = GetChildItem(hItem); while(hChild != NULL) { MarkChilds(hChild,!GetCheck(hItem)); hChild = GetNextSiblingItem( hChild ); } SetCheck(hItem, !GetCheck(hItem)); Expand(hItem, TVE_TOGGLE); } SelectSetFirstVisible(tItem); } break; } default: break; } *pResult = 0; }