Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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->m_rectDraw.top -- 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.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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 )
        drawRect.top = pixelMargTop - physMargTop;
        drawRect.top = 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);
      sizeXY.cx = pDC->GetDeviceCaps(HORZRES);
      sizeXY.cy = pDC->GetDeviceCaps(VERTRES);
      pDC->SetViewportExt (sizeXY.cx,sizeXY.cy);

      // 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 = sizeXY.cy / 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

618 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