Hide

SftPrintPreview/OCX 1.0 - ActiveX Print Preview Control

Display
Print

PreviewPages Sample (C++)

This sample illustrates application-generated output.

The source code is located at C:\Program Files (x86)\Softelvdm\SftPrintPreview OCX 1.0\Samples\VC++\PreviewPages\PreviewPagesDlg.cpp or C:\Program Files\Softelvdm\SftPrintPreview OCX 1.0\Samples\VC++\PreviewPages\PreviewPagesDlg.cpp (on 32-bit Windows versions).

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

    // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);         // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon

    CRect rect;
    GetClientRect(&rect);
    m_Preview1.MoveWindow(0, 0, rect.Width(), rect.Height());

    m_vPrintPreview1 = m_Preview1.GetControlUnknown();
    ASSERT(m_vPrintPreview1 != NULL);

    // Use the RenderContent event to render pages
    m_vPrintPreview1->ContentProviderCallback(0, NULL, NULL);

    ShowWindow(SW_MAXIMIZE);
    UpdateWindow();

    MessageBox(_T("This example demonstrates application-generated output.\r\n\r\nIt prints one bitmap and 50 lines of text a total of 20 times.  "
        "The included source code illustrates how an application can print one page at a time, with an easy "
        "mechanism to keep track of restart positions."), _T("SftPrintPreview/OCX"));

    return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSftPrintPreviewDlg::OnPageSetupWantedSftPrintPreview1()
{
    m_vPrintPreview1->PageSetup((long) m_hWnd);
}

void CSftPrintPreviewDlg::OnCloseWantedSftPrintPreview1()
{
    OnOK();
}

void CSftPrintPreviewDlg::OnHelpWantedSftPrintPreview1(LPCTSTR HelpName)
{
    MessageBox(_T("Sorry, this sample doesn't offer a help file"));
}

void CSftPrintPreviewDlg::OnRenderContentSftPrintPreview1(long Function, long FAR* LastPage, VARIANT FAR* VisitedPageInfo)
{
    // This function is called by SftPrintPreview to print pages, to initialize and
    // terminate printing (to prepare and free resources) and for page positioning
    if (Function == renderSftPrintPreview_Init) {
        SftPrintPreviewRenderingInfo I = m_vPrintPreview1->GetRenderingInfo();
        int height = MulDiv(10, GetDeviceCaps((HDC)I.hDCPrinter, LOGPIXELSY), 72);// 10 point font
        m_PrintFont.CreateFont(- height, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
                OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_DONTCARE|DEFAULT_PITCH,
                _T("Arial"));
        _ASSERT(m_PrintFont.m_hObject);
    } else if (Function == renderSftPrintPreview_Term)
        m_PrintFont.DeleteObject();
    else if (Function == renderSftPrintPreview_1Page)
        PrintOnePage(VisitedPageInfo, LastPage);
}

#define LINETEXT TEXT("Softel vdm, Inc. - www.WindowsControls.com")

