Simple printing and pInfo->m_rectDraw size problem

I'm trying to do what is supposed to be simple: print! Suggestions towards that goal are greatly appreciated. At the moment I've managed to do a reasonable print preview in an SDI app. The problem is to figure out the number of pages, which requires getting valid info out of pInfo->m_rectDraw.

Thansk for your help!
Who is Participating?
Use the following code but sorry the comments are in french.

The top four long variables can be substituted for a margin you want in inches. You can offer the user to specify it or leave them at 0.

The rect returned is the PRINTABLE rect, and that is the keyword. Every printer differs in their use of the paper, the margins, if any are substracted from the real printer offset from left, top, etc.

You get a rect in pixels, use the DPIs, ie GetDeviceCaps(LOGPIXELX), to convert it if need be.

CRect CPrintManager::GetPrintableRect(CDC *pDC)
    CRect   drawRect;
    long    leftMargin   = 0;   // Marge en pouces
    long    rightMargin  = 0;   // Marge en pouces
    long    topMargin    = 0;   // Marge en pouces
    long    bottomMargin = 0;   // Marge en pouces
    int     horzRes;  // Pixels dans le printable area (horizontale)
    int     vertRes;  // Pixels dans le printable area (verticale)
    int     horzPhysRes; // Largeur de la page en pixels
    int     vertPhysRes; // Hauteur de la page en pixels
    int     physMargLeft;  // Marge non-imprimable en pixel (gauche)
    int     physMargTop;   // Marge non-imprimable en pixel (top)
    int     physMargRight; // Marge non-imprimable en pixel (right)
    int     physMargBottom;// Marge non-imprimable en pixel (bottom)
    int     pixelMargLeft;  // Marge de l'utilisateur en pixels
    int     pixelMargRight;// Marge de l'utilisateur en pixels
    int     pixelMargTop;// Marge de l'utilisateur en pixels
    int     pixelMargBottom;// Marge de l'utilisateur en pixels
    POINT   pt;

    // On retrouve la précision DPI de l'imprimante
    int     xDpi = pDC->GetDeviceCaps( LOGPIXELSX );
    int     yDpi = pDC->GetDeviceCaps( LOGPIXELSY );

    // On retrouve aussi la taille du "printable area"
    horzRes = pDC->GetDeviceCaps(HORZRES);
    vertRes = pDC->GetDeviceCaps(VERTRES);
    // Ensuite on trouve, en pixels, la taille de la feuille complète
    pDC->Escape(GETPHYSPAGESIZE, 0, NULL, &pt);
    horzPhysRes = pt.x;
    vertPhysRes = pt.y;
    // L'imprimante ne peut fournir que l'offset, du coin en haut à gauche
    // de la page où le printable area débute. Il faut calculer l'offset
    // droit et bas des marges désirées.
    pDC->Escape(GETPRINTINGOFFSET, 0, NULL, &pt);
    physMargLeft = pt.x;
    physMargTop = pt.y;
    physMargRight = horzPhysRes - horzRes - physMargLeft;
    physMargBottom = vertPhysRes - vertRes - physMargTop;
    // On a nos marges en pouces, on les converti en pixels et on s'ajuste
    // avec les marges physiques de l'imprimante.
    pixelMargLeft = (int)((leftMargin * xDpi) + 1);
    pixelMargRight = (int)((rightMargin * xDpi) + 1);
    pixelMargTop = (int)((topMargin * yDpi) + 1);
    pixelMargBottom = (int)((bottomMargin * yDpi) + 1);

    // On doit donc définir le rectangle avec les marges que l'utilisateur
    // désire en tenant compte des marges physiques de l'imprimante. On ne peut
    // bien sûr pas descendre plus bas que ces marges physiques.
    if( pixelMargLeft > physMargLeft )
        drawRect.left = pixelMargLeft-physMargLeft;
        drawRect.left = physMargLeft;

    if( pixelMargRight > physMargRight )
        drawRect.right = horzPhysRes - pixelMargRight - physMargRight;
        drawRect.right = horzPhysRes - physMargRight - physMargLeft;

    if( pixelMargTop > physMargTop ) = pixelMargTop - physMargTop;
    else = physMargTop;

    if( pixelMargBottom > physMargBottom )
        drawRect.bottom = vertPhysRes - pixelMargBottom - physMargBottom;
        drawRect.bottom = vertPhysRes - physMargBottom - physMargTop;

    return drawRect;

You mean that you cannot estimate the number of pages out from the m_rectDraw member? (Do I get you right?)
RJVAuthor Commented:
The problem is that pInfo->m_rectDraw.bottom is the same as  pInfo-> -- as is, for that matter, .right and .left. Thus, even though I have the character's height I cannot get the number of lines as I cannot get the height of the printable area.


Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

In which event are U trying to access it??? If U are trying to access it in OnPreparePrinting/OnBeginPrinting, I do not know if that is possible. To get the pagewidth and pageheight, I use the CDC's GetDeviceCaps function with the HORZRES and VERTRES in the OnBeginPrinting.
RJVAuthor Commented:
Yes, I am trying to access in OnPreparePrinting/OnBeginPrinting, as some examples I've looked at. However, in those the size is correct, versus wrong in my case. Thus, what has been done to get the size right is the question. Or else, another solution.

How does GetDeviceCaps convert to twips? Looking at the function, HORZRES and VERTRES gives the width and height in pixels. How does that relate to a page size?

It ought to make matters easier if you could show how you got or get there.


RJVAuthor Commented:
Thanks for your effort. The French, BTW, is not a problem.

After I posted here I did some further checking out. Looking through the books "Inside Visual C++" and "MFC Internals" I discovered that as of Win95 and NT4 the pixels had been directly associated to paper size, irrespective of the font size. I also revisited the MFC mailing list and followed the thread you gave of GetDeviceCaps(VERTRES), seeing as VERTRES could now be associated more directly to TWIPS.

The outcome is the function that follows, inserted in the OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo) function. This seems a bit simpler than your suggestion above. The goal was to adjust the printable area to the font used too.

I hope this also helps you!


      int nHeight,iNumPages,i,ii;
      CSize sizeXY;

      // Get the count of items in the list control to print
      cnt = m_listCtrl.GetItemCount();
      if (cnt <= 0)
            return;            // forget it; nothing to print!

      // Create the font here to be able to find the printed area size

      // Get the basic sizes of the page
      iOldMode = pDC->SetMapMode(MM_TWIPS); = pDC->GetDeviceCaps(HORZRES); = pDC->GetDeviceCaps(VERTRES);
      pDC->SetViewportExt (,;

      // Get the line height to find the lines per page
      nHeight = tm.tmHeight + tm.tmExternalLeading;

      // Figure out how many lines to print and how many pages
      blkLines = 5;               // 5 lines per block
      i = / nHeight;   // find lines per page
      if (i!=0)
            blkSz = i / blkLines;      // find how many blocks per page
            iNumPages = cnt / blkSz; // now the number of pages
            ii = cnt % blkSz;
            if (ii > 0)      // anything left over to print?
                  iNumPages++;      // if yes, another page

      // All set, so set the number of pages to print

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.