#include "stdafx.h"

CPreviewDialog::CPreviewDialog(CWnd* pParent /*=NULL*/)
    : CDialog(CPreviewDialog::IDD, pParent)
{
    m_pEdit = NULL;
    //{{AFX_DATA_INIT(CPreviewDialog)
        // NOTE: the ClassWizard will add member initialization here
    //}}AFX_DATA_INIT
}

void CPreviewDialog::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);

    DDX_Control(pDX, IDC_PREVIEW, m_Preview);

    //{{AFX_DATA_MAP(CPreviewDialog)
        // NOTE: the ClassWizard will add DDX and DDV calls here
    //}}AFX_DATA_MAP
}

int CPreviewDialog::DoModal(CRichEditCtrl* pEditControl)
{
    m_pEdit = pEditControl;
    int rc = (int) CDialog::DoModal();
    m_pEdit = NULL;
    return rc;
}

afx_msg void CPreviewDialog::OnStartPrinting()
{
    SftPrintPreview_Print(m_Preview, m_hWnd);
}

afx_msg void CPreviewDialog::OnPreviewClose(NMHDR* pnmh, LRESULT* result)
{
    NM_SFTPRINTPREVIEW_CLOSE* pNotifyStruct = (NM_SFTPRINTPREVIEW_CLOSE*) pnmh;
    SendMessage(WM_COMMAND, IDCANCEL);
}

afx_msg void CPreviewDialog::OnPreviewHelp(NMHDR* pnmh, LRESULT* result)
{
    NM_SFTPRINTPREVIEW_HELP* pNotifyStruct = (NM_SFTPRINTPREVIEW_HELP*) pnmh;
    MessageBox(_T("Sorry, this sample application doesn't include online help."), _T("SftPrintPreview/DLL"), MB_OK|MB_TASKMODAL);
}

afx_msg void CPreviewDialog::OnPreviewPageSetup(NMHDR* pnmh, LRESULT* result)
{
    NM_SFTPRINTPREVIEW_PAGESETUP* pNotifyStruct = (NM_SFTPRINTPREVIEW_PAGESETUP*) pnmh;
    SftPrintPreview_PageSetup(m_Preview, m_hWnd);
}

void CPreviewDialog::InitPreviewWindow()
{
    // retrieve current control settings
    SFTPRINTPREVIEW_CONTROL Ctl;
    Ctl.cbSize = sizeof(SFTPRINTPREVIEW_CONTROL);
    if (!m_Preview.GetControlInfo(&Ctl)) {
        _ASSERT(0); // the SFTPRINTPREVIEW_CONTROL structure's cbSize member wasn't initialized properly
        return;
    }

    // set all desired options
    Ctl.numPageRows = 1;                // default to 1x2 pages
    Ctl.numPageGroups = 2;
    Ctl.zoom = 0;                       // start out with multiple pages
    Ctl.fCenterOnClick = FALSE;
    Ctl.fDragPage  = TRUE;
    Ctl.iZoomStyle = SFTPRINTPREVIEW_ZOOMSTYLE_BOTHBUTTONS_EXACT;

    lstrcpy(Ctl.szOutputName, TEXT("SftPrintPreview RichEdit Output")); // job name when printing
    lstrcpy(Ctl.szHeaderRight, TEXT("SftPrintPreview/DLL RichEdit Preview Sample"));
    lstrcpy(Ctl.szFooterLeft, TEXT("www.softelvdm.com"));

    if (!m_Preview.SetControlInfo(&Ctl)) {
        int errorValue = Ctl.errorValue;
        _ASSERT(0); // an error occurred, check errorValue for error code
        return;
    }
}

void CPreviewDialog::DefineContents()
{
    SFTPRINTPREVIEW_CONTROL Ctl;

    Ctl.cbSize = sizeof(SFTPRINTPREVIEW_CONTROL);
    if (!m_Preview.GetControlInfo(&Ctl)) {
        _ASSERT(0); // the SFTPRINTPREVIEW_CONTROL structure's cbSize member wasn't initialized properly
        return;
    }

    // define desired contents to preview/print
    Ctl.lpDrawPageWorkArea = (SFTPRINTPREVIEW_DWORD_PTR) NULL;// no workarea needed
    Ctl.lpDrawInfoProc = SftPrintPreview_RenderRicheditWindow_Callback;// callback needed for RichEdit control
    Ctl.hwndContent = *m_pEdit;          // We'll use this window
    Ctl.hwndData = NULL;                // no data window (content window is data window)
    Ctl.iContentSizing = SFTPRINTPREVIEW_CONTENTSIZING_SHRINKTOFIT;// shrink to fit on page

    if (!m_Preview.SetControlInfo(&Ctl)) {
        int errorValue = Ctl.errorValue;
        _ASSERT(0); // an error occurred, check errorValue for error codes
        return;
    }
}

BOOL CPreviewDialog::OnInitDialog() 
{
    CDialog::OnInitDialog();

    ModifyStyle(0, WS_SIZEBOX);
    ShowWindow(SW_SHOWMAXIMIZED);
    
    InitPreviewWindow();
    DefineContents();

    // changing window styles above has suppressed setfocus call
    m_Preview.SetFocus();
    return FALSE;
}

void CPreviewDialog::OnSize(UINT nType, int cx, int cy) 
{
    CDialog::OnSize(nType, cx, cy);

    if (m_Preview.m_hWnd) {
        RECT rect;
        GetClientRect(&rect);
        m_Preview.MoveWindow(&rect, TRUE);
    }
}

BEGIN_MESSAGE_MAP(CPreviewDialog, CDialog)
    //{{AFX_MSG_MAP(CPreviewDialog)
    ON_WM_SIZE()
    //}}AFX_MSG_MAP
    ON_SFTPRINTPREVIEWN_STARTPRINTING(IDC_PREVIEW, OnStartPrinting)
    ON_NM_SFTPRINTPREVIEW_CLOSE_CODE(IDC_PREVIEW, OnPreviewClose)
    ON_NM_SFTPRINTPREVIEW_HELP_CODE(IDC_PREVIEW, OnPreviewHelp)
    ON_NM_SFTPRINTPREVIEW_PAGESETUP_CODE(IDC_PREVIEW, OnPreviewPageSetup)
END_MESSAGE_MAP()


Feedback / comments / error reports for this topic
© 2016 - Softel vdm, Inc. - www.softelvdm.com