• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
yingtaoqqq
博客园    首页    新随笔    联系   管理    订阅  订阅
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);
posted on 2024-05-19 17:43  yingtaoqqq  阅读(42)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3