CListCtrl::SortItems
原型:
BOOL
SortItems(
PFNLVCOMPARE
pfnCompare,
DWORD_PTR
dwData
);
|
int CALLBACK CompareFunc(
LPARAM lParam1, LPARAM lParam2,
LPARAM lParamSort);
|
LVITEM lv_item;
lv_item.mask = LVIF_IMAGE|LVIF_TEXT| LVIF_PARAM;
//restore the text string into lparam for compare
lv_item.lParam = (LPARAM)lpszText;
…
Int iIndex = CListCtrl::InsertItem(&lv_item); //第一列
//BOOL bRnt = CListCtrl::SetItem(&lv_item); //1,2,3,…列
|
//-----------override the InsertItem method to add below codes block--------
int nIndex = CListCtrl::InsertItem(nItem, lpszText);
LPTSTR* lpszArr = new LPTSTR[nNumColumn];
//array for storing all subitem text
//as the first column text
lpszArr[0]
= new TCHAR[ lstrlen( lpszText ) + 1 ];
//set the first subItem text to the first elemant of array
(void)lstrcpy( lpszArr[0], lpszText );
//set the first subItem text to the first elemant of array
CListCtrl::SetItemData(nItem, (DWORD)lpszArr);
|
//-----------override
“
SetItem
”
method to add below codes block-----
BOOL
bRnt = CListCtrl::SetItem(nItem, nSubItem, LVIF_TEXT, lpszText, 0, 0, 0, 0);
//Get the pointer which stored all cells
’
text
LPTSTR* lpszArr
=
reinterpret_cast
<
LPTSTR
*>( CListCtrl::GetItemData( nItem ) );
//set the sepcify nSubItem item
’
s text into array
lpszArr[nSubItem]
= new TCHAR[ lstrlen( lpszText ) + 1 ];
(void)lstrcpy( lpszArr[nSubItem], lpszText );
|
struct
LISTITEMINFO
{
LISTITEMINFO()
{
lpszTextArr = NULL;
dwData =
NULL;
}
LPTSTR* lpszTextArr; //store all subitem
’
s text of a row
DWORD dwData; //store the lParam
};
|
LISTITEMINFO*
pid = new LISTITEMINFO;
pid->
lpszTextArr = lpszArr;
//instead of CListCtrl::SetItemData(nItem, (DWORD)lpszArr);
CListCtrl::SetItemData( nItem, (DWORD)pid );
|
DWORD
CListCtrlEx::GetItemData(int nItem) const
{
ASSERT( nItem < GetItemCount() );
LISTITEMINFO* pid = reinterpret_cast<LISTITEMINFO*>( CListCtrl::GetItemData( nItem ) );
ASSERT( pid );
return pid->dwData;
}
|
|
int
CALLBACK CListCtrlEx::CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
CListCtrlEx* pListCtrl = reinterpret_cast<CListCtrlEx*>( lParamSort);
ASSERT( pListCtrl->IsKindOf( RUNTIME_CLASS( CListCtrl ) ) );
LISTITEMINFO* pid1 = reinterpret_cast<LISTITEMINFO*>( lParam1 );
LISTITEMINFO* pid2 = reinterpret_cast<LISTITEMINFO*>( lParam2 );
ASSERT( pid1 );
ASSERT( pid2 );
LPCTSTR pszText1 = pid1->lpszTextArr[ pListCtrl->m_iCurSortCol ];
LPCTSTR pszText2 = pid2->lpszTextArr[ pListCtrl->m_iCurSortCol ];
//add your compare implement according different data type such as //DateTime/Number…
|
void CHeaderCtrlEx :: DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
//draw arrow up/down here
}
|
HDITEM
hdrItem;
GetHeaderCtrl()->
GetItem(m_iSortCol, & hdrItem);
hdrItem.
mask = HDI_FORMAT | HDI_IMAGE;
hdrItem.
iImage = nImageIndex; //index of up/down bitmap
pHeaderCtrl->
SetItem(m_iSortCol, & hdrItem);
|
Switch(columnType)
{
case DateTimeCol
:
…
break;
case NumberCol:
…
break;
|