Click here to Skip to main content
15,886,199 members
Articles / Desktop Programming / MFC

Another Report List Control

Rate me:
Please Sign up or sign in to vote.
4.89/5 (95 votes)
31 Dec 20036 min read 1.3M   17.4K   235  
A report style CListCtrl supporting sorting, sub-item editing, sub-item image, sub-item color etc.
// ReportCtrlDemoView.cpp : implementation of the CReportCtrlDemoView class
//

#include "stdafx.h"
#include "ReportCtrlDemo.h"

#include "ReportCtrlDemoDoc.h"
#include "ReportCtrlDemoView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CString GenRandInt()
{
	int n = (rand() % 100000) + 200;
	CString str;
	str.Format(_T("%d"), n);
	return str;
}

CString GenRandDate()
{
	COleDateTime now = COleDateTime::GetCurrentTime();
	COleDateTimeSpan span(rand() % 3650, rand() % 24, rand() % 60, rand() % 60);
	now -= span;
	return now.Format();
}

CString GenRandDbl()
{
	CString s = GenRandInt();
	s.Insert(s.GetLength() - 2, _T('.'));
	return s;
}

CString GenRandStr()
{
	const CString LETTERS = _T("abcdefghijklmnopqrstuvwxyz0123456789");
	const int LEN = 16;

	CString str;
	for (int i = 0; i < LEN; i++)
	{
		str += LETTERS[rand() % LETTERS.GetLength()];
	}

	return str;
}

/////////////////////////////////////////////////////////////////////////////
// CReportCtrlDemoView

IMPLEMENT_DYNCREATE(CReportCtrlDemoView, CFormView)

BEGIN_MESSAGE_MAP(CReportCtrlDemoView, CFormView)
	//{{AFX_MSG_MAP(CReportCtrlDemoView)
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CReportCtrlDemoView construction/destruction

CReportCtrlDemoView::CReportCtrlDemoView()
	: CFormView(CReportCtrlDemoView::IDD)
{
	//{{AFX_DATA_INIT(CReportCtrlDemoView)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CReportCtrlDemoView::~CReportCtrlDemoView()
{
}

void CReportCtrlDemoView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CReportCtrlDemoView)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BOOL CReportCtrlDemoView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CReportCtrlDemoView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
	
	srand((unsigned)time(NULL));
	m_wndList.Create(this, 0, NULL, 0);
	m_wndList.SetImageList(IDB_BITMAP1);
	m_wndList.SetColumnHeader(_T("Member ID, 100; Register Date, 140; Salary, 90, 2; FooBar, 120"));

	for (int i = 0; i < 100; i++)
	{
		const int IDX = m_wndList.InsertItemEx(INT_MAX, GenRandInt(), GenRandDate(), GenRandDbl(), GenRandStr());
		for (int j = 0; j < 3; j++)
		{
			m_wndList.SetItemImage(IDX, j, rand() % 5);
		}
	}

	m_wndList.SetHeaderImageList(IDB_BITMAP2);
	m_wndList.SetHeaderImage(0, 0);
	m_wndList.SetHeaderImage(1, 1, FALSE);
	m_wndList.SetHeaderImage(2, 2);
	
	m_wndList.SetGridLines(TRUE); // SHow grid lines
	m_wndList.SetCheckboxeStyle(RC_CHKBOX_NORMAL); // Enable checkboxes
	m_wndList.SetEditable(TRUE); // Allow sub-text edit
}

/////////////////////////////////////////////////////////////////////////////
// CReportCtrlDemoView printing

BOOL CReportCtrlDemoView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CReportCtrlDemoView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CReportCtrlDemoView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CReportCtrlDemoView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	// TODO: add customized printing code here
}

/////////////////////////////////////////////////////////////////////////////
// CReportCtrlDemoView diagnostics

#ifdef _DEBUG
void CReportCtrlDemoView::AssertValid() const
{
	CFormView::AssertValid();
}

void CReportCtrlDemoView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CReportCtrlDemoDoc* CReportCtrlDemoView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CReportCtrlDemoDoc)));
	return (CReportCtrlDemoDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CReportCtrlDemoView message handlers

void CReportCtrlDemoView::OnSize(UINT nType, int cx, int cy) 
{
	CFormView::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	m_wndList.ResizeToFitParent();
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions