列表控件的列排序

来源:互联网 发布:长泽雅美 知乎 编辑:程序博客网 时间:2024/05/18 20:13

BOOL CListCtrl::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
 // TODO: Add your specialized code here and/or call the base class
 if(bHScroll == FALSE)
 { switch (((NMHDR *)lParam )->code)
  {
   case HDN_BEGINTRACKW:
   case HDN_BEGINTRACKA:
   case (HDN_DIVIDERDBLCLICKA): // double click column to resize
   case (HDN_DIVIDERDBLCLICKW):

    *pResult=TRUE;
    return TRUE;
  }
 }
 return CListCtrl::OnNotify(wParam, lParam, pResult);
}

void CListCtrl::OnColumnclick(NMHDR* pNMHDR, LRESULT* pResult)
{

 NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
 // TODO: Add your control notification handler code here
 if( pNMListView->iSubItem == g_iSorted)
  g_fAsc = !g_fAsc;
 else
 {
  g_fAsc = TRUE;
  g_iSorted = pNMListView->iSubItem;
 }
// CString str;
// str.Format("%d",g_iSorted);
// MessageBox(str);

 for(int i=0;i<GetItemCount();i++)
  SetItemData(i,i);

 VERIFY( SortItems( CompareFunc, reinterpret_cast<DWORD>( this ) ) );

 *pResult = 0;
}

int CALLBACK CListCtrl::CompareFunc( LPARAM lParam1, LPARAM lParam2, LPARAM lParamData )
{
 CListCtrl * pSort = (CListCtrl *)lParamData;

 BOOL bAsc=((CListCtrl*)pSort)->g_fAsc;
 int nSorted=((CListCtrl*)pSort)->g_iSorted;

 int nRet;
 CString strData1 = pSort->GetItemText(lParam1, nSorted);
 CString strData2 = pSort->GetItemText(lParam2, nSorted);

 TCHAR szText[256];
 LVCOLUMN col;
 col.pszText=szText;
 col.cchTextMax = 256;
 col.mask = LVCF_TEXT;
// col.iSubItem=nSorted;
 pSort ->GetColumn(nSorted,&col);

// ::MessageBox(pSort->m_hWnd,strData1,strData2,MB_OK);
 long fValue1 = atol(strData1);
 long  fValue2 = atol(strData2);

 if (lstrcmp(col.pszText,"号码") == 0)
 {
// ::MessageBox(pSort->m_hWnd,col.pszText,strData2,MB_OK);

  if (fValue1 == fValue2)
   nRet = 0;
  if (fValue1 > fValue2)
  {
   nRet = 1;
  }
  else
  {
   nRet = -1;
  }
 }
 else
  nRet=lstrcmp(strData1,strData2);

 if ( bAsc)
 {
  return nRet;
 }
 else
 {
  return nRet * -1;
 }
 
}

 

原创粉丝点击