CListCtrl控件SortItems函数使用方法

 CListCtrl m_listCtrl;

int g_listSort= 1;

int g_subItem;

 

响应LVN_COLUMECLICK
void CTransmitterDlg::OnLvnColumnclickHostList(NMHDR *pNMHDR, LRESULT *pResult)
{
     LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
     // TODO: 在此添加控件通知处理程序代码
     NM_LISTVIEW* pNMListView= (NM_LISTVIEW*)pNMHDR;

     int i= m_listCtrl.GetItemCount();
     while(i--){
          m_listCtrl.SetItemData(i, i);
     }

 

     g_listSort*= -1;
     g_subItem= pNMListView->iSubItem;
     m_listCtrl.SortItems(GroupCompareFunc, (DWORD_PTR)&m_listCtrl);

     *pResult = 0;
}

 

//回调函数可在类中声明,但要加static,或声明为全局函数

int CALLBACK GroupCompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort){
     CListCtrl* pListCtrl;
     pListCtrl= (CListCtrl*)lParamSort;

 

     CString tempstr1= pListCtrl->GetItemText((int)lParam1, g_subItem);
     CString tempstr2= pListCtrl->GetItemText((int)lParam2, g_subItem);

     return tempstr1.CompareNoCase(tempstr2)* g_listSort;
}

你可能感兴趣的:(ListView,callback)