【VC开源代码栏目提醒】:网学会员为广大网友收集整理了,MULTICOLUMNSORTLISTVIEW.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 CListView, then go into the
header file and include MultiColumnSortListView.h and change all
references to CListView to CMultiColumnSortListView. Then in the .cpp
file of your class, change all the message maps to CMultiColumnSortListView
instead of CListView. 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 -- CMultiColumnSortListView::GetColumnCount
Roger Onslow -- CMultiColumnSortListView::AutoSizeColumns
Zafir Anjum -- CSortableHeaderCtrl::SetSortImage
Me -- The Rest, I think.
*/
// MultiColumnSortListView.cpp : implementation file
//
#include "stdafx.h"
#include "MultiColumnSortListView.h"
#include "SortClass.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMultiColumnSortListView
IMPLEMENT_DYNCREATE(CMultiColumnSortListView, CListView)
/*
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
*/
CMultiColumnSortListView::CMultiColumnSortListView()
{
m_strUniqueName.Empty();
m_strColumnWidthSection = "ColumnWidths";
m_bSorting = false;
m_lColumnSortStates = 0;
}
CMultiColumnSortListView::~CMultiColumnSortListView()
{
}
BEGIN_MESSAGE_MAP(CMultiColumnSortListView, CListView)
//{{AFX_MSG_MAP(CMultiColumnSortListView)
ON_WM_CREATE()
ON_WM_DESTROY()
//}}AFX_MSG_MAP
ON_NOTIFY(HDN_ITEMCLICKA, 0, OnHeaderClicked)
ON_NOTIFY(HDN_ITEMCLICKW, 0, OnHeaderClicked)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMultiColumnSortListView drawing
void CMultiColumnSortListView::OnDraw(CDC* pDC)
{
CDocument* pDoc = GetDocument();
}
/////////////////////////////////////////////////////////////////////////////
// CMultiColumnSortListView diagnostics
#ifdef _DEBUG
void CMultiColumnSortListView::AssertValid() const
{
CListView::AssertValid();
}
void CMultiColumnSortListView::Dump(CDumpContext& dc) const
{
CListView::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMultiColumnSortListView 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.
*/
void CMultiColumnSortListView::SaveColumnWidths()
{ //You must set a unique name for every listctrl
ASSERT( m_strUniqueName.GetLength() );
CString strEntry( m_strUniqueName );
CString strValue;
CListCtrl &rListCtrl = GetListCtrl();
int iNum