MFC树形结构

dsmin21 2018-02-01 02:54:07
需要做一个树形结构,整个树的架构存在sqlite3数据库中,表的结构如下所示,每一行都是一个分支,中间留空说明设备是上一级的直属设备

我需要从表中拿出 数据,然后生成一个树形结构,我想每次生成一个分支,过程类似于
hRoot = m_webTree.InsertItem(_T("鸡啄米"), 0, 0);

hCataItem = m_webTree.InsertItem(_T("IT互联网"), hRoot, TVI_LAST);

hArtItem = m_webTree.InsertItem(_T("百度文章1"), hCataItem, TVI_LAST);

hArtItem = m_webTree.InsertItem(_T("谷歌文章2"), hCataItem, TVI_LAST);
但是这样做每读取一行数据进行InsertItem的时候,都会把相同节点重复添加,zhegey
上面只是我的一个思路,大家有别的更好的方法也希望多交流交流(*^__^*)
...全文
453 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
dsmin21 2018-02-02
  • 打赏
  • 举报
回复
引用 2 楼 zgl7903 的回复:
个人思路大概是, 掌握好数结构 应该不难 |- S1省 |- X1县 |-G1队 |- S1 设备 |- S2 设备 |-G2 队 |- S2省 |- X2县 |-G3队 |- S21 设备 |- S22 设备 |-GN 队
我也是这么想的,这样用InsertItem添加的时候会方便很多,然而脑子不好使,想不出来怎么把表里那种结构做成这样的
dsmin21 2018-02-02
  • 打赏
  • 举报
回复
引用 1 楼 xieming2014 的回复:
我的思路是,先把整个表结构的内容都获取出来,用map存放,结构大概是这样 以省,市,县来说: typedef list<CString> listcounty; typedef map<CString, listcounty> mapcity; map<CString,mapcity> mapProv 这样存放好,到时候,你插入树控件时候,遍历一下,不就完事了吗
对了,还有就是如果用我问题里的那样,该怎么去避免把相同节点重复添加的这个问题
dsmin21 2018-02-02
  • 打赏
  • 举报
回复
引用 1 楼 xieming2014 的回复:
我的思路是,先把整个表结构的内容都获取出来,用map存放,结构大概是这样 以省,市,县来说: typedef list<CString> listcounty; typedef map<CString, listcounty> mapcity; map<CString,mapcity> mapProv 这样存放好,到时候,你插入树控件时候,遍历一下,不就完事了吗
不会用map
dsmin21 2018-02-02
  • 打赏
  • 举报
回复
引用 10 楼 schlafenhamster 的回复:
两行的X省 那就是根据 item name 找 hItem 如下面例子是根据 右边 listview 选中的 项 找 左边 tree 的 项 ,然后 展开。

void CHMI_manView::ExpandLeftTree(char * dir)
{
// get LeftTree
	CMainFrame* pMain=(CMainFrame*)AfxGetMainWnd();// m_pMainWnd;
	CLeftView * pLeft=pMain->GetLeftPane();
	CTreeCtrl&  LeftTree=pLeft->GetTreeCtrl();
// get selected tree item
	HTREEITEM hParentItem,hChildItem;
	TVITEM tvi;
	char tviname[MAX_PATH];
	hParentItem=LeftTree.GetSelectedItem();
// if parent not expanded
	if(TVIS_EXPANDED!=LeftTree.GetItemState(hParentItem,TVIF_STATE))
	{ // expand
		LeftTree.Expand(hParentItem,TVE_EXPAND);
	} 
// find which one 
	hChildItem=LeftTree.GetChildItem(hParentItem);
	tvi.mask=TVIF_TEXT|TVIF_HANDLE;  
    while (hChildItem)
	{
		tvi.hItem=hChildItem;
		strcpy(tviname,LeftTree.GetItemText(tvi.hItem));
//AfxMessageBox(tviname);
		if (stricmp(tviname,dir)==0)// found
		{ // expand
			LeftTree.Expand(tvi.hItem,TVE_EXPAND);
// change selection
			LeftTree.SelectItem(tvi.hItem);
// ensure visible
			LeftTree.EnsureVisible(tvi.hItem);
		}
// get new child
		hChildItem=LeftTree.GetNextSiblingItem(tvi.hItem);
	} 
}
谢谢,我先拿下来研究下
schlafenhamster 2018-02-02
  • 打赏
  • 举报
