CTreeCtrl::InsertItem给树形视图控件添加一个项目

1:CTreeCtrl::InsertItem给树形视图控件添加一个项目

项目由HTREEITEM句柄来标识,并输入给InsertItem的参数之一是父项目的HTREEITEM句柄。创建子项目就是将一个项目添加到树形视图中并指定一个项目作为父亲。根项目(数的最顶层项目)是通过指定TVI_ROOT作为父亲而创建的。

//Root items first,with automatic sorting .

HTREEITEM hEagles=GetTreeCtrl().InsertItem(_T("Eagles"),TVI_ROOT,TVI_SORT);

HTREEITEM hDoobies=GetTreeCtrl().InsertItem(_T("Doobie Brothers"),TVI_ROOT,TVI_SORT);

//Eagles subitems second(no sorting )

GetTreeCtrl().InsertItem(_T("Eagles"),hEagles);

GetTreeCtrl().InsertItem(_T("On the Border"),hEagles);

GetTreeCtrl().InsertItem(_T("Hotel California"),hEagles);

GetTreeCtrl(),InsertItem(_T("The Long Run"),hEagles);

//Doobie subitems third(no sorting )

GetTreeCtrl().InsertItem(_T("Thoulouse Street"),hDoobies);

GetTreeCtrl().InsertItem(_T("The Captain and me"),hDoobies);

GetTreeCtrl().InsertItem(_T("Stampede"),hDoobies);

给InsertItem传递一个TVI_SORT标记可以自动的根据同一子树中的其他项目将添加到树中的项目排列。

2:在树形视图项目中包含文本和图形。假定创建了包含两个图形的图形列表。图形0描绘的吉他,图形1描绘的唱片封面。我们希望吉他和摇滚演唱组的名字一起出现,而唱片图形和唱片标题一同出现。

//Add the image list to the control.

GetTreeCtrl().SetImageList(pImageList,TVSIL_NORMAL);

//Root items first,with automatic sorting .

HTREEITEM hEagles=GetTreeCtrl().InsertItem(_T("Eagles"),0,0,TVI_ROOT,TVI_SORT);

HTREEITEM hDoobies=GetTreeCtrl().InsertItem(_T("Doobie Brothers"),0,0,TVI_ROOT,TVI_SORT);

//Eagles subitems second(no sorting )

GetTreeCtrl().InsertItem(_T("Eagles"),1,1,hEagles);

GetTreeCtrl().InsertItem(_T("On the Border"),1,1,hEagles);

GetTreeCtrl().InsertItem(_T("Hotel California"),1,1,hEagles);

GetTreeCtrl(),InsertItem(_T("The Long Run"),1,1,hEagles);

//Doobie subitems third(no sorting )

GetTreeCtrl().InsertItem(_T("Thoulouse Street"),1,1,hDoobies);

GetTreeCtrl().InsertItem(_T("The Captain and me"),1,1,hDoobies);

GetTreeCtrl().InsertItem(_T("Stampede"),1,1,hDoobies);

注:微笑第2和第3个参数传递的是图形索引号。第一个索引号指定了在项目未选中时树形视图将显示的图形,第二个指定了项目被选中后显示的图形。给定两个参数指定相同的索引号说明将用相同的图形表示项目的两种状态。

3:CTreeCtrl的成员函数:

DeleteItem用来在控件中删除项目,DeleteAllItems用来删除锁有的项目。

Expand用来展开或折叠一个子树。

SetItemText用来修改一个项目的标签。

GetItemText用来索引标签。

SortChildren排列子树中的项目。

最重要的是HTREEITEM句柄,它标示了操作的目标项目。可以将InsertItem返回的句柄保存在数组,链表或其他结构,可以使用CTreeCtrl::GetSelectedItem来检索所选项目的句柄。

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值