Re: CListCtrl sort not working
"Tom Serface" <tom.nospam@camaswood.com> ha scritto nel messaggio
news:DC7E5B5D-BD6F-4AE2-B396-D70EC982C617@microsoft.com...
This article should help you:
http://support.microsoft.com/kb/250614
The article pointed by Tom is of course a good start.
However, it has some problems, IMHO.
For example:
The comparing function SortFunc in the aforementioned article uses the
obsolete strcmp function to compare strings.
I don't like this way of comparing strings.
I believe that it fails in Unicode builds, where LPTSTR expands to wchar_t*.
The author of the article should have used _tcscmp for coherence with
Unicode builds.
Moreover, I don't like the fact that the author of the article uses raw
TCHAR* pointers for string. I believe that using a robust class for strings
like CString is a better choice.
I also don't like the fact that in the article a raw C-style vector of
pointers is used to store the custom data for each list item. I do prefer
using STL std::vector as a container, instead of a fixed-size raw C-style
array storing pointers.
Moreover, in the OnItemclickList1 handler associated to HDN_ITEMCLICK, the
author casts to NMLISTVIEW*, but I would instead prefer to cast to
NMHEADER*, as from MSDN documentation for HDN_ITEMCLICK:
HDN_ITEMCLICK Notification
http://msdn2.microsoft.com/en-us/library/bb775286(VS.85).aspx
// Author's code - IMHO, bad:
NMLISTVIEW *pLV = (NMLISTVIEW *) pNMHDR;
// Better code casting to NMHEADER*
LPNMHEADER phdr = reinterpret_cast< LPNMHEADER >( pNMHDR );
I wrote a demo app to show sorting of list controls, using the corrections I
pointed here.
I used Visual C++ 9 (VS2008).
The VC9 solution can be downloaded here, with source code:
http://www.geocities.com/giovanni.dicanio/vc/TestSortListCtrl.zip
Here's a screenshot:
http://www.geocities.com/giovanni.dicanio/vc/TestSortListCtrl.jpg
HTH,
Giovanni