随笔-60  评论-111  文章-0  trackbacks-0
msdn原文

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. Remarks
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.
   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.
   extern CListCtrl* pmyListCtrl;

   
// Sort the list view items using my callback procedure.
   pmyListCtrl->SortItems(MyCompareProc, (LPARAM) pmyListCtrl);
}


例子中的代码是不能按照预想的工作的, 因为回调函数接收的2个参数是用SetItemData传入的值,而不是Item的Index!
posted on 2007-02-28 22:59 shaker(太子) 阅读(1800) 评论(3)  编辑 收藏 引用 所属分类: C++

评论:
# re: MSDN的Bug! 2007-03-01 09:07 | mzh
“The lParam1 and lParam2 parameters specify the item data for the two items being compared.”  回复  更多评论
  
# re: MSDN的Bug! 2007-03-01 22:49 | shaker
@mzh
你看函数用法虽然是这么说明的,
但是你看例子中的用法是错了的!  回复  更多评论
  
# re: MSDN的Bug! 2007-06-06 08:29 | Mickey Mouse
看来msdn也不是万能的  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理