CListCtrl::SortItems This method sorts list view items using an application-defined comparison function. The index of each item changes to reflect the new sequence.BOOL SortItems( PFNLVCOMPARE pfnCompare, DWORD dwData ); Parameters pfnCompare Specifies the address of the application-defined comparison function. The comparison function is called during the sort operation each time the relative order of two list items needs to be compared. The comparison function must be either a static member of a class or a standalone function that is not a member of any class. dwData Specifies the application-defined value that is passed to the comparison function. Return Value Nonzero if it is successful; otherwise, it is zero.Res The comparison function has the following form:int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);The comparison function must return a negative value if the first item should precede the second, a positive value if the first item should follow the second, or zero if the two items are equivalent.The lParam1 and lParam2 parameters specify the item data for the two items being compared. The lParamSort parameter is the same as the dwData value.Example // Sort the item in reverse alphabetical order. static int CALLBACK MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort) { // lParamSort contains a pointer to the list view control. // The lParam of an item is just its index. CListCtrl* pListCtrl = (CListCtrl*) lParamSort; CString strItem1 = pListCtrl->GetItemText(lParam1, 0); CString strItem2 = pListCtrl->GetItemText(lParam2, 0); return strcmp(strItem2, strItem1); }void snip_CListCtrl_SortItems() { // The pointer to my list view control. CListCtrl* pmyListCtrl; // Sort the list view items using my callback procedure. pmyListCtrl->SortItems(MyCompareProc, (LPARAM) pmyListCtrl); }Requirements Windows CE versions: 1.0 and later Header file: Declared in Afxcmn.h Platform: H/PC Pro, Palm-size PC, Pocket PCSee Also CListCtrl::FindItemBuilt on Friday, January 12, 2001
但是上面的函数 MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort) { // lParamSort contains a pointer to the list view control. // The lParam of an item is just its index. CListCtrl* pListCtrl = (CListCtrl*) lParamSort; CString strItem1 = pListCtrl->GetItemText(lParam1, 0); CString strItem2 = pListCtrl->GetItemText(lParam2, 0); return strcmp(strItem2, strItem1); } 在运行时lParam1和lParam2出现是一样的情况,不能正确排序,为什么?
to canjian(奋斗,探求,不达目的誓不罢休!) : 不能正确排序是因为在添加item的时候没有SetitemData~ 通常情况下这么处理就ok了 for(int i;i<m_list.GetItemCount();i++) m_list.SetitemData(i,i);
This method sorts list view items using an application-defined comparison function. The index of each item changes to reflect the new sequence.BOOL SortItems(
PFNLVCOMPARE pfnCompare,
DWORD dwData );
Parameters
pfnCompare
Specifies the address of the application-defined comparison function. The comparison function is called during the sort operation each time the relative order of two list items needs to be compared. The comparison function must be either a static member of a class or a standalone function that is not a member of any class.
dwData
Specifies the application-defined value that is passed to the comparison function.
Return Value
Nonzero if it is successful; otherwise, it is zero.Res
The comparison function has the following form:int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2,
LPARAM lParamSort);The comparison function must return a negative value if the first item should precede the second, a positive value if the first item should follow the second, or zero if the two items are equivalent.The lParam1 and lParam2 parameters specify the item data for the two items being compared. The lParamSort parameter is the same as the dwData value.Example
// Sort the item in reverse alphabetical order.
static int CALLBACK
MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
// lParamSort contains a pointer to the list view control.
// The lParam of an item is just its index.
CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
CString strItem1 = pListCtrl->GetItemText(lParam1, 0);
CString strItem2 = pListCtrl->GetItemText(lParam2, 0); return strcmp(strItem2, strItem1);
}void snip_CListCtrl_SortItems()
{
// The pointer to my list view control.
CListCtrl* pmyListCtrl; // Sort the list view items using my callback procedure.
pmyListCtrl->SortItems(MyCompareProc, (LPARAM) pmyListCtrl);
}Requirements
Windows CE versions: 1.0 and later
Header file: Declared in Afxcmn.h
Platform: H/PC Pro, Palm-size PC, Pocket PCSee Also
CListCtrl::FindItemBuilt on Friday, January 12, 2001
可是不明白strItem1,strItem2这两个CString变量在这里有什么用?
CALLBACK函数的话,上面三个参数程序都可以不用关心了,对吧?
GetItemText的第一个参数是行,类型int,LPARAM不能直接转换吧?
谢谢lxrlxr20021(艾文)的回复 ^_^
MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
// lParamSort contains a pointer to the list view control.
// The lParam of an item is just its index.
CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
CString strItem1 = pListCtrl->GetItemText(lParam1, 0);
CString strItem2 = pListCtrl->GetItemText(lParam2, 0); return strcmp(strItem2, strItem1);
}
在运行时lParam1和lParam2出现是一样的情况,不能正确排序,为什么?
不能正确排序是因为在添加item的时候没有SetitemData~
通常情况下这么处理就ok了
for(int i;i<m_list.GetItemCount();i++)
m_list.SetitemData(i,i);