回复
两行的X省 那就是根据 item name 找 hItem 如下面例子是根据 右边 listview 选中的 项 找 左边 tree 的 项 ,然后 展开。

void CHMI_manView::ExpandLeftTree(char * dir)
{
// get LeftTree
	CMainFrame* pMain=(CMainFrame*)AfxGetMainWnd();// m_pMainWnd;
	CLeftView * pLeft=pMain->GetLeftPane();
	CTreeCtrl&  LeftTree=pLeft->GetTreeCtrl();
// get selected tree item
	HTREEITEM hParentItem,hChildItem;
	TVITEM tvi;
	char tviname[MAX_PATH];
	hParentItem=LeftTree.GetSelectedItem();
// if parent not expanded
	if(TVIS_EXPANDED!=LeftTree.GetItemState(hParentItem,TVIF_STATE))
	{ // expand
		LeftTree.Expand(hParentItem,TVE_EXPAND);
	} 
// find which one 
	hChildItem=LeftTree.GetChildItem(hParentItem);
	tvi.mask=TVIF_TEXT|TVIF_HANDLE;  
    while (hChildItem)
	{
		tvi.hItem=hChildItem;
		strcpy(tviname,LeftTree.GetItemText(tvi.hItem));
//AfxMessageBox(tviname);
		if (stricmp(tviname,dir)==0)// found
		{ // expand
			LeftTree.Expand(tvi.hItem,TVE_EXPAND);
// change selection
			LeftTree.SelectItem(tvi.hItem);
// ensure visible
			LeftTree.EnsureVisible(tvi.hItem);
		}
// get new child
		hChildItem=LeftTree.GetNextSiblingItem(tvi.hItem);
	} 
}
dsmin21 2018-02-02
  • 打赏
  • 举报
回复
引用 8 楼 schlafenhamster 的回复:
Sorry 看错了,你的 表 就是 一行 一支,插入时 先插入 父 item
hFather = m_webTree.InsertItem
然后在 hFather 中 插入 子 item
一行完 后
再 插入 新的 父 项。


每一行是一支没错,但是我需要把相同的比如两行的X省,并成同一个父项,而不是另起一个父项,就像下面这样
schlafenhamster 2018-02-02
  • 打赏
  • 举报
回复
Sorry 看错了,你的 表 就是 一行 一支,插入时 先插入 父 item hFather = m_webTree.InsertItem 然后在 hFather 中 插入 子 item 一行完 后 再 插入 新的 父 项。
schlafenhamster 2018-02-02
  • 打赏
  • 举报
回复
你把 表 的 行列 交换 一下 就 明白了,即 一列 是 tree 的 一支!
smwhotjay 2018-02-01
  • 打赏
  • 举报
回复
理清思路,数据归类
zgl7903 2018-02-01
  • 打赏
  • 举报
回复
个人思路大概是, 掌握好数结构 应该不难 |- S1省 |- X1县 |-G1队 |- S1 设备 |- S2 设备 |-G2 队 |- S2省 |- X2县 |-G3队 |- S21 设备 |- S22 设备 |-GN 队
跟随我 2018-02-01
  • 打赏
  • 举报
回复
我的思路是,先把整个表结构的内容都获取出来,用map存放,结构大概是这样 以省,市,县来说: typedef list<CString> listcounty; typedef map<CString, listcounty> mapcity; map<CString,mapcity> mapProv 这样存放好,到时候,你插入树控件时候,遍历一下,不就完事了吗

16,470

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