Download source files - 4 Kb
Download demo project - 22 Kb
Introduction
CPrintPreviewListBox is a CListBox derived class which is designed
for one simple task - printing and previewing in SDI and MDI applications (for the present
not in dialog based applications).
This is the first version of the class with print and preview
capabilities, so the class has some constraints such as the maximum number
of characters per line (81), fixed font (Courier New) and as I already
mentioned this class does not support dialog based applications.
However, this class is very useful for some special tasks such as print and
print previewing the content of list boxes in your SDI or MDI application.
This class also supports line breaking.
Consequently I hope that following versions of this class will be able to
change font and have other capabilities.
The CPrintPreviewListBox class has several important
functions that should all be self explanatory:
void Print();
void SetPrintTitle( CString sNewTitle = "Default title" );
CString GetPrintTitle();
void SetOnlySelectedItems( bool bOnlySelectedItems );
bool GetOnlySelectedItems();
virtual void OnBeginPrinting (CDC*, CPrintInfo*);
virtual void OnPrint (CDC*, CPrintInfo*);
virtual void OnEndPrinting (CDC*, CPrintInfo*);
Using this class is very simple, as shown
in the following lines of code (see the sampleView.h header file):
#include "PrintPreviewListBox.h"
class CSampleView : public CView
{
public:
CPrintPreviewListBox m_lstPPListBox;
...
};
The sampleView.cpp file demonstrates using the class as follows:
int CSampleView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
CSampleApp* pApp = (CSampleApp*) AfxGetApp();
CRect rect;
rect.SetRectEmpty();
if( pApp->m_bSingleSelection )
{
if( m_lstPPListBox.Create( WS_CHILD | WS_BORDER | WS_VSCROLL,
rect, this, 100 ) == -1 )
return -1;
}
else
{
if( m_lstPPListBox.Create( WS_CHILD | LBS_HASSTRINGS | LBS_MULTIPLESEL
| LBS_EXTENDEDSEL | LBS_NOTIFY | LBS_NOINTEGRALHEIGHT
| LBS_USETABSTOPS | WS_VSCROLL,
rect, this, 100 ) == -1 )
return -1;
}
m_lstPPListBox.SetPrintTitle();
m_lstPPListBox.ShowWindow (SW_SHOW);
return 0;
}
void CSampleView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
m_lstPPListBox.OnBeginPrinting(pDC, pInfo);
}
void CSampleView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
m_lstPPListBox.OnEndPrinting(pDC, pInfo);
}
void CSampleView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
m_lstPPListBox.OnPrint(pDC, pInfo);
}
void CSampleView::OnOnlySelectedItems()
{
if( m_lstPPListBox.GetOnlySelectedItems() )
{
m_lstPPListBox.SetOnlySelectedItems( false );
m_bOnlySelectedItems = FALSE;
}
else
{
m_lstPPListBox.SetOnlySelectedItems( true );
m_bOnlySelectedItems = TRUE;
}
}
You can see entire code in attached files in details.
Credits
Parts of the code were based on Chris Maunder's Printing without the Document/View framework and Koay Kah Hoe's Print Previewing without the Document/View Framework.