Printing Long CStrings to Multiple Pages | CodeGuru

Printing Long CStrings to Multiple Pages

Environment: VC6 SP4, NT4 SP3 This is an very easy example how to print a (long) CString buffer ( CStringList ) on several pages and how to get the Print Preview to work properly. I don’t know if this is the right way to do this but I have looked everywhere but haven’t found a […]

Written By
CodeGuru Staff
CodeGuru Staff
Oct 17, 2001
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment: VC6 SP4, NT4 SP3

This is an very easy example how to print a (long) CString buffer ( CStringList ) on several pages and how to get the Print Preview to work properly. I don’t know if this is the right way to do this but I have looked everywhere but haven’t found a good solution. I havent had time to figure out how to set the pInfo->SetMaxPage(nMaxPage); parameter yet. Maybe somebody could post it in the article.

void CPrintExampleView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
  CRect strRect(0,0,0,0);
  CRect rcCalcText(0,0,0,0);
  int nStringheight=0,nStringwidth=0, nRowSpace = 0;
  TEXTMETRIC tm;
  pDC->SetMapMode(MM_TEXT); // Sets the mapping mode
  // Set the start of the Print Position
  nPrintPos = m_PrintStrings.GetHeadPosition();
  int nCurPage = (pInfo->m_nCurPage);
  for ( int c=1; c <= nCurPage; c++)
  {
    int iY = 300;  // Initial Y coordinate
    for( nPrintPos; nPrintPos != NULL && (iY < m_iVerticalSize ); )
    {
      pDC->GetTextMetrics(&tm);
      CString strTmp = m_PrintStrings.GetAt( nPrintPos );
      // Space between rows depending on the fontsize.
      nRowSpace = tm.tmExternalLeading;
      // Calc the Height of the String, that can different
      // if using newline etc.
      pDC->DrawText(strTmp, rcCalcText, DT_CALCRECT);
      nStringheight = rcCalcText.Height();
      strRect.left=200;
      strRect.right = pDC->GetDeviceCaps(HORZRES);
      strRect.top=iY;
      strRect.bottom = strRect.top + nStringheight;

      //Draw the tabbed text to a specific position
      CString tab1,tab2;

      // Instead of \t I choosed to control my tab position myself.
      int tabPos = strTmp.Find(“\t”);
      if ( tabPos != -1 )
      {
        tab1 = strTmp.Left(tabPos);
        tab2 = strTmp.Right(strTmp.GetLength()-tabPos-1);
            //  AfxMessageBox(tab2);
      }
      else
        tab1 = strTmp;

      // Draw the text..
      if ( nCurPage == c )
      {
        // Attach header font to the header text.
        pDC->SelectObject(&m_HeadingFont);
        PrintPageHeader(pDC,pInfo);

        // Attach Body font to the body text.
        pDC->SelectObject(&m_BodyFont2);
        pDC->DrawText( tab1,
                       strRect,
                       DT_WORDBREAK | DT_EXPANDTABS);
        strRect.left=strRect.right/6*2;
        pDC->SelectObject(&m_BodyFont);
        pDC->DrawText( tab2,
                       strRect,
                       DT_WORDBREAK | DT_EXPANDTABS);
      }
      m_PrintStrings.GetNext( nPrintPos );

      // Calculate the next start position
      int x = strRect.bottom;
      iY = ( nRowSpace + x);
    }
    if ((iY > m_iVerticalSize) && nCurPage == c)
      break;
  }
}

Downloads

Download demo project – 21 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.