?? multicolumnsortlistview.cpp
字號(hào):
if( wc > MAXCOLWIDTH )
{
wc = MAXCOLWIDTH;
}
CListCtrl::SetColumnWidth( iColumn,wc );
SetRedraw(true);
}
/*
Utility function to get rid of all the columns
*/
void CMultiColumnSortListCtrl::DeleteAllColumns()
{
int iNumCols = GetColumnCount();
for ( int i = 0; i < iNumCols; i++ )
{
DeleteColumn(0);
}
}
/*
Utility function to get rid of all items.
*/
void CMultiColumnSortListCtrl::DeleteAllItems()
{
if( GetItemCount() > 0 )
{
CListCtrl::DeleteAllItems(); //Ariel Benary <Ariel.Benary@ness.com>
}
}
/*
Utility function to find the item by item data, make it visible and select.
return the index of the found item or -1 if item not found
*/
int CMultiColumnSortListCtrl::FindShowAndSelect(DWORD dwData, BOOL bSelect /*= TRUE*/)
{
int nCurrentFilePos = -1;
//try to find the item
LVFINDINFO sFindInfo;
sFindInfo.flags = LVFI_PARAM;
sFindInfo.lParam = dwData;
nCurrentFilePos = FindItem(&sFindInfo);
//once found - select and make visible
if( 0 <= nCurrentFilePos && bSelect )
{
EnsureVisible( nCurrentFilePos, FALSE );
SetItemState( nCurrentFilePos, LVIS_SELECTED, LVIS_SELECTED );
}
return nCurrentFilePos;
}
/*
Utility function to find the item by text, make it visible and select.
return the index of the found item or -1 if item not found
*/
int CMultiColumnSortListCtrl::FindShowAndSelect(LPCTSTR szText, BOOL bSelect /*= TRUE*/)
{
int nCurrentFilePos = -1;
//try to find the item
LVFINDINFO sFindInfo;
sFindInfo.flags = LVFI_STRING;
sFindInfo.psz = szText;
nCurrentFilePos = FindItem(&sFindInfo);
//once found - select and make visible
if( 0 <= nCurrentFilePos && bSelect )
{
EnsureVisible( nCurrentFilePos, FALSE );
SetItemState( nCurrentFilePos, LVIS_SELECTED, LVIS_SELECTED );
}
return nCurrentFilePos;
}
/*
Utility function to get the number of columns
written by Zafir Anjum
*/
UINT CMultiColumnSortListCtrl::GetColumnCount()
{
CHeaderCtrl *pHeaderCtrl = (CHeaderCtrl*)/*GetListCtrl().*/GetDlgItem(0);
return pHeaderCtrl->GetItemCount();
}
/*
Just add some extended styles from the new IE4 stuff.
Of course you can either change the code or change your
derived class's OnCreate to call CListView::OnCreate
*/
//DEL int CMultiColumnSortListCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
//DEL {
//DEL if (CListCtrl::OnCreate(lpCreateStruct) == -1)
//DEL return -1;
//DEL // set list control's style to hilight the entire row
//DEL DWORD dwStyle = SendMessage(LVM_GETEXTENDEDLISTVIEWSTYLE);
//DEL dwStyle |= LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_HEADERDRAGDROP;
//DEL SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE, 0, (LPARAM)dwStyle);
//DEL m_ctlHeaderCtrl.SubclassWindow( GetDlgItem(0)->m_hWnd );
//DEL
//DEL return 0;
//DEL }
/*
We are only sorting in report view so far.
*/
//DEL BOOL CMultiColumnSortListCtrl::PreCreateWindow(CREATESTRUCT& cs)
//DEL {
//DEL cs.style |= LVS_REPORT;
//DEL return CListCtrl::PreCreateWindow(cs);
//DEL }
/*
Utility function to tell you if a sort is taking place
*/
const bool CMultiColumnSortListCtrl::IsSorting() const
{
return CMultiColumnSortListCtrl::m_bSorting;
}
/*
Utility function to tell you if the control key is being pressed
*/
const int CMultiColumnSortListCtrl::IsControlPressed() const
{
return (::GetKeyState( VK_CONTROL ) < 0 );
}
/*
Message handler for when a header is clicked.
*/
void CMultiColumnSortListCtrl::OnHeaderClicked(NMHDR* pNMHDR, LRESULT* pResult)
{
HD_NOTIFY *pHDN = (HD_NOTIFY *) pNMHDR;
if( pHDN->iButton == 0 )
{
if( IsControlPressed() )
SortColumn( pHDN->iItem, MULTI_COLUMN_SORT );
else
SortColumn( pHDN->iItem, SINGLE_COLUMN_SORT );
}
*pResult = 0;
}
/*
Message handler for when control is about to be destroyed.
This is where the column widths are saved.
*/
void CMultiColumnSortListCtrl::OnDestroy()
{
SaveColumnSettings();
CListCtrl::OnDestroy();
}
/*
Utility function to tell you if a column is in the combined sorted list.
*/
bool CMultiColumnSortListCtrl::NotInCombinedSortedColumnList(int iItem) const
{
int iNumCombinedSortedColumns = GetNumCombinedSortedColumns();
for( int i = 0; i < iNumCombinedSortedColumns; i++ )
{
if( m_aCombinedSortedColumns[i] == iItem )
return false;
}
return true;
}
/*
Utility function to set the sort state of a column
*/
void CMultiColumnSortListCtrl::SetItemSortState(int iItem, SORT_STATE bSortState)
{
if( bSortState != GetItemSortState( iItem ) )
{
m_lColumnSortStates ^= (1 << iItem);
}
}
/*
Utility function to get the sort state of a column
*/
const SORT_STATE CMultiColumnSortListCtrl::GetItemSortState( int iItem ) const
{
return (SORT_STATE)((m_lColumnSortStates) & ( 1 << iItem ));
}
/*
Utility function to set the default sort state of a column
*/
void CMultiColumnSortListCtrl::SetDefaultItemSortState( int iItem, SORT_STATE bSortState )
{
if( iItem >= 0 && iItem < MAX_COLUMNS )
{
m_aDefaultColumnsSort[iItem] = bSortState;
}
}
/*
Utility function to get the default sort state of a column
*/
const SORT_STATE CMultiColumnSortListCtrl::GetDefaultItemSortState( int iItem ) const
{
SORT_STATE nRes = DESCENDING;
if( iItem >= 0 && iItem < MAX_COLUMNS )
{
nRes = (SORT_STATE)m_aDefaultColumnsSort[iItem];
}
return nRes;
}
/*
Utility function to get you the number of combined sorted columns
*/
const int CMultiColumnSortListCtrl::GetNumCombinedSortedColumns() const
{
for( int i = 0; i < MAX_COLUMNS; i++ )
if( m_aCombinedSortedColumns[i] == -1 )
return i;
return MAX_COLUMNS;
}
/*
Utility function clear some internal arrays
*/
void CMultiColumnSortListCtrl::EmptyArray( int *pArray, int nVal )
{
memset( pArray, -1, MAX_COLUMNS );
}
/*
This function will move a clicked column to the end of the combined
column list. This is useful when you move backwards through column clicks.
Like click columns: 0, 1, 2, 1. The array will hold [0,1,2] after the first 3
clicks, this function will change it to [0,2,1] after the 4th click.
*/
void CMultiColumnSortListCtrl::MoveItemInCombinedSortedListToEnd(int iItem)
{
int iNumCombinedSortedColumns = GetNumCombinedSortedColumns();
int aCombinedSortedColumns[MAX_COLUMNS];
memset( aCombinedSortedColumns, -1, MAX_COLUMNS );
int iItemIndex = FindItemInCombinedSortedList( iItem );
if( iItemIndex != -1 )
{
if( iItemIndex >= 0 ) //Frank Chen <mkchen@iii.org.tw>
{
memcpy( aCombinedSortedColumns, m_aCombinedSortedColumns, iItemIndex * sizeof( int ) );
memcpy( &aCombinedSortedColumns[iItemIndex], &m_aCombinedSortedColumns[iItemIndex + 1], (iNumCombinedSortedColumns - iItemIndex - 1) * sizeof(int) );
}
}
aCombinedSortedColumns[ iNumCombinedSortedColumns - 1 ] = iItem;
memcpy( m_aCombinedSortedColumns, aCombinedSortedColumns, MAX_COLUMNS * sizeof(int) );
for( int i = 0; i < MAX_COLUMNS ; i++ )
{
if( aCombinedSortedColumns[i] == -1 )
break;
}
}
/*
Utility function to find an item in the combined sorted list.
*/
int CMultiColumnSortListCtrl::FindItemInCombinedSortedList( int iItem )
{
int iNumCombinedSortedColumns = GetNumCombinedSortedColumns();
for( int i = 0; i < iNumCombinedSortedColumns; i++ )
{
if(m_aCombinedSortedColumns[i] == iItem )
return i;
}
return -1;
}
/*
Utility function to look up a columns width in the registry.
If it returns -1 then it means that it was not found in the registry
*/
const int CMultiColumnSortListCtrl::GetRegColumnWidth( int iColumn ) const
{
int nRes = -1;
ASSERT( m_strUniqueName.GetLength() ); //You must set a unique name for every listctrl
CString strEntry( m_strUniqueName );
CString strValue, strSubString;
CString strSection( m_strColumnWidthSection );
strValue = AfxGetApp()->GetProfileString( strSection, strEntry, "" );
AfxExtractSubString(strSubString, strValue, iColumn, ',');
if( !strSubString.IsEmpty() )
{
nRes = atoi( (LPCTSTR)strSubString );
}
return nRes;
}
/*
Utility function to Autosize all columns in the case there is no registry entry.
*/
void CMultiColumnSortListCtrl::AutoSizeAllColumns()
{
int iNumCols = GetColumnCount();
for( int i = 0; i < iNumCols; i++ )
{
AutoSizeColumn( i );
}
}
/*
Utility function to set the width on the column based on the registry
value and a set minimum column width.
*/
void CMultiColumnSortListCtrl::SetColumnWidth( int nCol )
{
int iWidth = GetRegColumnWidth( nCol );
if( iWidth > -1 )
{
if( iWidth < MINCOLWIDTH )
{
AutoSizeColumn( nCol );
}
else
{
CListCtrl::SetColumnWidth( nCol, iWidth );
}
}
}
/*
Set the column sort type
*/
void CMultiColumnSortListCtrl::SetColumnType( int iCol, SORT_TYPE nType )
{
if( iCol >= 0 && iCol < MAX_COLUMNS )
{
m_aColumnType[iCol] = nType;
}
}
/*
Get the column sort type
*/
const SORT_TYPE CMultiColumnSortListCtrl::GetColumnType( int iCol ) const
{
return (iCol >= 0 && iCol < MAX_COLUMNS) ? m_aColumnType[iCol] : TYPE_TEXT;
}
/*
Set the unique name for the registry operations
*/
void CMultiColumnSortListCtrl::SetUniqueName(LPCTSTR lpszUniqueName)
{
m_strUniqueName = lpszUniqueName;
}
void CMultiColumnSortListCtrl::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
m_ctlHeaderCtrl.SubclassWindow( GetDlgItem(0)->m_hWnd );
CListCtrl::PreSubclassWindow();
}
/*
Modified version of InsertColumn to accomodate for the sort type and sort state
*/
int CMultiColumnSortListCtrl::InsertColumn( int nCol, SORT_TYPE nType, SORT_STATE nDefaultSort, const LVCOLUMN* pColumn )
{
int nRes = CListCtrl::InsertColumn( nCol, pColumn );
SetColumnType(nRes, nType);
SetDefaultItemSortState(nRes, nDefaultSort);
return nRes;
}
/*
Modified version of InsertColumn to accomodate for the sort type and sort state
*/
int CMultiColumnSortListCtrl::InsertColumn( int nCol, SORT_TYPE nType, SORT_STATE nDefaultSort, LPCTSTR lpszColumnHeading, int nFormat /*= LVCFMT_LEFT*/, int nWidth /*= -1*/, int nSubItem /*= -1*/ )
{
int nRes = CListCtrl::InsertColumn( nCol, lpszColumnHeading, nFormat, nWidth, nSubItem );
SetColumnType(nRes, nType);
SetDefaultItemSortState(nRes, nDefaultSort);
return nRes;
}
//hidden from the world
int CMultiColumnSortListCtrl::InsertColumn( int nCol, const LVCOLUMN* pColumn )
{
return CListCtrl::InsertColumn( nCol, pColumn );
}
//hidden from the world
int CMultiColumnSortListCtrl::InsertColumn( int nCol, LPCTSTR lpszColumnHeading, int nFormat /*= LVCFMT_LEFT*/, int nWidth /*= -1*/, int nSubItem /*= -1*/ )
{
return CListCtrl::InsertColumn( nCol, lpszColumnHeading, nFormat, nWidth, nSubItem);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -