?
Solved

Simple printing and pInfo->m_rectDraw size problem

Posted on 1998-09-22
6
Medium Priority
?
517 Views
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!
0
Comment
Question by:RJV
[X]
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
6 Comments
 

Expert Comment

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

Author Comment

by:RJV
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.

RJV

0
 

Expert Comment

by:t004024
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.
0
Industry Leaders: 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

by:RJV
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.

Thanks.
RJV

0
 
LVL 3

Accepted Solution

by:
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;
    else
        drawRect.left = physMargLeft;

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

    if( pixelMargTop > physMargTop )
        drawRect.top = pixelMargTop - physMargTop;
    else
        drawRect.top = physMargTop;

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

    return drawRect;
}


0
 

Author Comment

by:RJV
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!

RJV

      int nHeight,iNumPages,i,ii;
    TEXTMETRIC tm;
      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
      dataFont.CreateFont(295,0,0,0,FW_NORMAL,FALSE,FALSE,
            ANSI_CHARSET,OUT_TT_PRECIS,0,
            PROOF_QUALITY,DEFAULT_PITCH,FF_DONTCARE,"Arial");

      // 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
      pDC->GetTextMetrics(&tm);
      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
    pInfo->SetMaxPage(iNumPages);
    pDC->SetMapMode(iOldMode);

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

762 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