Paginate and Print MSFlexGrid Content the Easy Way

Friday Feb 7th 2003 by Jordi Duatis
Share:

Paginate and print according to the grid content is not a dummy task. See how to implement this easily with the PrintGrid class. It will paginate the MSFlexGrid content for you and will print every page accordingly.


This article was contributed by Jordi Duatis.

Environment: VC6

MSFlexGrid is a useful control to present data in a Grid, but what happens when this content has to be printed? Here I present a class, PrintGrid, that paginates and prints the grid content. It uses a second Grid control and must be additionally inserted in your CFormView dialog resource or in your dialog.

The instructions are as follows:

  • Add a second MSFlexGrid control to your form or dialog.
  • Change the member in your view or dialog class from CMSFlexGrid to CPrintGrid* m_pxPrintGrid.
  • Change the DDX command: DDX_Control(pDX, IDC_MSFLEXGRID2, *m_pxPrintGrid).
  • Instantiate the control in your constructor: m_pxPrintGrid = new CPrintGrid().
  • Override the operation OnPreparePrinting and OnPrint as follows:
  • BOOL MyFormView::OnPreparePrinting(CPrintInfo* pInfo)
    {
      // Calculate pages for all grid data
      CPrintDialog xPrintDlg(FALSE);
      CDC xDC;
      xPrintDlg.GetDefaults();
      xDC.Attach(xPrintDlg.GetPrinterDC());
      m_pxPrintGrid->Paginate(&xDC, m_pxGrid);
      xDC.Detach();
    
      // Update the number of pages according to DC size
      pInfo->m_nNumPreviewPages = m_pxPrintGrid->GetNumPages();
      pInfo->SetMinPage(1);
      pInfo->SetMaxPage(m_pxPrintGrid->GetNumPages());
    
      // Display printer setup dialog and initialize CPrintInfo
      if (!DoPreparePrinting(pInfo))
      {
        return FALSE;
      }
      // call parent
      return CFormView::OnPreparePrinting(pInfo);
    }
    
    void MyFormView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
    {
      CString sTitle;
      sTitle = "My App title";
      m_pxPrintGrid->PrintPage(pDC, pInfo->m_nCurPage, m_pxGrid,
                               sTitle);
      // Call parent
      CFormView::OnPrint(pDC, pInfo);
    }
    

Enjoy it! I hope it will be useful.

Downloads

Download Class Code: PrintGrid.zip - 4 Kb

Share:
Home
Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved