《CTreeCtrl树节点排序节点复选》由会员分享,可在线阅读,更多相关《CTreeCtrl树节点排序节点复选(8页珍藏版)》请在金锄头文库上搜索。
1、CTreeCtrl 树节点排序,节点复选效果描述:该功能可使某义节点下的全部子节点按某种序列重新排列SortChildren 是为该结点下面的所有子节点排序,注意只是子节点,不包括子 节点的子节点。SortChildrenCB 是自定义排序方式。现给出大体例子SortChildrenCB 中的 TVSORTCB 需要用到一个回调函数常常是难点。INT CHvrTree:SortItem(HTREEITEM hItem)BOOL bRet;CHvrTree *ptree = this;TVSORTCB sortpage;g_pLogger-Debug( SortItem():Enter,D_PC
2、_LOG_NAME_HVRTREE, _LINE_);/ / 、丿一亍人、小情報 g_pLogger-Info( Sor tit em():、丿一人、丿一 卜,hl tem -%08x,D_PC_LOG_NAME_HVRTREE, _LiNE_,hitem );sortpage.hParent = hitem;sortpage.lpfnCompare = MyCompareProc;sortpage.lParam = (LPARAM)ptree;bRet = SortChildrenCB(&sortpage);if ( bRet )g_pLogger-Debug( Sortitem():Lea
3、ve,D_PC_LOG_NAME_HVRTREE, _LiNE_);return D_PC_SUCCESS;elseg_pLogger-Debug( Sortitem():Leave,D_PC_LOG_NAME_HVRTREE, _LiNE_);return D_PC_FAiLURE;入口参数是两个在要排序的序列中的无序参数INT CALLBACK CHvrTree:MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)CHvrTree* pTreeCtrl = (CHvrTree*) lParamSort;HTREEI
4、TEM hItem1 = (HTREEITEM)lParam1;HTREEITEM hItem2 = (HTREEITEM)lParam2;/付疗料亡固有番号広/二疋、丿一卜、夕彳7取得INT nKind1 = pTreeCtrl-GetItemType(hItem1);INT nKind2 = pTreeCtrl-GetItemType(hItem2);/ 1.01 :力機能修正/ /丿一卜夕彳7取得于工if ( (nKind10)|(nKind2Error( MyCompareProc:目標丿一卜丿一卜 夕彳7情報取得疋吉卞 nType - %d,D_PC_LOG_NAME_HVRTREE
5、, _LINE_, D_PC_FAILURE );return D_PC_ERRCODE_HVRTREE_GETTREE_FAIL;/ 1.01 - END/丿一卜情報取得BOOL bRet;ST_TREE_NODE *pnodelnfol = NULL;/ 丿一卜 1 情報ST_TREE_NODE *pnodeInfo2 = NULL;/ 丿一卜 2 情報bRet = pTreeCtrl-m_mapNode.Lookup(hItem1,(void*&)pnodeInfo1); / 丿一卜1情報取得if(bRet = FALSE)/目標丿一卜情報取得 g_pLogger-Error( MyCo
6、mpareProc:目標丿一卜情報取 得疋吉卞 hit em %08x,D_PC_LOG_NAME_HVRTREE, _LINE_, hItem1 );return D_PC_ERRCODE_HVRTREE_GETTREE_FAIL;bRet = pTreeCtrl-m_mapNode.Lookup(hItem2,(void*&)pnodeInfo2); / 丿一卜 2情報取得if(bRet = FALSE) /目標丿一卜情報取得 g_pLogger-Error( MyCompareProc:目標丿一卜情報取 得疋吉卞 hit em %08x,D_PC_LOG_NAME_HVRTREE, _L
7、INE_, hItem2 ); return D_PC_ERRCODE_HVRTREE_GETTREE_FAiL;INT nRet 二 NULL;/ 戻。値if( nKind1 = nKind2)/丿一卜1七丿一卜2怎同匕丿一卜夕彳場合/丿一卜1七丿一卜2怎力亍丿一丿一卜場合if( nKind1 = D_PC_NODE_CATE_1)CString strItem1,strItem2; /力亍丿一名取得 strItem1 = pTreeCtrlGetItemText(hItem1); strItem2 = pTreeCtrlGetItemText(hItem2);/力亍丿一名比較 nRet =
8、 strcmp(strItem1, strItem2);/ 1.01 :力機能修正else if( nKind1 = D_PC_NODE_CATE_2)CString strItem1,strItem2; /力亍丿一名取得 strItem1 = pTreeCtrlGetItemText(hItem1); strItem2 = pTreeCtrlGetItemText(hItem2);/力亍丿一名比較 nRet = strcmp(strItem1, strItem2);/ 1.01 - END else if( nKind1 = D_PC_NODE_CAM )/丿一卜 1七丿一卜 2怎力丿一卜場
9、合 /力;严番号取得INT nCamId1 = pnodeInfo1-Id;INT nCamId2 = pnodeInfo2-Id;/力;严番号比較 nRet = nCamId1 - nCamId2;else if( nKind1 = D_PC_NODE_HVR )/丿一卜 1七丿一卜 2瓜物件丿一卜場合 ST_HVR_INFO hvrInfo1, hvrInfo2;CString strItem1, strItem2;/ 物件番号取得INT nHvrId1 = pnodeInfo1-Id;INT nHvrId2 = pnodeInfo2-Id;/ 物件情報取得g_hvrUser.GetHvr
10、Info(nHvrId1, &hvrInfo1); g_hvrUser.GetHvrInfo(nHvrId2, &hvrInfo2);/ 1.05 :物件引順機能修正/物件力于名取得strItem1.Format(%s,hvrInfo1.szKana);strItem2.Format(%s,hvrInfo2.szKana);/物件力于名比較nRet = CCheckWord:CompareValidKataName( strItem1, strItem2 );if (nRet = 0)if (nHvrId1 nHvrId2)nRet = 1;else nRet = -1;/ 1.05 - EN
11、Delse/他場合 nRet = NULL;else/丿一卜 1七丿一卜 2怎違丿一卜 夕人乍0場合 nRet = nKind1 - nKind2;return nRet;GetItemType 是获得结点状态即它的图标,主要是利用了 GetItemImage 得到其 图标复选重绘有时可能会用到复选操作,即点击一个结点时该结点和另一个结点同时被选中 (如其父结点)可使用系统消息OnCustomDraw,该消息须手动添加该功能的实质是:先把要选则的所有节点刷上颜色,再到调用时如单击或双击 事件把不需要的颜色去掉virtual void OnCustomDraw(NMHDR *pHdr, LRES
12、ULT *pResult);BEGIN_MESSAGE_MAP(CHvrTree, CTreeCtrl)ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw) /AFX_MSG_MAPEND_MESSAGE_MAP()void CHvrTree:OnCustomDraw(NMHDR *pHdr, LRESULT *pResult)LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)pHdr;INT nState;switch(lplvcd-nmcd.dwDrawStage)case CDDS_PREPAINT:* pRe
13、sult= CDRF_NOTIFYITEMDRAW;break;/厶力讨苗画6前case CDDS_ITEMPREPAINT:nState = lplvcd-nmcd.uItemState;if(nState = (CDIS_FOCUS|CDIS_SELECTED) | (nState =CDIS_SELECTED) )lplvcd-clrTextBk = D_PC_DEV_ADD_BLACK_BRUSH;/ 了 3厶文字色总設定lplvcd-clrText = D_PC_DEV_LIST_RUN_COLOR;* pResult= CDRF_DODEFAULT; break;else* pRe
14、sult= 0; break; default:* pResult = 0; break;判断结点状态可使用GetltemState。如判断结点是否展开UINT nState;nState = GetItemState( hItem,TVIS_EXPANDED );if ( 0 = (nState & TVIS_EXPANDED) ) /丿一卜非展開場合 SetItemImage(hItem,D_PC_NODE_TREEROOT,D_PC_NODE_TREEROOT); else SetItemImage( hItem,D_PC_NODE_TREEROOTCOL,D_PC_NODE_TREEROOTCOL );复选IN