treectrl的个性化排序

本文介绍了CTreeCtrl的三种排序方式:通过InsertItem指定TVI_SORT实现按字母顺序插入;使用SortChildren对父节点所有子节点按字母顺序排序;利用SortChildrenCB结合自定义比较函数实现灵活排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


CTreeCtrl有几种方式对其子项进行排序
  • InsertItem allows to insert the child item alphabetically, when specifying TVI_SORT for hInsertAfter可以实现对插入节点按照字母顺序进行排序,前提是在参数hInsertAfter中指定为TVI_SORT.
  • SortChildren performs an alphabetical sorting of the child items of the given parent item in a tree可以实现对对一个父节点的所有子节点以字母顺序进行排序。
  • SortChildrenCB performs a sort with a user-defined callback (hence the CB suffix) of the children of the specified item。这种方式,则可以由自己指定规则,按照自己的规则进行排序。



// Sort the item in reverse alphabetical order.
int CALLBACK MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
   // lParamSort contains a pointer to the tree control.
   // The lParam of an item is just its handle, 
   // as specified with SetItemData
   CTreeCtrl* pmyTreeCtrl = (CTreeCtrl*)lParamSort;
   CString strItem1 = pmyTreeCtrl->GetItemText((HTREEITEM)lParam1);
   CString strItem2 = pmyTreeCtrl->GetItemText((HTREEITEM)lParam2);

   return strItem2.Compare(strItem1);
}
TVSORTCB tvs;

// Sort the tree control's items using my
// callback procedure.
tvs.hParent = TVI_ROOT;
tvs.lpfnCompare = MyCompareProc;
tvs.lParam = (LPARAM)&m_TreeCtrl;

m_TreeCtrl.SortChildrenCB(&tvs);



特别注意回调函数
<span style="font-size:18px;"><span style="font-family:Microsoft YaHei;"><span class="code-keyword" style="margin: 0px; padding: 0px; border: 0px currentColor; border-image: none; color: blue;">int</span> CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);</span></span>
的lparam1和lparam2都是树节点的getitemdata获取的DWORD值,可以将有用的值与该DWORD值绑定,进而根据自己想要排序根据进行排序
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值