怎么对CListCtrl控件按列进行排序?
有一个CListCtrl控件m_List,它有三列,分别是“学号”、“名字”、“成绩”,现在我想根据某列(比如名字或者成绩)进行升序(降序)排列,要怎么做?请各位大侠不吝...
有一个CListCtrl控件m_List,它有三列,分别是“学号”、“名字”、“成绩”,现在我想根据某列(比如名字或者成绩)进行升序(降序)排列,要怎么做?请各位大侠不吝指教!在明天中午前解决追加30分!
展开
展开全部
void CN7SeeView::OnColumnclick(NMHDR* pNMHDR, LRESULT* pResult)
{
int i,nItemCounter;
NM_LISTVIEW * pNMListView = (NM_LISTVIEW*)pNMHDR;
CListCtrl * pListCtrl=&(this->GetListCtrl());
if(!m_bSortable) return;
nColToSort=pNMListView->iSubItem;
if(nLastColToSort!=nColToSort)
{
bDesc=0;
nLastColToSort=nColToSort;
}
else
bDesc=bDesc?0:1;
nItemCounter=pListCtrl->GetItemCount();
for(i=0;i<nItemCounter;i++)
pListCtrl->SetItemData(i,i);
pListCtrl->SortItems(SortLVProc,(LPARAM)pListCtrl);
*pResult = 0;
}
static int CALLBACK SortLVProc(LPARAM lParam1,LPARAM lParam2,LPARAM lParamSort)
{
// lParamSort contains a pointer to the list view control.
CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
CString strItem1 = pListCtrl->GetItemText(lParam1,nColToSort);
CString strItem2 = pListCtrl->GetItemText(lParam2,nColToSort);
if(bDesc)
return strcmp(strItem1,strItem2);
else
return strcmp(strItem2,strItem1);
}
{
int i,nItemCounter;
NM_LISTVIEW * pNMListView = (NM_LISTVIEW*)pNMHDR;
CListCtrl * pListCtrl=&(this->GetListCtrl());
if(!m_bSortable) return;
nColToSort=pNMListView->iSubItem;
if(nLastColToSort!=nColToSort)
{
bDesc=0;
nLastColToSort=nColToSort;
}
else
bDesc=bDesc?0:1;
nItemCounter=pListCtrl->GetItemCount();
for(i=0;i<nItemCounter;i++)
pListCtrl->SetItemData(i,i);
pListCtrl->SortItems(SortLVProc,(LPARAM)pListCtrl);
*pResult = 0;
}
static int CALLBACK SortLVProc(LPARAM lParam1,LPARAM lParam2,LPARAM lParamSort)
{
// lParamSort contains a pointer to the list view control.
CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
CString strItem1 = pListCtrl->GetItemText(lParam1,nColToSort);
CString strItem2 = pListCtrl->GetItemText(lParam2,nColToSort);
if(bDesc)
return strcmp(strItem1,strItem2);
else
return strcmp(strItem2,strItem1);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询