1.MFC创建树状导览 Tree Control
CTreeCtrl* pTree = (CTreeCtrl*)GetDlgItem(IDC_TREE1); // 设置图片列表 HICON hIcon[3]; hIcon[0] = theApp.LoadIcon(IDI_ICON_GREEN_GREEN); hIcon[1] = theApp.LoadIcon(IDI_ICON_GREEN_GREY); hIcon[2] = theApp.LoadIcon(IDI_ICON_RED_GREEN); m_imageList.Create(16, 16, ILC_COLOR32, 3, 3); for(int i=0; i<3; i++) m_imageList.Add(hIcon[i]); m_cTree.SetImageList(&m_imageList, TVSIL_NORMAL);//添加图标到treectrl //pTree->SetImageList(&m_imageList, TVSIL_NORMAL); // 创建待插入的TV_INSERTSTRUCT结构 TV_INSERTSTRUCT tvinsert; tvinsert.hParent = NULL; // 无父结点 tvinsert.hInsertAfter = TVI_LAST; // 插入到本层最后 tvinsert.item.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT; // 掩码包括 图标 选中图标 文字 tvinsert.item.hItem = NULL; // 句柄为空 tvinsert.item.state = 0; // 状态 tvinsert.item.stateMask = 0; // 状态掩码,不使用这两项 tvinsert.item.cchTextMax = 6; // 最大文字长度,忽略 tvinsert.item.iSelectedImage = 1; // 选中图标索引 tvinsert.item.cChildren = 0; // 没有子节点 tvinsert.item.lParam = 0; // 自定义数据 // 第一层 //tvinsert.item.iImage = 2; // 插入第一层第一个节点"father" tvinsert.item.pszText = L"father"; HTREEITEM hDad = pTree->InsertItem(&tvinsert); // 插入第一层第二个节点"mother" tvinsert.item.pszText = L"mother"; HTREEITEM hMom = pTree->InsertItem(&tvinsert); // 创建第二层 tvinsert.hParent = hDad; // 父节点为"father" //tvinsert.item.iImage = 3; // 插入第二层"father"的第一个节点"son" tvinsert.item.pszText = L"son"; pTree->InsertItem(&tvinsert); // 插入第二层"father"的第二个节点"daughter" tvinsert.item.pszText = L"daughter"; pTree->InsertItem(&tvinsert); // 还是创建第二层 tvinsert.hParent = hMom; // 父结点为"mother" //tvinsert.item.iImage = 4; // 插入第二层"mother"的第一个节点"son" tvinsert.item.pszText = L"son"; pTree->InsertItem(&tvinsert); // 插入第二层"mother"的第二个节点"daughter" tvinsert.item.pszText = L"daughter"; pTree->InsertItem(&tvinsert); // 插入第二层"mother"的第三个节点"cartoon" tvinsert.item.pszText = L"cartoon"; HTREEITEM hOther = pTree->InsertItem(&tvinsert); // 创建第三层 tvinsert.hParent = hOther; // 父结点为"cartoon" //tvinsert.item.iImage = 7; // 插入第三层"cartoon"的第一个节点"Tom" tvinsert.item.pszText = L"Tom"; pTree->InsertItem(&tvinsert); // 插入第三层"cartoon"的第二个节点"Jerry" tvinsert.item.pszText = L"Jerry"; pTree->InsertItem(&tvinsert);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)