Improve company productivity with a Business Account.Sign Up


Simple printing and pInfo->m_rectDraw size problem

Posted on 1998-09-22
Medium Priority
Last Modified: 2013-11-19
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!
Question by:RJV

Expert Comment

ID: 1322448
You mean that you cannot estimate the number of pages out from the m_rectDraw member? (Do I get you right?)

Author Comment

ID: 1322449
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.



Expert Comment

ID: 1322450
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.
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!


Author Comment

ID: 1322451
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.



Accepted Solution

plaroche earned 560 total points
ID: 1322452
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;


Author Comment

ID: 1322453
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


Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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.

Join & Write a Comment

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Watch the video of Kernel Migrator for SharePoint, which demonstrate the process easily of migration from SharePoint to SharePoint, OneDrive for Business & Google Drive servers, Public Folder to SharePoint, File Server to SharePoint. The tool has va…

608 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question