一 基础操作
1 插入节点
1)插入根节点
[cpp] view plaincopyprint?
- //插入根节点
- HTREEITEM hRoot;
- CString str=L"ROOT"
- hRoot=nTreeCtrl.InsertItem(str);
- //相当于
- hRoot=nTreeCtrl.InsertItem(str,TVI_ROOT,TVI_LAST);
2)插入孩子节点
[cpp] view plaincopyprint?
- //添加hRoot节点的孩子节点,并且被添加的节点位于hRoot所有孩子节点的末尾
- HTREEITEM hChild=nTreeCtrl.InsertItem(str,hRoot);
- //相当于
- HTREEITEM hChild=nTreeCtrl.InsertItem(str,hRoot,TVI_LAST);
2 获得节点句柄
[cpp] view plaincopyprint?
- //获得根节点
- HTREEITEM hRootItem;
- hRootItem=nTreeCtrl.GetRootItem();
- //获得当前节点
- HTREEITEM hCurrentItem;
- hCurrentItem=nTreeCtrl.GetSelectedItem();
- //获得hItem的前一个节点
- HTREEITEM hPreItem;
- hPreItem=nTreeCtrl.GetNextItem(hItem,TVGN_PREVIOUS);
- //获得hItem的下一个节点
- HTREEITEM hNextItem;
- hNextItem=nTreeCtrl.GetNextItem(hItem,TVGN_NEXT);
3 判断某节点是否有孩子节点
[cpp] view plaincopyprint?
- //判断某节点是否有孩子节点
- if (nTreeCtrl.ItemHasChildren(hRoot))
4 展开或收缩子节点
[cpp] view plaincopyprint?
- //展开
- if(nTreeCtrl.ItemHasChildren(hRoot))
- nTreeCtrl.Expand(hParentItem,TVE_EXPAND);
5 获得第一个孩子节点的句柄
[cpp] view plaincopyprint?
- //判断某节点是否有孩子节点
- if (nTreeCtrl.ItemHasChildren(hRoot))
- {
- //获得孩子节点
- HTREEITEM hChild=nTreeCtrl.GetChildItem(hRoot);
- }
6 遍历hRoot下一层的所有孩子节点
[cpp] view plaincopyprint?
- //判断某节点是否有孩子节点
- if (nTreeCtrl.ItemHasChildren(hRoot))
- {
- //获得孩子节点
- HTREEITEM hChild=nTreeCtrl.GetChildItem(hRoot);
- //遍历hRoot下一层的所有孩子节点
- while(hChild)
- {
- hChild=nTreeCtrl.GetNextItem(hChild,TVGN_NEXT);
- }
- }
7 获得某节点上的文字
[cpp] view plaincopyprint?
- //获得某节点上的文字
- CString str;
- nTreeCtrl.GetItemText(hRoot);
8 选择某节点,并让其获得焦点
首先,TREE控件的样式必须设置为TVS_SHOWSELALWAYS
其次: 选择该节点
[html] view plaincopyprint?
- treeCtrl.SelectItem(hItem);
最后,设置焦点
[html] view plaincopyprint?
- treeCtrl.SetFocus();
Tree控件设置焦点后,会自动将焦点定位到选择的节点上
9 清空树控件
[cpp] view plaincopyprint?
- <strong> nTreeCtrl.DeleteAllItems();</strong>
10 按照文件路径插入节点
[cpp] view plaincopyprint?
- void InsertPath(CString path, HTREEITEM hRoot, CTreeCtrl& ctrl)
- {
- CFileFind nFindFile;
- CString str=L"";
- CString nPicFileName=L"";
- BOOL IsExist=FALSE;
- HTREEITEM hSubItem;
- nPicFileName.Format(L"%s\\*.*",path);
- IsExist = nFindFile.FindFile(nPicFileName);
- while (IsExist)
- {
- IsExist = nFindFile.FindNextFile();
- if(nFindFile.IsDots())
- continue;
- nPicFileName = nFindFile.GetFileName();
- //路径
- if(nFindFile.IsDirectory())
- {
- hSubItem = ctrl.InsertItem(nPicFileName,hRoot);
- InsertPath(nFindFile.GetFilePath(),hSubItem,ctrl);
- }
- else
- {
- //文件
- str = nPicFileName.Right(4);
- if(!str.CompareNoCase(_T(".jpg")) || !str.CompareNoCase(_T(".tif")))
- {
- ctrl.InsertItem(nPicFileName,hRoot);
- }
- }
- }
- nFindFile.Close();
- }
[cpp] view plaincopyprint?
- void LoadPath(CString path)
- {
- CTreeCtrl& ctrl = GetTreeCtrl();
- ASSERT(ctrl);
- ctrl.DeleteAllItems();
- HTREEITEM hRoot = ctrl.InsertItem(path);
- InsertPath(path,hRoot,ctrl);
- ctrl.Expand(hRoot,TVE_EXPAND);
- }
二 扩展操作
1 响应TVN_ITEMEXPANDING 消息时 如何获得将要展开或收缩的那一个节点的句柄
MSDN:
TVN_ITEMEXPANDING pnmtv = (NM_TREEVIEW FAR *) lParam
pnmtv
- Pointer to an NM_TREEVIEW structure. The itemNew member is aTVITEM structure that contains valid information about the parent item in thehItem,state, andlParam members. Theaction member indicates whether the list is to expand or collapse. For a list of possible values, see the description of theTVM_EXPAND message.
。。。。。。。。。
[cpp] view plaincopyprint?
- typedef struct _NM_TREEVIEW {
- NMHDR hdr;
- UINT action;
- TV_ITEM itemOld;
- TV_ITEM itemNew;
- POINT ptDrag;
- } NM_TREEVIEW;
- typedef NM_TREEVIEW FAR* LPNM_TREEVIEW;
[cpp] view plaincopyprint?
- typedef struct _TV_ITEM { tvi
- UINT mask;
- HTREEITEM hItem;
- UINT state;
- UINT stateMask;
- LPSTR pszText;
- int cchTextMax;
- int iImage;
- int iSelectedImage;
- int cChildren;
- LPARAM lParam; }
- TV_ITEM, FAR* LPTV_ITEM;
在 TV_ITEM 的 hItem中 存放着要展开项的句柄
解决:查了这么多,其实很简单 代码如下:
[cpp] view plaincopyprint?
- void CLeftView::OnItemexpanding(NMHDR* pNMHDR, LRESULT* pResult)
- {
- LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
- // TODO: 在此添加控件通知处理程序代
- HTREEITEM htree=pNMTreeView->itemNew.hItem; // 这个就是 将要被扩展或收缩节点的句柄
- 。。。
- }
2 怎么知道CTreeCtrl的一个节点是展开的还是收缩着的
解决:
方法1
[cpp] view plaincopyprint?
- <strong> (GetItemState(hItem, TVIS_EXPANDED )&TVIS_EXPANDED)!=TVIS_EXPANDED //如果相等,则说明改节点是扩展的,如果不相等,则说明该节点是收缩的</strong>
方法2
响应TVN_ITEMEXPANDING事件时:
[cpp] view plaincopyprint?
- void CExampleDlg::OnItemexpandingTree1(NMHDR* pNMHDR, LRESULT* pResult)
- {
- NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
- if (pNMTreeView->action == TVE_COLLAPSE) //判断action的值
- 。。。
- 。。。
- }
3 判断节点是否被扩展过
[cpp] view plaincopyprint?
- if ((GetTreeCtrl().GetItemState(hItem,TVIS_EXPANDEDONCE )&TVIS_EXPANDEDONCE )!=0 ) //判断是否扩展过一次,若!=0则说明被扩展过
4 使用 CImageList m_ImageList; 加载位图或图标,并将其与树控件联系在一起,由此便可以设置每个节点的图标
[cpp] view plaincopyprint?
- CImageList m_ImageList;
- m_ImageList.Create(12,12,ILC_COLORDDB | ILC_MASK, 3, 1);
- HICON hAdd=::LoadIcon(::AfxGetInstanceHandle(), (LPCTSTR)IDI_ADD);
- HICON hRemove=::LoadIcon(::AfxGetInstanceHandle(), (LPCTSTR)IDI_REMOVE);
- HICON hLeaf=::LoadIcon(::AfxGetInstanceHandle(), (LPCTSTR)IDI_LEAF);
- m_ImageList.Add(hAdd);
- m_ImageList.Add(hRemove);
- m_ImageList.Add(hLeaf);
- GetTreeCtrl().SetImageList(&m_ImageList,TVSIL_NORMAL); // 树控件和图像列表相连
[cpp] view plaincopyprint?
- m_treeCtrl.SetItemImage(htree,0,0) // 通过SetItemImage(htree,0,0) 设置节点的图标
5 什么时候响应OnItemexpanding 消息
当节点第一次被展开时,才响应此消息。也就是说:当以开后该节点再展开或收缩时,便不再响应此消息了。
6 设置树控件形式为 TVS_HASBUTTONS|TVS_LINESATROOT 时, 树控件节点前才会出现+ - 号
以下为综合例子: 点击按钮上一个 显示该节点的上一个兄弟节点,并更改控件焦点
设置控件样式:
[html] view plaincopyprint?
- BOOL CTreePathView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: 在此处通过修改
- // CREATESTRUCT cs 来修改窗口类或样式
- cs.style|=TVS_HASLINES|TVS_SHOWSELALWAYS; //若是想用CImageList的图标 ,则不要设置为TVS_HASBUTTONS形式
- return CTreeView::PreCreateWindow(cs);
- }
点击按钮5(焦点移动到上一个兄弟节点)
[html] view plaincopyprint?
- void NewImageView::OnBnClickedButton5() // 上一个图
- {
- // TODO: 在此添加控件通知处理程序代码
- CTreePathView * pTree=(CTreePathView* )(((CMainFrame *)AfxGetMainWnd())->m_wndSplitter.GetPane(0,0));
- CTreeCtrl & treeCtrl=pTree->GetTreeCtrl();
- HTREEITEM hItem=treeCtrl.GetSelectedItem();
- if (hItem!=NULL)
- {
- hItem=treeCtrl.GetNextItem(hItem,TVGN_PREVIOUS);
- if (hItem!=NULL)
- {
- CString str;
- str=pTree->GetFullPath(hItem);
- SetImage(str);
- treeCtrl.SelectItem(hItem);
- treeCtrl.SetFocus();
- InvalidateRect(m_ClientRect);
- }
- }
- }
点击按钮6(焦点移动到下一个兄弟节点)
[html] view plaincopyprint?
- void NewImageView::OnBnClickedButton6() //下一个
- {
- // TODO: 在此添加控件通知处理程序代码
- CTreePathView * pTree=(CTreePathView* )(((CMainFrame *)AfxGetMainWnd())->m_wndSplitter.GetPane(0,0));
- CTreeCtrl & treeCtrl=pTree->GetTreeCtrl();
- HTREEITEM hItem=treeCtrl.GetSelectedItem();
- if (hItem!=NULL)
- {
- hItem=treeCtrl.GetNextItem(hItem,TVGN_NEXT);
- if (hItem!=NULL)
- {
- CString str;
- str=pTree->GetFullPath(hItem);
- SetImage(str);
- treeCtrl.SelectItem(hItem);
- treeCtrl.SetFocus();
- InvalidateRect(m_ClientRect);
- }
- }
- }
7 遍历树控件的所有节点
1) 获得根节点句柄
[cpp] view plaincopyprint?
- CTreeCtrl& nTreeCtrl=((CImportTreeView*)m_SplitterWnd.GetPane(0,0))->GetTreeCtrl();
- HTREEITEM hItem;
- //获得根目录节点
- hItem = nTreeCtrl.GetRootItem();
- //遍历树控件节点
- TreeVisit(&nTreeCtrl,hItem);
2)遍历所有节点
[cpp] view plaincopyprint?
- void TreeVisit(CTreeCtrl* pCtrl,HTREEITEM hItem)
- {
- if(pCtrl->ItemHasChildren(hItem))
- {
- HTREEITEM hChildItem = pCtrl->GetChildItem(hItem);
- while(hChildItem!=NULL)
- {
- TreeVisit(pCtrl,hChildItem); //递归遍历孩子节点
- hChildItem = pCtrl->GetNextItem(hChildItem, TVGN_NEXT);
- }
- }
- else // 对叶子节点进行操作
- Leaf(pCtrl,hItem);
- }
3)获得某叶子节点的全路径
[cpp] view plaincopyprint?
- CString m_ParentFolder[10];
- CString m_OldParentFolder[10];
[cpp] view plaincopyprint?
- //--------------------将nParent添加到nParentFolder[10]第一位----------------------
- BOOL AddParentFolder(CString nParentFolder[10], CString nParent)
- {
- for(int i=9;i>0;i--)
- nParentFolder[i]=nParentFolder[i-1];
- nParentFolder[0]=nParent;
- return TRUE;
- }
- //---------------------nParentFolder[10]中的有效数据整合(加\)---------------------
- CString AllCString(CString nParentFolder[10])
- {
- CString nAllCString=L"";
- for(int i=0;i<10;i++)
- {
- if(nParentFolder[i]==L"") break;
- nAllCString+=L"\\"+nParentFolder[i];
- }
- return nAllCString;
- }
[cpp] view plaincopyprint?
- void Leaf(CTreeCtrl* pCtrl,HTREEITEM hItem)
- {
- CString itemName=pCtrl->GetItemText(hItem);
- // 叶子节点是jpg文件或tif文件
- if(nSelItemName.Find(L".jpg")!=-1 || nSelItemName.Find(L".tif")!=-1)
- {
- //m_OldParentFolder 记录上一个节点的父节点
- for(int i=0;i<10;i++)
- m_OldParentFolder[i]=m_ParentFolder[i];
- //m_ParentFolder 记录当前节点的父亲节点
- for(int i=0;i<10;i++)
- m_ParentFolder[i]=L"";
- CString imgPath=L"";
- CString parentFolder=itemName;
- //将parentFolder添加到m_ParentFolder[0],其他值依次后移
- AddParentFolder(m_ParentFolder,parentFolder);
- HTREEITEM parentItem=pCtrl->GetParentItem(hItem);
- // m_imgPath 为根节点对应的名字
- while(pCtrl->GetItemText(parentItem).Compare(m_imgPath))
- {
- parentFolder=pCtrl->GetItemText(parentItem);
- AddParentFolder(m_ParentFolder,parentFolder);
- parentItem=pCtrl->GetParentItem(parentItem)
- }
- // 获得叶子节点的全路径
- imgPath.Format(L"%s%s",m_imgPath,AllCString(m_ParentFolder));
- }
- // 对imgPath 所指的文件进行操作
- ShowPic(imgPath);
- }