Simple printing and pInfo->m_rectDraw size problem

Posted on 1998-09-22
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.
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.


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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
bigDiff challenge 17 106
sum28 challenge 31 112
mapBully challenge 6 134
JQuery serialize and unserialize 8 84
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 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