分类:
2008-10-13 16:47:12
树控件的应用 -- 求子树节点的集
作者:
本人,在去年已经发表一篇关于使用数据库内记录的内容,来创建树控件的节点的文章--《按数据库记录构建树控件》(),
由于这篇文件里,对于点击树的某一节点后,显示当前节点的子节点的集上,是使用启动数据库的方法,造成了数据处理复杂,而且程序发应速度慢的问题;经过我再三考虑,将程序修改如下:
一、 将ReturnTreeChilds(CString strCurItem);代码修改如下:
//----------------提取当前所选择项的子项文本所组成的字符串------------------------ CString CTreeDataDlg::ReturnTreeChilds(HTREEITEM hP) { CString text=""; //取出当前的Item值 text=m_ctrlTree.GetItemText(hP); //取子项的Item值 HTREEITEM hC; hC=m_ctrlTree.GetChildItem(hP); while(true) { //-----------判断是否有子项,如果有,则递归调用----------- if (m_ctrlTree.GetChildItem(hC)!=NULL) { text+=(","+ReturnTreeChilds(hC));} else {text+=(","+m_ctrlTree.GetItemText(hC));} //----------------判断是否有兄弟项,如果有,则将它的内容添加进去--- if (m_ctrlTree.GetNextSiblingItem(hC)==NULL) break; HTREEITEM hBC; hBC=m_ctrlTree.GetNextSiblingItem(hC); hC=hBC; } return text; }注:
二、 将void CTreeDataDlg::OnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult)代码修改如下:
void CTreeDataDlg::OnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult) { NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR; // TODO: Add your control notification handler code here //-------------------------------------------------------- hTreeCurrent=m_ctrlTree.GetSelectedItem (); hTreeParent=m_ctrlTree.GetParentItem(m_ctrlTree.GetSelectedItem ()); //-------------------树型控件的图标更改--------- m_ctrlTree.SetItemImage(hTreeCurrent,1,true ); //---------------------------------------------- TreeParent=m_ctrlTree.GetItemText (hTreeParent); hTreeCurrent=m_ctrlTree.GetSelectedItem (); //---------------------处理ListTree中的相应显示内容-------------- //-----------------获取当前的Item和子Items----------------------- if (m_ctrlTree.GetChildItem(hTreeCurrent)!=NULL) {//-------------如果,有子项则进行递归操作,返回自己和子项的值--------- m_strEdit=ReturnTreeChilds(hTreeCurrent); } else { m_strEdit=m_ctrlTree.GetItemText(hTreeCurrent); } UpdateData(false);//子项内容显示到Edit控件中 //--------------------------------------------------- *pResult = 0; }这样,就可以实现点击树节点时,程序自动去搜集当前节点与子节点的值集。