http://www.cppblog.com/zhouhuishine/archive/2007/07/17/28210.html
1. 排序函数。
排序主要依靠的是SortItems(CompareProc , (LPARAM)this)函数,因此需要一个排序的静态函数CompareProc 作为参数,在CompareProc 函数中,获得ItemText的操作不可以使用简单的GetItemText(lParam1,szItemText)函数,因为在排序操作中lParam1不是静态的Index(由于排序过程中Index在不断变化)。所以需要在每个Item中先使用SetItemData保存Index,这样的Index才是静态的。在排序时,这样操作:
1
int
CListInfo::CompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
2 {
3 // lParamSort contains a pointer to the list view control.
4 // The lParam of an item is just its index.
5 CString strFir , strSec;
6 int cmp = 0;
7 CListInfo* pListInfo = (CListInfo*) lParamSort;
8
9 LVFINDINFO info;
10 int nIndex;
11 info.flags = LVFI_PARAM; //set LVFI_PARAM to info
12 info.lParam = lParam1; //get the item text item data(lParam1)
13 if ( (nIndex=pListInfo->FindItem(&info)) != -1)
14 strFir = pListInfo->GetItemText(nIndex, pListInfo->m_nSortCol);
15 info.lParam = lParam2; //get the item text item data(lParam2)
16 if ( (nIndex=pListInfo->FindItem(&info)) != -1)
17 strSec = pListInfo->GetItemText(nIndex, pListInfo->m_nSortCol);
18
19 cmp = strFir.CompareNoCase(strSec);
20
21 return pListInfo->m_bAscending ? cmp : -cmp;
22}
这里使用了FindItem来获得Item的静态Index。
2 {
3 // lParamSort contains a pointer to the list view control.
4 // The lParam of an item is just its index.
5 CString strFir , strSec;
6 int cmp = 0;
7 CListInfo* pListInfo = (CListInfo*) lParamSort;
8
9 LVFINDINFO info;
10 int nIndex;
11 info.flags = LVFI_PARAM; //set LVFI_PARAM to info
12 info.lParam = lParam1; //get the item text item data(lParam1)
13 if ( (nIndex=pListInfo->FindItem(&info)) != -1)
14 strFir = pListInfo->GetItemText(nIndex, pListInfo->m_nSortCol);
15 info.lParam = lParam2; //get the item text item data(lParam2)
16 if ( (nIndex=pListInfo->FindItem(&info)) != -1)
17 strSec = pListInfo->GetItemText(nIndex, pListInfo->m_nSortCol);
18
19 cmp = strFir.CompareNoCase(strSec);
20
21 return pListInfo->m_bAscending ? cmp : -cmp;
22}
2. 使用表示上下的小图标来表示排序顺序。
首先需要给表头栏添加小图标:
CImageList m_ImageList;
m_ImageList.Create(IDB_SORT , 16 , 1 , RGB(255 , 255 , 255));
GetHeaderCtrl()->SetImageList(&m_ImageList);
点击了表头栏后,需要按照排序顺序来选择贴图:
1
HDITEM hdi;
2 CHeaderCtrl * pHdrCtrl = GetHeaderCtrl();
3 int nCount = pHdrCtrl -> GetItemCount();
4
5 for ( int nItem = 0 ; nItem < nCount; nItem ++ )
6 {
7 pHdrCtrl->GetItem(nItem, &hdi);
8
9 if (nItem == m_nSortCol)
10 {
11 hdi.mask= HDI_IMAGE | HDI_FORMAT;
12
13 hdi.iImage = m_bAscending ? 0 : 1;
14 hdi.fmt = HDF_LEFT | HDF_STRING |HDF_IMAGE | HDF_BITMAP_ON_RIGHT;
15 }
16 else
17 {
18 hdi.mask= HDI_FORMAT;
19
20 hdi.fmt = HDF_LEFT | HDF_STRING;
21 }
22
23 pHdrCtrl->SetItem(nItem, &hdi);
24}
通过HDITEM的iImage参数即可决定选择第几个图片(图片的顺序按照m_ImageList.Create(IDB_SORT , 16 , 1 , RGB(255 , 255 , 255));
2 CHeaderCtrl * pHdrCtrl = GetHeaderCtrl();
3 int nCount = pHdrCtrl -> GetItemCount();
4
5 for ( int nItem = 0 ; nItem < nCount; nItem ++ )
6 {
7 pHdrCtrl->GetItem(nItem, &hdi);
8
9 if (nItem == m_nSortCol)
10 {
11 hdi.mask= HDI_IMAGE | HDI_FORMAT;
12
13 hdi.iImage = m_bAscending ? 0 : 1;
14 hdi.fmt = HDF_LEFT | HDF_STRING |HDF_IMAGE | HDF_BITMAP_ON_RIGHT;
15 }
16 else
17 {
18 hdi.mask= HDI_FORMAT;
19
20 hdi.fmt = HDF_LEFT | HDF_STRING;
21 }
22
23 pHdrCtrl->SetItem(nItem, &hdi);
24}
中的16像素点来决定)。