Click here to Skip to main content
Click here to Skip to main content

MultiPage PrintPreview enhancements for MFC Doc/View applications

By , 27 Apr 2002
 

Sample Image - 9pages.gif

Acknowledgements

First I would like to acknowledge some articals 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 this these enhancements?

    Adding the required resources

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

    • The replacement toolbar resource
    • The popup menu resource
    • The button bitmaps
    • 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)

    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:

    // 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 additonal 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 a follows:

    // 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 your 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 update the scrollbar control code

    Future enhancements

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

    • 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)

    About the Author

    Roger Allen
    Software Developer (Senior) Sirius Analytical Instruments
    United Kingdom United Kingdom
    Member
    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.

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board.
    Search this forum  
        Spacing  Noise  Layout  Per page   
    QuestionWhat piece of code should I change to get rid of the spacing..memberMember 78714814 Mar '13 - 8:01 
    Great article!
     
    One quick question.
     
    What piece of code should I change to remove the blank spacing between the pages?
    So that each page would be beside each other in other words?
    I have tried everything.
     
    Thank you!
    GeneralHellomemberMark Jefferson27 Apr '08 - 4:41 
    Could I send email to you?
    I have some question about printing in MFC. thank you. Smile | :)
    GeneralDon't need to modify you CWinApp classmemberWolfram Rösler21 Jan '07 - 21:59 
    You can get/set the current printer orientation without having to modify your application class because it's not necessary to access CWinApp::m_hDevMode - you find another hDevMode in the PRINTDLG structure retrieved with GetPrinterDeviceDefaults which is ready for use. Just try this, which can go right into the view's message handler:
     
         CWinApp *const pApp = AfxGetApp();
         if (pApp!=NULL)
         {
              PRINTDLG pd;
              pd.lStructSize = sizeof(PRINTDLG);
              if (pApp->GetPrinterDeviceDefaults(&pd))
              {
                   DEVMODE FAR *const pDevMode
                        = reinterpret_cast<DEVMODE FAR *>(::GlobalLock(pd.hDevMode));
                   pDevMode->dmOrientation   = DMORIENT_LANDSCAPE:
                   ::GlobalUnlock(pd.hDevMode);
              }
         }
     
    And it looks just better without those ugly C casts. Smile | :)

    GeneralprintingmemberAlexDa1 Dec '05 - 23:25 
    In a network printer, when the printer print the document, How many pages printer lets print & which system sent the data to print.
     
    How to find this job fron VC++/VB
     
    Please help me



    GeneralCombine pages with portrait and landscapememberStoil24 Sep '04 - 4:47 
    First, great work!!!
    I wonder if it's possible to change your code to have opportunity to add pages with different size and orientation!?
    To have portrait and landscape together!?
    Thank you in advance and Good luck!
     
    Stoil Todorov
    GeneralRe: Combine pages with portrait and landscapememberStoil16 Oct '04 - 0:26 
    I understood how to combine pages with portrait and landscape.
    I found Microsoft Knowledge Base Article - 214617 "Changing the Page Orientation to Landscape Without Interacting with the Print Dialog Box in an MFC-Based Application" in MSDN and I used the given code in my project.
    Now I can change orientation of page during printing and in print preview. The key was in OnPrepareDC(CDC* pDC, CPrintInfo* pInfo) where I change orientation with this function: pDC->ResetDC(m_lpDevMode) - m_lpDevMode show page orientation.
     
    Stoil Todorov
    QuestionFont Bug ?membermroy_10016 Feb '04 - 5:44 
    Why the Print Preview font (Page X of X) change from Courier to Arial, and back to courier depending of the zooming factor and the orientation ?
     
    I'm using WinXP and Win2K and have this issue on both systems
    GeneralFrom landscape to full setupmemberasconaga3 Dec '03 - 3:08 
    Instead of just limiting your code to landscape and portrait why not add the setup dialog.
     
    CWinApp *pApp = AfxGetApp();
    CPrintDialog pd(TRUE);
    pApp->DoPrintDialog(&pd); // print setup dialog
     
    CPrintInfo *pInfo = m_pPreviewInfo;
    m_pPreviewInfo = NULL;
     
    m_dcPrint.Detach(); // print DC is deleted by CPrintInfo destructor
    delete pInfo;
    SetPrintView(m_pPrintView);
     
    This allows the view to change options, printer and orientation
     
    Asconaga Blush | :O
    GeneralProblem with hiding the viewmemberwolfbert17 Oct '03 - 2:27 
    Hello,
    first very nice job, seems to be very helpfull
     
    But before i could even think using this new Print Preview, maybe somebody could help me with following problem.
     
    I use Visual STudio .Net (VC++7.0), migrated a middlesized Application from VC++6.0, But experiences hangups when I try to switch into Print preview.
    I debugged the project and found out that so far everything seems right, Until in
    CFrameWnd::OnSetPreviewMode,
    following code is executed
    HWND hWnd = ::GetDlgItem(m_hWnd, pState->nIDMainPane);
    ASSERT(hWnd != NULL); // must be one that we are hiding!
    ::ShowWindow(hWnd, SW_HIDE);
    where
    pState->nIDMainPane==AFX_IDW_PANE_FIRST
    which should be correct, and the retrieved hWnd seems to be ok too (Its not NULL) but then on the ::ShowWindow command, somehow the application went into an endless loop...
     
    Did anybody had the same Problems and could solve this?
     
    THanks in Advance
    Wolfbert

    Generalvery usefulmemberMarco Walle25 Sep '03 - 5:45 
    This is great! thanks Roger!

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

    Permalink | Advertise | Privacy | Mobile
    Web04 | 2.6.130523.1 | Last Updated 28 Apr 2002
    Article Copyright 2002 by Roger Allen
    Everything else Copyright © CodeProject, 1999-2013
    Terms of Use
    Layout: fixed | fluid