CListCtrl排序问题

caoweiguo 2005-08-25 11:15:22
各位:
根据上一次的回帖,我将程序做了如下改动:
void CDIALOG_LAYER_DISSECT::OnColumnclickList1(NMHDR* pNMHDR, LRESULT* pResult)
{

NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
m_List_Control_Layer_Display.SortItems(CompareFunc,(LPARAM)&m_List_Control_Layer_Display);
*pResult = 0;
}

int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
LPARAM i,j;
i = lParam1;
j = lParam2;
CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
CString strItem1 = pListCtrl->GetItemText(lParam1, 0);
CString strItem2 = pListCtrl->GetItemText(lParam2, 0);

return strcmp(strItem2, strItem1); //按升序排序
}
问题又出现了:每一排序时,lParam1,lParam2中的参数都是0,0,为什么?如何改?恳请指导!
...全文
201 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
bager 2005-09-06
  • 打赏
  • 举报
回复
你可能使用简单插入函数来增加节点,这将致使与排序相关的节点信息值“lParam”无意义,因此回调函数得到的参数不正确。

修正方法:
列表结点添加完毕后,调用CListCtrl的SetItemData方法设置节点信息lParam的值。

int count = GetListCtrl().GetItemCount();
for(int i=0; i<count; i++)
{
GetListCtrl().SetItemData(i,i);
}

//以下摘自MSDN
//SetItemData函数说明
SetItemData sets the 32-bit application-specific value associated with the item specified by nItem. This value is the lParam member of the LVITEM structure;
//LVITEM成员lParam的说明
Specifies the 32-bit value of the item. If you use the LVM_SORTITEMS message, the list view control passes this value to the application-defined comparison function.
菜牛 2005-09-03
  • 打赏
  • 举报
回复
lParam1,lParam2指的是ItemData,看着办吧。
i_noname 2005-09-03
  • 打赏
  • 举报
回复
void CDIALOG_LAYER_DISSECT::OnColumnclickList1(NMHDR* pNMHDR, LRESULT* pResult)
{

NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
for (int i = 0; i < Num; i++) //Num为你的Item数
m_List_Control_Layer_Display.SetItemData(i,i);
m_List_Control_Layer_Display.SortItems(CompareFunc,(LPARAM)&m_List_Control_Layer_Display);
*pResult = 0;
}
mynamelj 2005-09-02
  • 打赏
  • 举报
回复
只能反回-1 or 1

你试试这样:
if(strcmp(strItem2, strItem1) > 1)
return -1;
else
return 1;
idAnts 2005-09-02
  • 打赏
  • 举报
回复
每次SortItems之前按item的index分别SetItemData,从0开始……
zhangshengyu 2005-09-02
  • 打赏
  • 举报
回复
SetItemData

15,980

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