Click here to Skip to main content
11,704,768 members (58,571 online)
Click here to Skip to main content

Print ListCtrl on multiple pages

, 26 Mar 2000 CPOL 131.5K 2.1K 54
Rate this:
Please Sign up or sign in to vote.
Printing the contents of a CListCtrl or CListView with multiple pages
  • Download demo project - 56 Kb

    Sample Image - listPrint.gif

    The demo consists of a doc/view-framework with a CListView-class. It has a button to fill with data and another to print the stuff.

    The Problem

    MFC has no built-in feature for printing a List Control. Some approaches can be found on the internet but they always have the problem that if you have more columns then will fit to a single page, then the rest is clipped away and not printed.

    The Solution

    My class manages:

    • Printing a CListCtrl or CListView with support for multiple pages
    • The first column is printed again on each page
    • The width of the columns are used for printing
    • Columns can be sorted (nothing new, I know)

    My appraoch is mainly based on the fabulous work of Dan Pilat with his article Printing with MFC Made Easy.

    My work was to build a kind of wrapper over his classes and do the newpage-logic. Each single page is printed just by his classes with some overriden virtual functions.

    The printing from the CListView then looks like this:

    void CListPrintView::OnFilePrintDirect() 
    {
    	CWaitCursor		wait;
    	CListCtrl		&listctrl = GetListCtrl();
    
    	CListCtrlPrintJob	job(&listctrl, !m_bUseSetup);
    	job.Print();
    }
    

    The trick is to calculate a translater for Screen [Pixels] -> Printer [mm]. Then I know how much space a column needs on the paper. This helps me calculating the amount of columns that fit to a page.

    I then loop through all columns and count their width.

    When its time to begin a new one, I define a print-job the Dan Pilat way.

    void CListCtrlPrintJob::OnPrint()
    {
    	...
    
    	widthOfPage = m_pListCtrl->GetColumnWidth(0);
    	iStart      = 1;
    
    	for( i=1 ; i<m_ColCount ; i++ )
    	{
    		widthThisCol = m_pListCtrl->GetColumnWidth(i);
    		bNewPage = FALSE;
    
    		if( widthOfPage+widthThisCol > paperWidthInPixel )
    		{	
    			// not enough place on side, begin new one
    			bNewPage = TRUE;
    		}
    		else
    		{
    			// also print the last one
    			if( i == (m_ColCount-1) )
    				bNewPage = TRUE;
    			
    			// this col needs this space:
    			widthOfPage += widthThisCol;
    		}
    
    		if( bNewPage )
    		{
    			// ok, print it on a new page
    			CListCtrlDataPage data(this, m_pListCtrl, iStart, i);
    			data.Print();
    
    			iStart = i+1;
    			widthOfPage = m_pListCtrl->GetColumnWidth(0);
    		}
    	}
    }
    

    CListCtrlDataPage is derived from GPrintUnit and also has this print-function:

    BOOL CListCtrlDataPage::Print()
    {
    	...
    
    	zeilenMax = m_pListCtrl->GetItemCount();
    		
    	for( zeile=0 ; zeile<zeilenMax ; zeile++ )
    	{
    		nRC = StartRow();
    		// first Col
    		if( m_pListCtrl->GetItemText(zeile, 0, szText, 
                                                                 sizeof(szText)) )
    			PrintCol(colId, szText, DT_LEFT);
    		colId++;
    		for( spalte=m_firstCol ; spalte<=m_lastCol ; spalte++ )
    		{
    			if( m_pListCtrl->GetItemText(zeile, spalte, 
                                                         szText, sizeof(szText)) )
    				PrintCol(colId, szText, DT_LEFT);
    			colId++;
    		}
    
    		EndRow();
    	}
    
    	EndPage();
    	
    	...
    }
    

    Do you have Enchancements (e.g. a print-preview) ?

    Let me know: Markus Loibl, Germany

  • License

    This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Share

    About the Author

    Markus Loibl
    Germany Germany
    No Biography provided

    You may also be interested in...

    Comments and Discussions

     
    GeneralMy vote of 5 Pin
    china_hxx11-Nov-14 18:01
    memberchina_hxx11-Nov-14 18:01 
    GeneralMy vote of 5 Pin
    Don_Hard11-Jul-10 2:39
    memberDon_Hard11-Jul-10 2:39 
    QuestionBug or not? Pin
    littlesome12-Jul-07 22:04
    memberlittlesome12-Jul-07 22:04 
    QuestionHow Can I Increase the margin Pin
    vimal19838-Jun-07 0:13
    membervimal19838-Jun-07 0:13 
    GeneralA small bug Pin
    _MAK_6-Jun-07 3:21
    member_MAK_6-Jun-07 3:21 
    QuestionPrinting in landscape Pin
    jonbong9-Nov-06 3:48
    memberjonbong9-Nov-06 3:48 
    Generalprint in dialog based application Pin
    y_naga_malleswararao22-Mar-06 3:22
    membery_naga_malleswararao22-Mar-06 3:22 
    QuestionRe: print in dialog based application Pin
    rootdial23-May-07 20:22
    memberrootdial23-May-07 20:22 
    GeneralPrint Preview Pin
    Luke_2123-Oct-05 22:59
    memberLuke_2123-Oct-05 22:59 
    QuestionIs there a way to print several list controls at the same page? Pin
    Dudi Daabul18-Oct-05 7:20
    memberDudi Daabul18-Oct-05 7:20 
    Questionwhat is i am not using ListCtrl on a Dialog based project Pin
    Sameer Khanna22-May-05 19:44
    memberSameer Khanna22-May-05 19:44 
    QuestionDoes it need any DLL ? Pin
    AlexPark007022-Aug-04 22:08
    memberAlexPark007022-Aug-04 22:08 
    AnswerRe: Does it need any DLL ? Pin
    tim+999929-Mar-05 9:02
    membertim+999929-Mar-05 9:02 
    GeneralPrinting by using horizontal and vertical grid lines Pin
    Erkan Sütçüler26-Feb-04 5:04
    sussErkan Sütçüler26-Feb-04 5:04 
    GeneralProblem with Acrobat Distiller Pin
    niederer roland3-Sep-03 21:16
    memberniederer roland3-Sep-03 21:16 
    QuestionHow do you alter the CPrintDialog styles? Pin
    AORD18-Jan-03 20:54
    memberAORD18-Jan-03 20:54 
    GeneralPrint columns in order Pin
    Rudy Crespin30-Jul-01 4:19
    memberRudy Crespin30-Jul-01 4:19 
    GeneralRe: Print columns in order Pin
    Scot Brennecke6-Apr-02 19:37
    memberScot Brennecke6-Apr-02 19:37 
    Generalfont changes Pin
    Fig27-Jun-01 18:56
    memberFig27-Jun-01 18:56 
    GeneralRe: font changes Pin
    Anonymous30-Oct-02 18:12
    sussAnonymous30-Oct-02 18:12 
    GeneralRe: font changes Pin
    sbone992-May-03 5:56
    susssbone992-May-03 5:56 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    | Advertise | Privacy | Terms of Use | Mobile
    Web03 | 2.8.150819.1 | Last Updated 27 Mar 2000
    Article Copyright 2000 by Markus Loibl
    Everything else Copyright © CodeProject, 1999-2015
    Layout: fixed | fluid