Priting List View Control Items

Many a times we need to print the contents listed in a list control. For example, generally in an anti virus software the details such as which files are infected, cured, quarantined etc. are listed in a list control which user may wish to print. We can implement the logic to print the list control. Here, we intend to print a list control in which filename, file size and date are displayed.

Create an SDI application and derive the view class from CListView class. Add appropriate columns and data to the list control. Here we will discuss only the handlers that are used for printing. Firstly add the following variables to the view class:

CStringList m_prnlist, m_reportlist ;

CFont m_prnfont ;

int ht, wd, linesperpage, charsperline, maxpage ;

Modify the OnPreparePrinting( ) function as shown below. Here, we have read the data from the list control and added it in a CStringList. We have also added the column names to the list.

BOOL CSortlistView::OnPreparePrinting ( CPrintInfo* pInfo )

{

m_reportlist.RemoveAll( ) ;

CString str, temp ;

str.Format ( "%25s %25s %25s", " Filename", " Size ", " Date " ) ;

m_reportlist.AddTail ( str ) ;

int listcount = GetListCtrl( ).GetItemCount( ) ;

for ( int i = 0 ; i <>

{

str.Format ( "%2d. ", i ) ;

for ( int j = 0 ; j <>

{

temp.Format ( "%25s", GetListCtrl( ).GetItemText ( i, j ) ) ;

str += temp ;

}

m_reportlist.AddTail ( str ) ;

}

return DoPreparePrinting ( pInfo ) ;

}

Let us now create a font in which the contents of the list control would be printed. Also, we need to calculate the characters per line, number of pages to be printed etc. This has been done in the OnBeginPrinting( ) function as shown below:

void CSortlistView::OnBeginPrinting ( CDC* pDC, CPrintInfo* pInfo )

{

m_prnfont.CreatePointFont ( 100, "Arial", pDC ) ;

CFont *prevfont = pDC -> SelectObject ( &m_prnfont ) ;

TEXTMETRIC tm ;

pDC -> GetTextMetrics ( &tm ) ;

ht = tm.tmHeight + tm.tmExternalLeading ;

wd = tm.tmAveCharWidth + 1 ;

int vertpixels = pDC -> GetDeviceCaps ( VERTRES ) ;

int horzpixels = pDC -> GetDeviceCaps ( HORZRES ) ;

CSize sz ( horzpixels, vertpixels ) ;

pDC -> DPtoLP ( &sz ) ;

linesperpage = sz.cy / ht ;

charsperline = sz.cx / wd ;

linesperpage -= 10 ;

CString tempstr ;

POSITION temppos = m_reportlist.GetHeadPosition( ) ;

int tempcount = m_reportlist.GetCount( ) ;

m_prnlist.RemoveAll( ) ;

for ( int i = 0 ; i <>

{

tempstr = m_reportlist.GetNext ( temppos ) ;

m_prnlist.AddTail ( tempstr ) ;

}

POSITION prnpos = m_prnlist.GetHeadPosition( ) ;

POSITION posprev1, posprev2 ;

int prncount = m_prnlist.GetCount( ) ;

CString str, pa ;

for ( i = 0 ; i <>

{

if ( prnpos == NULL )

break ;

posprev1 = posprev2 = prnpos ;

str = m_prnlist.GetNext ( prnpos ) ;

int len = str.GetLength( ) ;

if ( len > charsperline )

{

int linecount = max ( 1, ( len + ( charsperline - 1 ) ) /

charsperline ) ;

CString leftstr ;

int remainlen, leftlen = 0 ;

for ( int j = 0 ; j <>

{

leftstr = str.Left ( charsperline ) ;

m_prnlist.InsertAfter ( posprev2, leftstr ) ;

leftlen += leftstr.GetLength( ) ;

remainlen = len - leftlen ;

m_prnlist.GetNext ( posprev2 ) ;

str = str.Right ( remainlen ) ;

}

str = "" ;

pa = m_prnlist.GetAt ( posprev1 ) ;

m_prnlist.RemoveAt ( posprev1 ) ;

pa.FreeExtra( ) ;

}

else

{

m_prnlist.InsertAfter ( posprev1, str ) ;

pa = m_prnlist.GetAt ( posprev1 ) ;

m_prnlist.RemoveAt ( posprev1 ) ;

pa.FreeExtra( ) ;

}

}

prncount = m_prnlist.GetCount( ) ;

maxpage = max ( 1, ( prncount + ( linesperpage - 1 ) ) / linesperpage ) ;

pDC -> SelectObject ( prevfont ) ;

pInfo -> SetMaxPage ( maxpage ) ;

}

Let us now carry out the actual printing job in the OnPrint( ) function. We have displayed the title ‘Files List’ as the header and page number as the footer. In addition to the list control data we have also added the line number before each row.

void CSortlistView::OnPrint ( CDC* pDC, CPrintInfo* pInfo )

{

CFont *prevfont = pDC -> SelectObject ( &m_prnfont ) ;

TEXTMETRIC tm ;

pDC -> GetTextMetrics ( &tm ) ;

int horzpixels = pDC -> GetDeviceCaps ( HORZRES ) ;

CSize sz ( horzpixels, 0 ) ;

pDC -> DPtoLP ( &sz ) ;

int center = sz.cx / 2 ;

CString header = "Files List" ;

UINT l = center - ( ( header.GetLength( ) / 2 ) * tm.tmAveCharWidth ) ;

pDC -> TextOut ( l, 20, header ) ;

int prncount = m_prnlist.GetCount( ) ;

int start = ( ( pInfo -> m_nCurPage ) - 1 ) * linesperpage ;

int end = start + linesperpage ;

POSITION prnpos = m_prnlist.FindIndex ( linesperpage * ( ( pInfo -> m_nCurPage ) - 1 ) ) ;

for ( int i = start, y = ( ht * 5 ) ; i <>

{

pDC ->TextOut ( 10, y, m_prnlist.GetNext ( prnpos ) ) ;

y += ht ;

}

CString pagenumber ;

pagenumber.Format ( "%2d / %2d", pInfo -> m_nCurPage, maxpage ) ;

l = center - ( ( pagenumber.GetLength( ) / 2 ) * tm.tmAveCharWidth ) ;

pDC -> TextOut ( l, ( linesperpage + 5 + 3 ) * ht, pagenumber ) ;

pDC -> SelectObject ( prevfont ) ;

}

Finally let us do the clean up job in the OnEndPrinting( ) function given below:

void CSortlistView::OnEndPrinting ( CDC* pDC, CPrintInfo* pInfo )

{

m_reportlist.RemoveAll( ) ;

m_prnlist.RemoveAll( ) ;

m_prnfont.DeleteObject( ) ;

CListView::OnEndPrinting ( pDC, pInfo ) ;

}

Run the program and click the ‘Print’ menu item from the File menu. On doing this the contents of the list control would be printed in appropriate rows and columns.


Download

No comments: