记得以前就试过给list控件排序,但是失败了,今天下午试了一个也失败看,晚上抱着试试看的想法又搜了一个,呵呵,竟然成功了,用了2个函数而已,比用类的简单。
先在.cpp文件的上面定义几个全局变量
BOOL m_bAsc1;//是否顺序排序
int m_nSortedCol1;//当前排序的列
static int CALLBACK SortLVProc1(LPARAM lParam1,LPARAM lParam2,LPARAM lParamSort);排序函数
static int CALLBACK SortLVProc1(LPARAM lParam1,LPARAM lParam2,LPARAM lParamSort)
{
CListCtrl *pList=(CListCtrl*)lParamSort;
int nItem1,nItem2;
LVFINDINFO FindInfo;
FindInfo.flags=LVFI_PARAM;
FindInfo.lParam=lParam1;
nItem1=pList->FindItem(&FindInfo,-1);
FindInfo.lParam=lParam2;
nItem2=pList->FindItem(&FindInfo,-1);
if((nItem1==-1)||(nItem2==-1))
{
return 0;
}
CString str1,str2;
str1=pList->GetItemText(nItem1,m_nSortedCol1);
str2=pList->GetItemText(nItem2,m_nSortedCol1);
HDITEM headerItem;
headerItem.mask = HDI_LPARAM;
CHeaderCtrl* pHeaderCtrl = pList->GetHeaderCtrl( );
pHeaderCtrl->GetItem( m_nSortedCol1, &headerItem );
UINT nType = (UINT)(headerItem.lParam);
int iCompRes = 0;
switch( nType )
{
case INT_TYPE:
{
int i1,i2;
i1 = atoi( str1 );
i2 = atoi( str2 );
if( i1 > i2)
iCompRes = 1;
else if( i1 == i2 )
iCompRes = 0;
else
iCompRes = -1;
}
break;
case DOUBLE_TYPE:
{
double i1,i2;
i1 = atof( str1 );
i2 = atof( str2 );
if( i1 > i2)
iCompRes = 1;
else if( i1 == i2 )
iCompRes = 0;
else
iCompRes = -1;
}
break;
case STRING_TYPE:
default:
{
if( str1 > str2)
iCompRes = 1;
else if( str1 == str2 )
iCompRes = 0;
else
iCompRes = -1;
}
break;
}
if(m_bAsc1)
return iCompRes;
else
return iCompRes*-1;
return 0;
}
这个排序函数的有点在于他对要排序的变量包括的很全,有int ,cstring,double型的,这是在.h文件的函数体外定义的:enum { INT_TYPE = 0, STRING_TYPE, DOUBLE_TYPE };
最后加一个单击表头的消息响应函数 ON_NOTIFY(LVN_COLUMNCLICK, IDC_LIST1, OnLvnColumnclickList1)
void CGAMEFAST::OnLvnColumnclickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
LPNMLISTVIEW pNMListView = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
if(pNMListView->iSubItem==m_nSortedCol1)
m_bAsc1=!m_bAsc1;
else
{
m_bAsc1=true;
m_nSortedCol1=pNMListView->iSubItem;
}
int i=m_MyList.GetItemCount(); // 这两句函数是关键哦。。。。
while(i--)m_MyList.SetItemData(i,i);
m_MyList.SortItems(SortLVProc1,(DWORD)&m_MyList);
*pResult = 0;
}
把控件变量名改成你的控件变量就可以了!上面标着关键的那两句就是关键
先在.cpp文件的上面定义几个全局变量
BOOL m_bAsc1;//是否顺序排序
int m_nSortedCol1;//当前排序的列
static int CALLBACK SortLVProc1(LPARAM lParam1,LPARAM lParam2,LPARAM lParamSort);排序函数
static int CALLBACK SortLVProc1(LPARAM lParam1,LPARAM lParam2,LPARAM lParamSort)
{
CListCtrl *pList=(CListCtrl*)lParamSort;
int nItem1,nItem2;
LVFINDINFO FindInfo;
FindInfo.flags=LVFI_PARAM;
FindInfo.lParam=lParam1;
nItem1=pList->FindItem(&FindInfo,-1);
FindInfo.lParam=lParam2;
nItem2=pList->FindItem(&FindInfo,-1);
if((nItem1==-1)||(nItem2==-1))
{
return 0;
}
CString str1,str2;
str1=pList->GetItemText(nItem1,m_nSortedCol1);
str2=pList->GetItemText(nItem2,m_nSortedCol1);
HDITEM headerItem;
headerItem.mask = HDI_LPARAM;
CHeaderCtrl* pHeaderCtrl = pList->GetHeaderCtrl( );
pHeaderCtrl->GetItem( m_nSortedCol1, &headerItem );
UINT nType = (UINT)(headerItem.lParam);
int iCompRes = 0;
switch( nType )
{
case INT_TYPE:
{
int i1,i2;
i1 = atoi( str1 );
i2 = atoi( str2 );
if( i1 > i2)
iCompRes = 1;
else if( i1 == i2 )
iCompRes = 0;
else
iCompRes = -1;
}
break;
case DOUBLE_TYPE:
{
double i1,i2;
i1 = atof( str1 );
i2 = atof( str2 );
if( i1 > i2)
iCompRes = 1;
else if( i1 == i2 )
iCompRes = 0;
else
iCompRes = -1;
}
break;
case STRING_TYPE:
default:
{
if( str1 > str2)
iCompRes = 1;
else if( str1 == str2 )
iCompRes = 0;
else
iCompRes = -1;
}
break;
}
if(m_bAsc1)
return iCompRes;
else
return iCompRes*-1;
return 0;
}
这个排序函数的有点在于他对要排序的变量包括的很全,有int ,cstring,double型的,这是在.h文件的函数体外定义的:enum { INT_TYPE = 0, STRING_TYPE, DOUBLE_TYPE };
最后加一个单击表头的消息响应函数 ON_NOTIFY(LVN_COLUMNCLICK, IDC_LIST1, OnLvnColumnclickList1)
void CGAMEFAST::OnLvnColumnclickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
LPNMLISTVIEW pNMListView = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
if(pNMListView->iSubItem==m_nSortedCol1)
m_bAsc1=!m_bAsc1;
else
{
m_bAsc1=true;
m_nSortedCol1=pNMListView->iSubItem;
}
int i=m_MyList.GetItemCount(); // 这两句函数是关键哦。。。。
while(i--)m_MyList.SetItemData(i,i);
m_MyList.SortItems(SortLVProc1,(DWORD)&m_MyList);
*pResult = 0;
}
把控件变量名改成你的控件变量就可以了!上面标着关键的那两句就是关键