//********************************************************* // //设置TreeCtrl的风格 // DWORD dwStyle = ::GetWindowLong(m_treeCtrl.GetSafeHwnd(), GWL_STYLE); dwStyle |= TVS_HASLINES; //Has Lines : true dwStyle |= TVS_HASBUTTONS; //Has Buttons : true dwStyle |= TVS_LINESATROOT; //Lines At Root : true ::SetWindowLong(m_treeCtrl.GetSafeHwnd(), GWL_STYLE, dwStyle); //********************************************************* // //插入Item // //(1)Tree Item纯文本形势 //函数原型: HTREEITEM InsertItem(LPCTSTR lpszItem, HTREEITEM hParent = TVI_ROOT, HTREEITEM hInsertAfter = TVI_LAST); //示例代码: HTREEITEM hTreeItem; hTreeItem = m_treeCtrl.InsertItem(TEXT("001")); hTreeItem = m_treeCtrl.InsertItem(TEXT("002")); m_treeCtrl.InsertItem(TEXT("003"), treeItem); //(2)Tree Item中加入图片 //函数原型: HTREEITEM InsertItem(LPCTSTR lpszItem, int nImage, int nSelectedImage, HTREEITEM hParent = TVI_ROOT, HTREEITEM hInsertAfter = TVI_LAST); //示例代码: CImageList m_imgList; //对话框成员变量 m_imgList.Create(16, 16, ILC_COLOR32, 2, 1); //Size: 16x16; nInitial=2; nGrow=1 m_imgList.Add(AfxGetApp()->LoadIcon(IDI_ICON_001)); m_treeCtrl.SetImageList(&m_imgList, TVSIL_NORMAL); HTREEITEM hTreeItem; hTreeItem = m_treeCtrl.InsertItem(TEXT("001"), 0, 1); hTreeItem = m_treeCtrl.InsertItem(TEXT("002"), 0, 1); m_treeTest.InsertItem(TEXT("003"), 0, 1, hTreeItem); //********************************************************* // //删除Item // BOOL DeleteItem(HTREEITEM hItem); BOOL DeleteAllItems(); //********************************************************* // //遍历某Tree Item下的所有子节点(需用到递归) // void fun(HTREEITEM hTreeItem) { HTREEITEM hTreeChild = m_treeCtrl.GetChildItem(hTreeItem); while (hTreeChild != NULL) { doSomething(hTreeChild); if (m_treeCtrl.ItemHasChildren(hTreeChild)) { fun(hTreeChild); //递归调用 } hTreeChild = m_treeCtrl.GetNextSiblingItem(hTreeChild); } } // //遍历Tree Ctrl下的所有节点 // HTREEITEM hRootItem = m_treeCtrl.GetRootItem(); while (hRootItem != NULL) { doSomething(hRootItem); if ((m_treeCtrl.ItemHasChildren(hRootItem)) { fun(hRootItem); } hRootItem = m_treeCtrl.GetNextSiblingItem(hRootItem); } //Note:GetRootItem()返回所有RootItem中的第一个 // 所以,遍历所有节点必须还要调用GetNextSiblingItem //********************************************************* // //用代码选中某项或取消选中 // m_treeCtrl.SetItemState(hItem, TVIS_SELECTED, TVIS_SELECTED); m_treeCtrl.SetItemState(hItem, 0, TVIS_SELECTED); //********************************************************* // //设置Item项的Image // BOOL SetItemImage(HTREEITEM hItem, int nImage, int nSelectedImage); //********************************************************* // //Others // CString GetItemText(HTREEITEM hItem) const; BOOL SetItemText(HTREEITEM hItem, LPCTSTR lpszItem); HTREEITEM GetSelectedItem( ) const; DWORD_PTR GetItemData(HTREEITEM hItem) const; BOOL SetItemData(HTREEITEM hItem, DWORD_PTR dwData); UINT GetCount() const;