http://blog.sina.com.cn/s/blog_4b44e1c00100d493.html
下面这段排序内容讲的尤其简单实用,对我的项目很有帮助,谢谢原作者!
23. listctrl排序
Q250614:How To Sort Items in a CListCtrl in Report View
http://support.microsoft.com/kb/250614/en-us
// 排序用的比较函数
static int CALLBACK MyCompareProc(LPARAMlParam1, LPARAMlParam2, LPARAMlParamSort)
{
CString &lp1 = *((CString *)lParam1);
CString &lp2 = *((CString *)lParam2);
int &sort = *(int *)lParamSort;
if (sort == 0)
{
return lp1.CompareNoCase(lp2);
}
else
{
return lp2.CompareNoCase(lp1);
}
}
//要处理的事件
ON_NOTIFY(LVN_COLUMNCLICK, IDC_LIST1, &CDlg::OnLvnColumnclickList1)
//处理代码
void CDlg::OnLvnColumnclickList1(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMLISTVIEWpNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
int Length = m_ListCtrl.GetItemCount();
CArray<CString,CString> ItemData;
ItemData.SetSize(Length);
for (int i = 0; i < Length; i++)
{
ItemData = m_ListCtrl.GetItemText(i,pNMLV->iSubItem);
m_ListCtrl.SetItemData(i,(DWORD_PTR)&ItemData);//设置排序关键字
}
static int sort = 0;
static int SubItem = 0;
if (SubItem != pNMLV->iSubItem)
{
sort = 0;
SubItem = pNMLV->iSubItem;
}
else
{
if (sort == 0)
{
sort = 1;
}
else
{
sort = 0;
}
}
m_ListCtrl.SortItems(MyCompareProc,(DWORD_PTR)&sort);//排序
*pResult = 0;
}
文章评论(0条评论)
登录后参与讨论