?? multisortlistctrl.cpp
字號:
/*
Usage:
You generally should not use this class directly, though it
is possible. You need to do two things to use it directly.
Set m_strUniqueName to someting, and set m_strColumnWidthSection
to where you want the column widths to be saved.
The purpose of m_strUniqueName is to allow for saving of
multiple instances of listview objects. So obviously you would
need to set this differently for each instance. SetUniqueName must be called
before calling InsertColumn() or LoadColumnWidths().
If you are deriving from this class, you need to do the following:
Add a class to your project derived from CListCtrl, then go into the
header file and include MultiColumnSortListView.h and change all
references to CListCtrl to CMultiSortListCtrl. Then in the .cpp
file of your class, change all the message maps to CMultiSortListCtrl
instead of CListCtrl. That should do it.
Compiling:
One problem you will have is finding IDB_ARROWUP and IDB_ARROWDOWN.
Those bitmaps will be included with this set of classes, You should
add them to your project or add your own bitmaps named correctly.
These are the bitmaps that show the sort order on the header control.
I hope this is simple enough, kind of a pain to get started but after
that it should be cake and hopefully it will be useful.
Things to be aware of:
Multithreading:
If you delete all the items from another thread
in the middle of a sort, it will crash. This is the only
bug i have found.
Column Widths:
MINCOLWIDTH - Minimum width a column can be.
MAXCOLWIDTH - Maximum width a column can be.
These are hard coded in the header file. Be aware.
MAXCOLUMNS - The most columns you can have right
now is 64, that is only because im use __int64 to
hold the column sort states. Who needs more than
64 columns anyway? If you do, you can change it to
CUIntArray, i just didnt see the need for a whole int
when all i needed was a bit.
Credits:
Iuri Apollonio -- Sorting Class ( great class )
Zafir Anjum -- CMultiSortListCtrl::GetColumnCount
Roger Onslow -- CMultiSortListCtrl::AutoSizeColumns
Zafir Anjum -- CSortableHeaderCtrl::SetSortImage
Me -- The Rest, I think.
*/
// MultiColumnSortListView.cpp : implementation file
//
#include "stdafx.h"
#include "MultiSortListCtrl.h"
#include "SortClass.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMultiSortListCtrl
IMPLEMENT_DYNCREATE(CMultiSortListCtrl, CListCtrl)
/*
When deriving from this class you must set m_strUniqueName to something
this name is used to save each instances column widths to the registry
*/
CMultiSortListCtrl::CMultiSortListCtrl()
{
m_strUniqueName.Empty();
m_strColumnWidthSection = "";
m_bSorting = false;
m_lColumnSortStates = 0;
m_bCreate = false;
}
CMultiSortListCtrl::~CMultiSortListCtrl()
{
}
BEGIN_MESSAGE_MAP(CMultiSortListCtrl, CListCtrl)
//{{AFX_MSG_MAP(CMultiSortListCtrl)
ON_WM_CREATE()
ON_WM_DESTROY()
//}}AFX_MSG_MAP
ON_NOTIFY(HDN_ITEMCLICKA, 0, OnHeaderClicked)
ON_NOTIFY(HDN_ITEMCLICKW, 0, OnHeaderClicked)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMultiSortListCtrl drawing
void CMultiSortListCtrl::OnDraw(CDC* pDC)
{
//CDocument* pDoc = GetDocument();
}
/////////////////////////////////////////////////////////////////////////////
// CMultiSortListCtrl diagnostics
#ifdef _DEBUG
void CMultiSortListCtrl::AssertValid() const
{
CListCtrl::AssertValid();
}
void CMultiSortListCtrl::Dump(CDumpContext& dc) const
{
CListCtrl::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMultiSortListCtrl message handlers
/*
This function saves the columns widths of the listctrl to the registry.
This is called in two places, OnDestroy, and OnEndTrack in the headerCtrl class.
*/
/*
This function loads all the column widths for each column that was saved and applies the width
to the column. This function should be called when you are done inserting data. Or you
can call SetColumnWidth to set the column width right after you InsertColumn(), If you call
my InsertColumn it will do this for you.
*/
/*
This function is the heart of the class. This will get called automatically
when you click a header, and if you press control while clicking the header,
a multi column sort will take place (ie: sorting the current columns within all the
previously control+clicked columns). The multi column sort saves all the previosuly
control+clicked columns in an array and sorts them in reverse order. So if you click
column 0, 2, 3, 5 while holding control, it will sort 5, 3, 2, 0. ( this acheives a
muli-column sort).
*/
void CMultiSortListCtrl::SortColumn( int iSubItem, BOOL bSortingMultipleColumns )
{
int iNumCombinedSortedCols = GetNumCombinedSortedColumns();
m_bSorting = true;
if( bSortingMultipleColumns )
{
if( NotInCombinedSortedColumnList( iSubItem ) )
m_aCombinedSortedColumns[ iNumCombinedSortedCols++ ] = iSubItem;
else
MoveItemInCombinedSortedListToEnd( iSubItem );
for( int i = iNumCombinedSortedCols - 1; i >= 0 ; i-- )
{
SORT_STATE ssEachItem = GetItemSortState( m_aCombinedSortedColumns[i] );
if( iNumCombinedSortedCols - 1 != i )
ssEachItem = (SORT_STATE)!ssEachItem;
CSortClass csc(this, m_aCombinedSortedColumns[i], IsColumnNumeric( m_aCombinedSortedColumns[i] ) );
csc.Sort( ssEachItem );
if( i == iNumCombinedSortedCols - 1 )
{ //Only swap the last column's sort order.
m_ctlHeaderCtrl.SetSortImage( m_aCombinedSortedColumns[i], ssEachItem );
SetItemSortState( m_aCombinedSortedColumns[i] , (SORT_STATE)!ssEachItem );
}
}
}
else
{
m_ctlHeaderCtrl.RemoveAllSortImages();
EmptyArray(m_aCombinedSortedColumns);
m_aCombinedSortedColumns[ 0 ] = iSubItem;
SORT_STATE ssEachItem = GetItemSortState( iSubItem );
CSortClass csc(this, iSubItem, IsColumnNumeric( iSubItem ) );
csc.Sort( ssEachItem );
m_ctlHeaderCtrl.SetSortImage( iSubItem, ssEachItem );
SetItemSortState( iSubItem , (SORT_STATE)!ssEachItem );
}
m_bSorting = false;
}
/*
My version of InsertColumn that will automatically load the last column
width from the registry.
*/
int CMultiSortListCtrl::InsertColumn(int nCol, LPCTSTR lpszColumnHeading, int nFormat, int nWidth, int nSubItem)
{
CListCtrl::InsertColumn( nCol, lpszColumnHeading, nFormat, nWidth, nSubItem );
//SetColumnWidth( nCol );
return 1;
}
/*
Utility function to get rid of all the columns
*/
void CMultiSortListCtrl::DeleteAllColumns()
{
int iNumCols = GetColumnCount();
for ( int i = 0; i < iNumCols; i++ )
CListCtrl::DeleteColumn(0);
}
/*
Utility function to get rid of all items.
*/
void CMultiSortListCtrl::DeleteAllItems()
{
if( GetItemCount() > 0 )
CListCtrl::DeleteAllItems();
}
/*
Utility function to get the number of columns
written by Zafir Anjum
*/
UINT CMultiSortListCtrl::GetColumnCount()
{
CHeaderCtrl *pHeaderCtrl = (CHeaderCtrl*)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 CListCtrl::OnCreate
*/
int CMultiSortListCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CListCtrl::OnCreate(lpCreateStruct) == -1)
return -1;
// set list control's style to hilight the entire row
DWORD dwStyle = SendMessage(LVM_GETEXTENDEDLISTVIEWSTYLE);
dwStyle |= LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_HEADERDRAGDROP;
SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE, 0, (LPARAM)dwStyle);
m_ctlHeaderCtrl.SubclassWindow( GetDlgItem(0)->m_hWnd );
return 0;
}
/*
We are only sorting in report view so far.
*/
BOOL CMultiSortListCtrl::PreCreateWindow(CREATESTRUCT& cs)
{
cs.style |= LVS_REPORT;
return CListCtrl::PreCreateWindow(cs);
}
/*
Utility function to tell you if a sort is taking place
*/
const BOOL CMultiSortListCtrl::IsSorting() const
{
return CMultiSortListCtrl::m_bSorting;
}
/*
Utility function to tell you if the control key is being pressed
*/
const int CMultiSortListCtrl::IsControlPressed() const
{
return (::GetKeyState( VK_CONTROL ) < 0 );
}
/*
Message handler for when a header is clicked.
*/
void CMultiSortListCtrl::OnHeaderClicked(NMHDR* pNMHDR, LRESULT* pResult)
{
if(!m_bCreate)
{
DWORD dwStyle = SendMessage(LVM_GETEXTENDEDLISTVIEWSTYLE);
dwStyle |= LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP;
SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE, 0, (LPARAM)dwStyle);
m_ctlHeaderCtrl.SubclassWindow( GetDlgItem(0)->m_hWnd );
m_bCreate = true;
}
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 CMultiSortListCtrl::OnDestroy()
{
//SaveColumnWidths();
CListCtrl::OnDestroy();
}
/*
Utility function to tell you if a column is in the combined sorted list.
*/
BOOL CMultiSortListCtrl::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 get you the sort state of a column
*/
const SORT_STATE CMultiSortListCtrl::GetItemSortState( int iItem ) const
{
return (SORT_STATE)((m_lColumnSortStates) & ( 1 << iItem ));
}
/*
Utility function to set the sort state of a column
*/
void CMultiSortListCtrl::SetItemSortState(int iItem, SORT_STATE bSortState)
{
if( bSortState != GetItemSortState( iItem ) )
m_lColumnSortStates ^= (1 << iItem);
}
/*
Utility function to get you the number of combined sorted columns
*/
const int CMultiSortListCtrl::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 CMultiSortListCtrl::EmptyArray( int *pArray )
{
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 CMultiSortListCtrl::MoveItemInCombinedSortedListToEnd(int iItem)
{
int iNumCombinedSortedColumns = GetNumCombinedSortedColumns();
int aCombinedSortedColumns[MAX_COLUMNS];
memset( aCombinedSortedColumns, -1, MAX_COLUMNS );
int iItemIndex = FindItemInCombedSortedList( iItem );
if( iItemIndex != -1 )
{
if( iItemIndex > 0 )
{
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 CMultiSortListCtrl::FindItemInCombedSortedList( int iItem )
{
int iNumCombinedSortedColumns = GetNumCombinedSortedColumns();
for( int i = 0; i < iNumCombinedSortedColumns; i++ )
{
if(m_aCombinedSortedColumns[i] == iItem )
return i;
}
return -1;
}
/*
Utility function to set a column that will contain only numeric values.
Speeds up the sorting if this is set on the right columns.
*/
void CMultiSortListCtrl::SetColumnNumeric( int iCol )
{
m_aNumericColumns.Add( iCol );
}
/*
Utility function to tell you if the given column is set as numeric.
*/
const BOOL CMultiSortListCtrl::IsColumnNumeric( int iCol ) const
{
for( int i = 0; i < m_aNumericColumns.GetSize(); i++ )
{
if( m_aNumericColumns.GetAt( i ) == (UINT)iCol )
return true;
}
return false;
}
/*
Utility function to remove the numeric status of a column
*/
void CMultiSortListCtrl::UnsetColumnNumeric(int iCol)
{
int iIndex = FindNumericColumnIndex( iCol );
if( iIndex >= 0 )
m_aNumericColumns.RemoveAt( iIndex );
}
/*
Utility function to find a numeric column in the array.
*/
int CMultiSortListCtrl::FindNumericColumnIndex( int iCol )
{
for( int i = 0; i < m_aNumericColumns.GetSize(); i++ )
{
if( m_aNumericColumns.GetAt( i ) == (UINT)iCol )
return i;
}
return -1;
}
void CMultiSortListCtrl::SetUniqueName(LPCTSTR lpszUniqueName)
{
m_strUniqueName = lpszUniqueName;
}
BOOL CMultiSortListCtrl::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -