Clistctrl: sortitems usage:
(1) Where is sortitems used?
The prototype of clistctrl: sortitems is:
Bool sortitems (pfnlvcompare pfncompare, DWORD dwdata );
Where
1) The first parameter pfncompare is the callback function in the form:
Int callback comparefunc (lparam lparam1, lparam lparam2,
Lparam lparamsort );
What is lparam1 and lparam2? This is why sortitems is hard to understand. In (2.
Lparamsort is actually the number of columns, equal to 2) in dwdata.
2) The second parameter, dwdata, is the user-defined value.
Dwdata is actually passed in the number of columns, equal to 1) in lparamsort.
The following is where sortitems is used:
// Lvn_columnclick message Response Function
Void cuplistctrl: oncolumnclick (nmhdr * pnmhdr, lresult * presult)
...{
Nm_listview * pnmlistview = (nm_listview *) pnmhdr;
// Sort
// Comparefunc is the callback function.
// Pnmlistview-> isubitem is the number of Columns
Sortitems (pfnlvcompare) comparefunc, pnmlistview-> isubitem );
* Presult = 0;
}
(2) What is lparam1 and lparam2 In the callback function of sortitems?
Put simply: lv_item: lparam.
Lv_item is a structure, see msdn.
It can be seen that when we insert an item to clistctrl, we must use
Int insertitem (const lvitem * pitem );
Insertitem has several forms. Only in this form can lv_item: lparam be used.
The following is an example of the Code for inserting an item:
Taginfo * pfileinfo = new taginfo; // taginfo is a structure that stores the information you need to sort.
Pfileinfo-> strfilename = strfilename;
Pfileinfo-> strfilesize = formatfilesize (FileFind. getlength ());
Pfileinfo-> strfiletype = gettypename (lpszfilename );
// Pfileinfo-> strfilepath = strpath;
Int nitem = getitemcount ();
Int nicon = geticonindex (lpszfilename, FileFind. isdirectory (), false );
Lv_item LVI;
LVI. Mask = lvif_text | lvif_param | lvif_image;
LVI. iItem = nitem;
LVI. isubitem = 0;
LVI. psztext = (lptstr) (lpctstr) pfileinfo-> strfilename;
LVI. lparam = (lparam) pfileinfo;
LVI. iimage = nicon;
If (nitem = insertitem (& LVI ))! =-1) // Insert the file name (column 0th) and display the corresponding icon
...{
// Messageint (nitem );
LVI. Mask = lvif_text;
LVI. iItem = nitem;
// Set the 1st column (that is, set the file size)
LVI. isubitem = 1;
If (! FileFind. isdirectory () // if it is not a directory
...{
LVI. psztext = (lptstr) (lpctstr) pfileinfo-> strfilesize;
Setitem (& LVI );
}
Else // if it is a directory
...{
LVI. psztext = (lptstr) yct_unknow_size;
Setitem (& LVI );
}
// Set the 2nd column (that is, set the file type)
LVI. isubitem = 2;
LVI. psztext = (lptstr) (lpctstr) pfileinfo-> strfiletype;
Setitem (& LVI );
// Set the 3rd column (that is, set the directory of the file)
LVI. isubitem = 3;
LVI. psztext = (lptstr) (lpctstr) strpath;
Setitem (& LVI );
// Update the interface
// Update (LVI. iItem );
}
Now lparam points to the new space. Delete is required. The following is the deletion code:
// Lvn_deleteitem message Response Function
// Every time an item is deleted, the system automatically calls this function
Void cuplistctrl: ondeleteitem (nmhdr * pnmhdr, lresult * presult)
...{
Nm_listview * pnmlistview = (nm_listview *) pnmhdr;
// Delete the space pointed to by lv_item: lparam
Lv_item LVI;
LVI. Mask = lvif_param;
LVI. iItem = pnmlistview-> iItem;
LVI. isubitem = 0;
If (getitem (& LVI ))
...{
Cuplistctrl: taginfo * pinfo = (cuplistctrl: taginfo *) (LVI. lparam );
Delete pinfo;
}
* Presult = 0;
}
(3) Write callback Functions
This is simple, and you can sort by your rules.
The callback function is a static member function.
// Callback function
Int callback cuplistctrl: comparefunc (cuplistctrl: taginfo * pinfo1, cuplistctrl: taginfo * pinfo2, lparam LCOL)
...{
// Clistctrl: callback function used by sortitems
// [In] pinfo1, pinfo2: the input is lv_item: lparam, which is specified when insertitem (const lvitem * pitem) is called.
// [In] LCOL: specifies the number of columns (starting from scratch), that is, the columns sorted
Int nret = 0;
Assert (LCOL> = 0 & LCOL <NumCol-1 );
Assert (pinfo1! = NULL );
Assert (pinfo2! = NULL );
Switch (LCOL)
...{
Case 0: // o Column
// Sort the code by zero-column rules
// If you want pinfo1 (the item) to be placed before pinfo2 (the item), nret is smaller than 0; otherwise, it is greater than 0; the order is not changed to 0.
Break;
Case 1: // column 1
// Sort the code by one column of rules
//...
Break;
}
Return nret;
}