Discuss CTREECTRL :: Sortchildrencb
In actual development, we often need to sort the nodes of the tree control (CTREECTRL). In fact, most of the sorting work can be implemented with ctreectrl :: sortchildren, but ctreeCtrl :: Sortchildren is sorted by the tree project name, if you want to personalize, you need to use Sortchildrencb.
CTreeCtrl :: Sortchildrencb This function is used to achieve personalized sorting of tree controls, but because of the defects of this function itself, the initiator is difficult to use this function to sort the tree, often fail, I don't know why . Here, I will introduce the usage of sortchildrencb.
First look at the definition of Sortchildrencb:
Bool Sortchildrencb (LPTVSORTCB PSORT);
Typedef struct tagtvsortcb
{
HtreeItem hParent;
PFNTVCompare LPFNCompare;
LParam Lparam;
} Tvsortcb, * lptvsortcb;
Explain the meaning of each parameter:
LPTVSORTCB PSORT This is a structure that includes data that must be performed.
HParent This parameter flag is a tree of a tree, and we are sorted, the child of this item.
LPFNCompare This parameter flag is a very important callback function, which will be described separately.
LPARAM This parameter is a pointer to the tree control to be sorted.
I just said that lpfNCompare is a very important parameter because it is the callback function it logs, will directly affect the result of the sort:
TypeDef Int (Callback * PfntvCompare)
LParam LPARAM1,
LParam Lparam2,
LPARAM LPARMSORT;
This is the definition of sorting callback functions.
If you have used STL, you must know Qsort (), this quick sort function also references a callback function to discriminate the size of the two data items, the callback function of TreeCtrl, just similar to the callback function of QSort. When we think the first item should return a negative number; when we need to reverse two items, returns an integer; when we think two items are equivalent, we return 0. Such definitions, just the same return value with Strcmp, so we can easily write a callback function with SortChildren equivalent:
INT Callback CTesticondlg :: MyCompareProc (LParam LPARAM1,
LParam Lparam2,
Lparam lparamsort)
{
CTreeCtrl * ptree = (ctreeCtrl *) lparamsort;
CString stritem1 = PMYTREECTRL-> GetItemText (htreeItem) lparam1);
CSTRING STRITEM2 = PMYTREECTRL-> GetItemText (HTREEITEM) LPARAM2;
Return strcmp (stritem2, stritem1);
}
Here, I have to focus on two points:
First, have you seen LPARAMSORT? It is the value you just assigned to TvSortcb :: LPARAM.
Second, what is LPARAM1 and LPARAM2? DATA of the two items of the tree, you can specify in setItemData. In the example of just now, we can conclude that it has been implemented similar to htreeitem hleaf = m_tree.insertitem (& TCIItem);
m_tree.setitemdata (HLEAF, (DWORD) HLEAF;
Code. In this code, the HTREEITEM of this item is set to DATA. This is critical because this value is the basis for sorting.
Ok, for introduction
CTREECTRL :: Sortchildren
Introduce here