Click here to Skip to main content
15,867,278 members
Articles / Desktop Programming / MFC

MultiPage PrintPreview Enhancements for MFC Doc/View Applications

Rate me:
Please Sign up or sign in to vote.
4.73/5 (13 votes)
27 Apr 2002CPOL3 min read 165K   5.2K   34   30
How to improve the standard MFC print preview options to allow preview from 1 to 9 pages at a time

Sample Image - 9pages.gif

Acknowledgements

First, I would like to acknowledge some articles which I used as a reference when putting this project together.

Overview

The standard Print Preview mechanism supplied by MFC is a little understood phenomenon. There are few enhancements currently published for it, and none for showing more than 2 pages at a time in preview mode. I set out to solve this! It turned out to be reasonably easy to do, working from the examples supplied by Robin and Yasuhiko. In my solution, I used small parts of both examples (Robins onwner drawn buttons, and Yasuhiko's extra zoom levels).

The enhancement are:

  • From 1 to 9 pages viewed at a time - selectable by a popup menu
  • The scrollbar will not allow blank pages to be scrolled into view
  • You can switch dynamically between portrait and landscape mode using the toolbar command

So how do you go about using these enhancements?

Adding the Required Resources

To use this enhanced preview mode, you need to include the following resources into your project.

These should be copied and pasted into your project. You can do this from the VC IDE, or by manually editing the .rc file (not really recommended):

  • The replacement toolbar resource
  • The popup menu resource
  • The button bitmaps

Add the Source Files

The enhanced preview uses the following source files:

  • MappedBitmapButton.cpp/h - Robin J. Leatherbarrow
  • MultiPagePreview.cpp/h - Myself with some code from Yasuhiko
  • An additional function in your CWinApp derived class - see later

Replacing the Standard Print Preview

To replace the standard PrintPreview supplied by the MFC Doc/View architecture, you have to write a handler in your projects CView class to handle the ID_FILE_PRINT_PREVIEW command. You put the following code in that handler to supplant the MFC preview with the new one:

C++
// replace the default print preview with ours!

// In derived classes, implement special window handling here
// Be sure to Unhook Frame Window close if hooked.
    
// must not create this on the frame.  Must outlive this function
CPrintPreviewState* pState = new CPrintPreviewState;
    
// DoPrintPreview's return value does not necessarily indicate that
// Print preview succeeded or failed, but rather what actions are necessary
// at this point.  If DoPrintPreview returns TRUE, it means that
// OnEndPrintPreview will be (or has already been) called and the
// pState structure will be/has been deleted.
// If DoPrintPreview returns FALSE, it means that OnEndPrintPreview
// WILL NOT be called and that cleanup, including deleting pState
// must be done here.
    
if (!DoPrintPreview(IDD_PREVIEW, this, RUNTIME_CLASS(CMultiPagePreviewView),
                    pState)) // note, put your class name in here
{
    // In derived classes, reverse special window handling here for
    // Preview failure case
        
    TRACE0("Error: DoPrintPreview failed.\n");
    AfxMessageBox(AFX_IDP_COMMAND_FAILURE);
    delete pState;      // preview failed to initialize, delete State now
    pState = NULL;
}

Update Your CWinApp Derived Class

The additional functionality to switch dynamically between portrait and landscape mode in print preview requires support of an additional function in your CWinApp derived class. This is because the m_hDevMode object is a protected member and cannot be accessed directly by the preview class. The prototype of this function and the code for it is as follows:

C++
// prototype for CWinApp derived class
void        SetPrintOrientation(int mode) ;

void CYourApp::SetPrintOrientation(int mode)
{
    switch (mode)
        {
        case DMORIENT_PORTRAIT :
                {
                // portrait mode
                PRINTDLG pd;
                pd.lStructSize = (DWORD)sizeof(PRINTDLG) ;
                BOOL bRet = GetPrinterDeviceDefaults(&pd) ;
                if (bRet)
                    {
                    // protect memory handle with ::GlobalLock and ::GlobalUnlock
                    DEVMODE *pDevMode = (DEVMODE*)::GlobalLock(m_hDevMode) ;
                    // set orientation to portrait
                    pDevMode->dmOrientation = DMORIENT_PORTRAIT ;
                    ::GlobalUnlock(m_hDevMode) ;
                    }
                }
                break ;
        case DMORIENT_LANDSCAPE :
                {
                // landscape mode
                PRINTDLG pd;
                pd.lStructSize = (DWORD)sizeof(PRINTDLG) ;
                BOOL bRet = GetPrinterDeviceDefaults(&pd) ;
                if (bRet)
                    {
                    // protect memory handle with ::GlobalLock and ::GlobalUnlock
                    DEVMODE *pDevMode = (DEVMODE*)::GlobalLock(m_hDevMode) ;
                    // set orientation to landscape
                    pDevMode->dmOrientation = DMORIENT_LANDSCAPE ;
                    ::GlobalUnlock(m_hDevMode) ;
                    }
                }
                break ;
        default :    ASSERT(FALSE) ;        // invalid parameter
        }
}

If you are adding this to your application, you will also have to mod the code so it uses your CWinApp derived class name and not that of the demo project. If you insert the code and compile. The errors generated will get you to the lines you will need to update.

Once all of this is in, the preview mode should work automatically. You should now be able to preview upto a maximum of 9 pages simultaneously. This can be expanded for more by enhancing the options in the popup menu, and modifying the function CMultiPagePreviewView::OnPreviewPages() to handle the new layout. Just make sure that you increase the size of the m_pageInfoArray2 array in the .h file to avoid overwriting memory. So if you wanted to support a 4 * 4 preview, the array size needs to be increased from 9 to 16.

Changes Made

  • 24-4-2002 Release 1
  • 28-4-2002 Release 2 - Added portrait/landscale switching and updated the scrollbar control code

Future Enhancements

I would like to add the following features in the future:

  • Make the pages flicker free

Or you could always add them yourselves.

License

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


Written By
Software Developer (Senior) Sirius Analytical Instruments
United Kingdom United Kingdom
A research and development programmer working for a pharmaceutical instrument company for the past 17 years.

I am one of those lucky people who enjoys his work and spends more time than he should either doing work or reseaching new stuff. I can also be found on playing DDO on the Cannith server (Send a tell to "Maetrim" who is my current main)

I am also a keep fit fanatic, doing cross country running and am seriously into [url]http://www.ryushinkan.co.uk/[/url] Karate at this time of my life, training from 4-6 times a week and recently achieved my 1st Dan after 6 years.

Comments and Discussions

 
GeneralProblem with hiding the view Pin
wolfbert17-Oct-03 2:27
wolfbert17-Oct-03 2:27 

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.