void CSftPrintPreviewDlg::PrintOnePage(VARIANT FAR* VisitedPageInfo, long FAR* LastPage)
{
    // Print one page (can occur multiple times)
    // RenderingInfo.CurrentPage has the absolute page # to print (0..n)
    // RenderingInfo.PreviousVisitedPageInfo contains the value you returned in
    // RenderingInfo.VisitedPageInfo when you finished printing the previous page
    // This allows you to simply restart where you left off.
    // The information you return in RenderingInfo.VisitedPageInfo is of type
    // object, so it can be a simple counter or even a complex object, with cached
    // information.

    // In this example we print 1 bitmap, then 50 lines of text,
    // 20 times

    // We use RenderingInfo.PreviousVisitedPageInfo to resume printing.
    // We simply start at 0 and add 1 for each bitmap or line we
    // printed. Using a simple formula, we can determine where we
    // left off.  In an application, the concept remains the same, but
    // instead of a simple counter, you could use RenderingInfo.VisitedPageInfo
    // to store more complex information.

    long counter = 0;
    RECT outRect;

    COleVariant varVisitedPageInfo(*VisitedPageInfo);

    SftPrintPreviewRenderingInfo RenderInfo = m_vPrintPreview1->GetRenderingInfo();
    outRect = RenderInfo.OutputRectDisplayPix;

    CDC* pDC = CDC::FromHandle((HDC)RenderInfo.hDC);

    if (RenderInfo.PreviousVisitedPageInfo.vt == VT_I4)
        counter = RenderInfo.PreviousVisitedPageInfo.lVal;// we left off here on the previous page

    // starting output position on this page
    int xPrinter, yPrinter;
    xPrinter = outRect.left;
    yPrinter = outRect.top;

    for ( ; ; )
    {
        if (counter >= (1+50)*20) // 1 bitmap, 50 lines of text, 20 times
        {
            // we have printed 1 bitmap and 50 lines 20 times, we're done
            *LastPage = RenderInfo.CurrentPage; // return that this is the last page
            break;
        }

        if ((counter % (50+1)) == 0)
        {
            // Print a bitmap

            // get bitmap size
            long wPrinterPix, hPrinterPix, wDispPix, hDispPix;
            m_vPrintPreview1->GetDDBSize((long)(HBITMAP)m_BitmapPage, RenderInfo.hDCPrinter, &wPrinterPix, &hPrinterPix, &wDispPix, &hDispPix);
            // make sure entire image fits on remaining page
            if (yPrinter + hPrinterPix > outRect.bottom)
                break;
            // print bitmap, horizontally centered
            m_vPrintPreview1->PrintDDB((long)(HBITMAP)m_BitmapPage, (long) pDC->m_hDC, xPrinter +((outRect.right-outRect.left)-wPrinterPix)/2,
                yPrinter, wPrinterPix, hPrinterPix, wDispPix, hDispPix);
            yPrinter += hPrinterPix; // next available space
        }
        else
        {
            // Print line
            // get text size
            RECT rect;

            pDC->SelectObject(m_PrintFont);
            pDC->SetTextColor(RGB(0,0,0));

            // get text size
            SetRectEmpty(&rect);
            pDC->DrawText(LINETEXT, &rect, DT_LEFT| DT_TOP | DT_SINGLELINE | DT_CALCRECT);

            // make sure text fits on remaining page
            if (yPrinter + (rect.bottom-rect.top) > outRect.bottom)
                break;

            RECT r = outRect;
            r.top = yPrinter;
            pDC->DrawText(LINETEXT, &r, DT_CENTER| DT_TOP | DT_SINGLELINE);

            yPrinter += rect.bottom-rect.top; // next available space
        }

        ++counter;  // one more item done
    }
    varVisitedPageInfo = counter;// we left off here

    VariantCopy(VisitedPageInfo, varVisitedPageInfo);
}

BEGIN_EVENTSINK_MAP(CSftPrintPreviewDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CSftPrintPreviewDlg)
	ON_EVENT(CSftPrintPreviewDlg, IDC_SFTPRINTPREVIEW1, 8 /* CloseWanted */, OnCloseWantedSftPrintPreview1, VTS_NONE)
	ON_EVENT(CSftPrintPreviewDlg, IDC_SFTPRINTPREVIEW1, 6 /* HelpWanted */, OnHelpWantedSftPrintPreview1, VTS_BSTR)
	ON_EVENT(CSftPrintPreviewDlg, IDC_SFTPRINTPREVIEW1, 7 /* PageSetupWanted */, OnPageSetupWantedSftPrintPreview1, VTS_NONE)
	ON_EVENT(CSftPrintPreviewDlg, IDC_SFTPRINTPREVIEW1, 14 /* RenderContent */, OnRenderContentSftPrintPreview1, VTS_I4 VTS_PI4 VTS_PVARIANT)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()


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