在树展控件上选择,在ListBox控件里显示。对树展的某个结点的子结点进行增加和删除

界面:

  • Tree Control被点击时的响应函数:
void CDlgManageChannels::OnTvnSelchangedTree1(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码

	//1.得到当前选中的项
	HTREEITEM hSelected = m_treGroupStatus.GetSelectedItem();
	//2.得到所所选项的文字
	CString str1;
//	str1 = m_treGroupStatus.GetItemText(hSelected);
//str1的值的设定放到下面的判断里


	//将选中组(或选中项所在组)的所有观测站显示在当前组list box里

	//首先需要将listbox_all里上次显示的内容清空
	m_lstbxIncludedStations.ResetContent();

	HTREEITEM hChildItem = NULL;//当前选中组的第一个子结点
	bool bIsDir = false;
	if (m_treGroupStatus.ItemHasChildren(hSelected))//有子结点,把当前结点当作父结点
	{
		 hChildItem = m_treGroupStatus.GetChildItem(hSelected);//得到当前组的第一个结点
		 m_hCurrentSelectedGroup = m_treGroupStatus.GetSelectedItem();//保存组名
		str1 = m_treGroupStatus.GetItemText(hSelected);
	}
	else//无子结点,得到该结点的父结点,再重新遍历
	{
		//判断所有的兄弟结点是否有子结点
		HTREEITEM hBrother = m_treGroupStatus.GetParentItem(hSelected);//这里得到父结点
		hBrother = m_treGroupStatus.GetChildItem(hBrother);//再得到子结点,所以用hBrother来命名
		while (hBrother != NULL)
		{
			if (m_treGroupStatus.ItemHasChildren(hBrother))
			{
				bIsDir = true;
				break;
			}
			hBrother = m_treGroupStatus.GetNextItem(hBrother,TVGN_NEXT);
		}

		if (bIsDir)//是组名一级的结点
		{
			hChildItem = m_treGroupStatus.GetChildItem(hSelected);
			m_hCurrentSelectedGroup = m_treGroupStatus.GetSelectedItem();//保存组名
			str1 = m_treGroupStatus.GetItemText(hSelected);
		}
		else//若其所有兄弟结点也无子结点,才说明是站点,而不是分组
		{
			HTREEITEM hParent = m_treGroupStatus.GetParentItem(hSelected);
			m_hCurrentSelectedGroup = m_treGroupStatus.GetParentItem(hSelected);//保存组名
			str1 = m_treGroupStatus.GetItemText(hParent);
			hChildItem = m_treGroupStatus.GetChildItem(hParent);
		}
	
	}

	//将组名显示到编辑框里
	SetDlgItemText(IDC_EDIT_CurSelected,str1);

	//确定了一组的第一个子结点后,将这一组的站点添加到listbox里
	while (hChildItem != NULL)
	{
		str1 = m_treGroupStatus.GetItemText(hChildItem);
		m_lstbxIncludedStations.AddString(str1);
		hChildItem = m_treGroupStatus.GetNextItem(hChildItem,TVGN_NEXT);
	}
	*pResult = 0;
}
  • 往一个分组里增加站点:
void CDlgManageChannels::OnBnClickedAdditem()
{
	// TODO: 在此添加控件通知处理程序代码
	//得到listbox_allstations里被选中的站点及名字
	CString str1;
	if (m_lstbxAllStations.GetCurSel() != LB_ERR )//判断是否有选中
	{
		m_lstbxAllStations.GetText(m_lstbxAllStations.GetCurSel(),str1);//得到item的值

		//加到右边listbox_included里
		m_lstbxIncludedStations.AddString(str1);

		//得到当前被选中的组
		CString str2;//存储临时item(站)的内容
		CString str3;//当前选中的 组 的名字
		GetDlgItemText(IDC_EDIT_CurSelected,str3);
		HTREEITEM hRoot = m_treGroupStatus.GetRootItem();	
		while(hRoot != NULL)
		{
			str2 = m_treGroupStatus.GetItemText(hRoot);
			if (str3 == str2)//得到当前选中的组
			{
				break;
			}
			hRoot =m_treGroupStatus.GetNextItem(hRoot,TVGN_NEXT);//在组名一级进行遍历
		}
		m_treGroupStatus.InsertItem(str1,0,1,hRoot);//向组中加入item
	}
	else
	{
		MessageBox(_T("请选择一个站点!"));
	}	
}
  • 送去一个分组里的站点:
void CDlgManageChannels::OnBnClickedDelitem()
{
	// TODO: 在此添加控件通知处理程序代码
	CString str1;//当前选中的item的值
	CString str2;//查找到的item的值


	//从treeCtrl里的对应组内移除item
	if (m_lstbxIncludedStations.GetCurSel() != LB_ERR )//判断是否有选中
	{
		m_lstbxIncludedStations.GetText(m_lstbxIncludedStations.GetCurSel(),str1);//得到item的值
		m_lstbxIncludedStations.DeleteString(m_lstbxIncludedStations.GetCurSel());	//从listbox_included里移除item

		//////////////////////////////////////////////////////////////////////////
		HTREEITEM hChild;
		if (m_hCurrentSelectedGroup != NULL)
		{
			hChild = m_treGroupStatus.GetChildItem(m_hCurrentSelectedGroup);//得到当前组的第一项
			while(hChild !=  NULL)//比较当前组的各子项
			{
				str2 = m_treGroupStatus.GetItemText(hChild);
				if (str1 == str2)//找到删除
				{
					m_treGroupStatus.DeleteItem(hChild);
					str2 = _T("成功删除: ") + str2;
					MessageBox(str2);
					break;
				}
				hChild = m_treGroupStatus.GetNextItem(hChild,TVGN_NEXT);
			}
		}
	}
	else
	{
		MessageBox(_T("请选择一个站点!"));
	}
}

  

posted on 2011-09-01 17:49  LateStop  阅读(705)  评论(0)    收藏  举报

导航