Click here to Skip to main content
13,355,018 members (58,803 online)
Click here to Skip to main content
Add your own
alternative version


37 bookmarked
Posted 19 Dec 2000


, 28 Dec 2000
Rate this:
Please Sign up or sign in to vote.
A listbox derived class that allows printing and print preview in SDI and MDI applications
<!-- Article Starts --> <!-- Download Links -->
  • Download source files - 4 Kb
  • Download demo project - 22 Kb
  • <!-- Main HTML starts here -->


    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
    	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;
    	// TODO: Add your specialized creation code here
    	CSampleApp* pApp = (CSampleApp*) AfxGetApp();
    	CRect rect;
    	if( pApp->m_bSingleSelection )
    		if( m_lstPPListBox.Create( WS_CHILD | WS_BORDER | WS_VSCROLL, 
    			rect, this, 100 ) == -1 )
    			return -1;
    			rect, this, 100 ) == -1 )
    			return -1;
    	m_lstPPListBox.ShowWindow (SW_SHOW);
    	return 0;
    void CSampleView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
    	// TODO: add extra initialization before printing
    	m_lstPPListBox.OnBeginPrinting(pDC, pInfo);
    void CSampleView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
    	// TODO: add cleanup after printing
    	m_lstPPListBox.OnEndPrinting(pDC, pInfo);
    void CSampleView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
    	// TODO: Add your specialized code here and/or call the base class
    	m_lstPPListBox.OnPrint(pDC, pInfo);
    //	CView::OnPrint(pDC, pInfo);
    void CSampleView::OnOnlySelectedItems()
    	// TODO: Add your command handler code here
    	if( m_lstPPListBox.GetOnlySelectedItems() )
    		m_lstPPListBox.SetOnlySelectedItems( false );
    		m_bOnlySelectedItems = FALSE;
    		m_lstPPListBox.SetOnlySelectedItems( true );
    		m_bOnlySelectedItems = TRUE;

    You can see entire code in attached files in details.


    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.


    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


    About the Author

    Tesic Goran
    Web Developer student
    Russian Federation Russian Federation
    No Biography provided

    You may also be interested in...

    Comments and Discussions

    GeneralIt can also be used in dialog box app. Excellent! Pin
    fredpu12-Oct-04 7:06
    memberfredpu12-Oct-04 7:06 
    GeneralVery good article Pin
    Nemanja Trifunovic28-Dec-00 13:15
    memberNemanja Trifunovic28-Dec-00 13:15 
    QuestionWhy Necessary? Pin
    Anonymous28-Dec-00 7:30
    memberAnonymous28-Dec-00 7:30 
    AnswerRe: Why Necessary? Pin
    =[ Abin ]=12-Jan-04 14:01
    member=[ Abin ]=12-Jan-04 14:01 

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

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

    Permalink | Advertise | Privacy | Terms of Use | Mobile
    Web02 | 2.8.180111.1 | Last Updated 29 Dec 2000
    Article Copyright 2000 by Tesic Goran
    Everything else Copyright © CodeProject, 1999-2018
    Layout: fixed | fluid