CListCtrl排序

.h
static int CALLBACK MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);
static int iColumn;
static bool m_iSortColumn;
CListCtrl *m_ListResFile;
.cpp
int CMyFileListView::iColumn =0;
bool CMyFileListView::m_iSortColumn = false;
bool IsNumber( LPCTSTR pszText )
{
int Num = lstrlen( pszText );
if(Num == 0)
{
return false;
}
for( int i = 0; i < Num; i++ )
if( !_istdigit( pszText[ i ] ))
{
// if (i == Num-1)
// {
// return true;
// }
return false;
}
return true;
}
int NumberCompare( LPCTSTR pszNumber1, LPCTSTR pszNumber2 )
{
const int iNumber1 = _wtoi( pszNumber1 );
const int iNumber2 = _wtoi( pszNumber2 );


if( iNumber1 < iNumber2 )
return -1;


if( iNumber1 > iNumber2 )
return 1;


return 0;
}
int CALLBACK CMyFileListView::MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort) 
{
CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
CString strItem1 = pListCtrl->GetItemText(lParam1, iColumn);
CString strItem2 = pListCtrl->GetItemText(lParam2, iColumn);
int n1 = 0;
if( IsNumber( strItem1 ) )
{
return m_iSortColumn ? NumberCompare( strItem1, strItem2 ) : NumberCompare( strItem2, strItem1 );
}
return m_iSortColumn ? StrCmp( strItem1, strItem2 ) : StrCmp( strItem2, strItem1 );
}
//点击表头
void CMyFileListView::OnLvnColumnclick(NMHDR *pNMHDR, LRESULT *pResult)//LVN_COLUMNCLICK 消息
{
LPNMLISTVIEW pNMLV = reinterpret_cast(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
iColumn = pNMLV->iSubItem;
for(int i = 0; i < m_ListResFile->GetItemCount(); ++i)
{
m_ListResFile->SetItemData(i,i);
}
m_ListResFile->SortItems( MyCompareProc, (DWORD)m_ListResFile);
m_iSortColumn = !m_iSortColumn;


*pResult = 0;
}

你可能感兴趣的:(学习)