Hide

SftPrintPreview/DLL 2.0 - Print Preview Control for C/C++

Display
Print

Using the CSftPrintPreview_View Class

This topic describes the steps required to use the provided CSftPrintPreview_App, CSftPrintPreview_Main, CSftPrintPreview_View classes to extend any CView-derived class in order to add Print Preview support.

Any window can take advantage of Print Preview features offered by SftPrintPreview/DLL. A CView-derived class can be extended to offer Print Preview by using the predefined MFC classes CSftPrintPreview_App, CSftPrintPreview_Main, CSftPrintPreview_View. Even applications that do not use the Document/View architecture can take advantage of SftPrintPreview/DLL.

Before adding Print Preview support to a window or view, make sure to follow all steps outlined in chapter Using C++/MFC.

CSftPrintPreview_App Class

The first step required to add SftPrintPreview/DLL support to a window or view in your application is to derive the application object from CSftPrintPreview_App, instead of CWinApp. Essentially, this means replacing all references to CWinApp with CSftPrintPreview_App. This can be accomplished by globally editing the project files.

The CSftPrintPreview_App class is used by the CSftPrintPreview_Main and CSftPrintPreview_View classes to retrieve application printer settings. Because MFC retains ownership of the current printer information, the CSftPrintPreview_App class is needed to allow SftPrintPreview/DLL to access and update the current printer information.

CSftPrintPreview_Main Class

The CView-derived window that should receive Print Preview support is extended using the CSftPrintPreview_Main class, using multiple inheritance:

class CChildView : public CView, public CSftPrintPreview_Main
{
    ... member functions
}

The window can now take advantage of the Print Preview implementation available in the CSftPrintPreview_Main class, by adding menu handlers or similar mechanisms to invoke the Print Preview window:

BEGIN_MESSAGE_MAP(CChildView, CWnd)
 ON_COMMAND(ID_FILE_PRINTPREVIEW, OnFilePrintpreview)
END_MESSAGE_MAP()

void CChildView::OnFilePrintpreview()
{
    FilePrintPreview(this, RUNTIME_CLASS(CChildPrintPreview));
}

Implementing a CSftPrintPreview_View-Derived Class

CSftPrintPreview_View implements a complete Print Preview class. By deriving a class from this base class, an application merely needs to add any desired menu handlers and can optionally customize the control.

Example

ChildPrintPreview.h

class CChildPrintPreview : public CSftPrintPreview_View
{
public:
    virtual CString GetOutputName() { return _T("Sample Output"); }

    virtual void CustomizeControl(LPSFTPRINTPREVIEW_CONTROL lpCtl);

    afx_msg void OnNotifyHelpReflect(NMHDR * pNotifyStruct, LRESULT* result);

    DECLARE_MESSAGE_MAP()
    DECLARE_DYNCREATE(CChildPrintPreview)
};

ChildPrintPreview.cpp

#include "StdAfx.h"

IMPLEMENT_DYNCREATE(CChildPrintPreview, CSftPrintPreview_View)

BEGIN_MESSAGE_MAP(CChildPrintPreview, CSftPrintPreview_View)
    // ON_COMMAND(ID_FILE_PRINT, OnFilePrintWithDialog)
    // ON_COMMAND(ID_FILE_PRINTSETUP, OnFilePrintSetup)
    ON_COMMAND(ID_FILE_CLOSE, OnPreviewClose)
    ON_NM_SFTPRINTPREVIEW_HELP_CODE_REFLECT(OnNotifyHelpReflect)
END_MESSAGE_MAP()

afx_msg void CChildPrintPreview::OnNotifyHelpReflect(NMHDR * pNotifyStruct, LRESULT* lResult)
{
    MessageBox(_T("Sorry, this sample application doesn't include online help."), _T("SftPrintPreview/DLL"), MB_OK);
    *lResult = 0;
}

void CChildPrintPreview::CustomizeControl(LPSFTPRINTPREVIEW_CONTROL lpCtl)
{
    lpCtl->numPageRows = 1; // default to 1x2 pages
    lpCtl->numPageGroups = 2;
    lpCtl->zoom = 0; // start out with multiple pages

    lstrcpy(lpCtl->szHeaderRight, TEXT("SftPrintPreview/DLL Sample"));
    lstrcpy(lpCtl->szHeaderRight, TEXT("SftPrintPreview/DLL Sample"));
    lstrcpy(lpCtl->szFooterLeft, TEXT("www.softelvdm.com"));
}

Output Source

Finally, the actual output needs to be generated by the CView-derived window using one of the methods described in the topics Application-Generated Output, SftTree/DLL and SftTree/OCX or RichEdit Control.


Last Updated 08/13/2020 - (email)
© 2024 Softel vdm, Inc.