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 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


Comments

  • Patrik and Richard :Thanks

    Posted by Legacy on 11/06/2003 12:00am

    Originally posted by: vani

    Using your codes i could create a successful Print feauture

    Reply
  • coloumns

    Posted by Legacy on 11/03/2003 12:00am

    Originally posted by: vanisri

    How can i make this projects result to be displayed in 2 coloumns instead of only one ..in same page?

    Reply
  • JdbTable Printing Components

    Posted by Legacy on 07/19/2003 12:00am

    Originally posted by: Nair

    i m getting the problem to print the values of JdbTable so any body can help me in this case also these table values may exceed to the multiple pages ........tell me the solution with code material thnaks nasir

    Reply
  • could this be used other ways?

    Posted by Legacy on 06/20/2003 12:00am

    Originally posted by: yimingjiang

    in the demo,you use MM_TEXT,can i use MM_LOENGLISH or other
    mode?

    Reply
  • 1

    Posted by Legacy on 04/22/2003 12:00am

    Originally posted by: Kohinoor


    Hi,

    Iam new to MFC & Iam virtually doing my first program using MFC.

    My aim was to get a window & draw some text on it.I selected the project type-MFC AppWizard(exe).It generated me custom classes derived from C_winApp,CDocument,CView & CFrameWnd.

    On the onDraw Function I used a "Text out" Function to call the Text.

    CderivedView is a class derived from CView.

    void CderivedView::OnDraw(CDC* pDC)
    {
    pDC->SelectObject(&newPen);
    pDC->MoveTo(10,10);
    pDC->LineTo(110,10);
    pDC->SelectStockObject(BLACK_PEN);
    pDC->SetBkColor(RGB(255,0,0));
    pDC->TextOut(10,20 ,"Hello World!");
    }

    The way it is working is fine with me.Now What I need is,Iam in a win32 console application which has a main(). & I want call the application FameWork's some function so that I can pass some values to the "OnDraw" Function, such as in which position to draw the text.How Can I make this connection?

    I know that when Iam using MFC AppWizard(exe) Project Type,the application framework internally calls functions for displaying the window etc.I would like to have all the similar functionalities when Iam calling from main() The Application FrameWork Function.

    my point is I want to pass some values from my console main so that it reaches the "OnDraw"Function and then I could pass these values to the Text Out Function.

    How can it be Done?.Could any one show it with a code snippet.
    Thanks...

    Reply
  • Zoom in

    Posted by Legacy on 02/12/2003 12:00am

    Originally posted by: Jorgen

    I have found that if you click the "zoom in" button until
    
    the page does not fit on the screen and then click on
    the "next page" or "prev page" and if you then use the
    slider up/down the page splits like this:


    ***********
    * *
    * *
    * * (This section is on the screen!)
    * *
    * *
    * *
    ***********

    If I Minimize the window and then maximize it again it is
    ok again until I press the next page or prev page. Is it
    more than I that have this problem? Is there someone that
    have a solution for it?

    /Jorgen Jonsson

    Reply
  • SetMaxPage

    Posted by Legacy on 10/30/2002 12:00am

    Originally posted by: John T. Edward

    At the bottom of OnPrint, add the following:

    if( !nPrintPos )
    {
    pInfo->SetMaxPage(nCurPage);
    }

    I am sure there is an easy way to calculate it beforehand based on the number of Strings in the list, but I am not smart enough to do it.

    Reply
  • Print range

    Posted by Legacy on 12/06/2001 12:00am

    Originally posted by: Richard

    If you leave "All" as selected when specifying a print range, you'll notice the program goes on to print quite a few pages....I got up to 1100 before I cancelled it!!

    I'm not sure why this occurs at this point, but I'll try to remember to post a solution here if someone else doesn't beat me to it.

    Richard

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds