HOWTO: Implement Scaled Printing in an MFC/OLE Container

Last reviewed: February 17, 1998
Article ID: Q138266
The information in this article applies to:
  • The Microsoft Foundation Classes (MFC) included with:

        - Microsoft Visual C++ for Windows, versions 1.5, 1.51, 1.52
        - Microsoft Visual C++, 32-bit Edition, versions 2.0, 2.1, 2.2,
          4.0, 4.1, 4.2, 5.0
    

SUMMARY

This article shows by example how to implement scaling properly.

Printing an embedded object from an AppWizard-generated OLE container application may cause the printout of the embedded object to appear too small.

The developer of an OLE container must implement the code to scale an embedded object properly from the screen device to the printer device. If only the screen device is considered, the embedded object will appear too small on a printout.

MORE INFORMATION

Sample Code

   /* Compile options needed: none
   */

The MFC sample Contain, step 2 implements a container that does not consider the difference in resolution between the screen device and the printer device. If, for instance, Scribble, step 7 is embedded in Contain and PRINT or PRINT PREVIEW is selected, the embedded object will appear too small.

Using Contain as an example of MFC container that doesn't support scaling, the following code can be added to CContainView::OnDraw() to implement proper scaling:

   void CContainView::OnDraw(CDC* pDC)
   {
           CContainDoc* pDoc = GetDocument();
           ASSERT_VALID(pDoc);
   
           // draw the OLE items from the list
           POSITION pos = pDoc->GetStartPosition();
           while (pos != NULL)
           {
                  // draw the item
                  CCntrItem* pItem = (CCntrItem*)pDoc->GetNextItem(pos);
   
                   // copy the client items rect, which will be scaled
                   CRect rect2(pItem->m_rect);
                   if(pDC->IsPrinting())
                   {
                           // get the printer's pixel resolution
                           int ixp=pDC->GetDeviceCaps(LOGPIXELSX);
                           int iyp=pDC->GetDeviceCaps(LOGPIXELSY);
   
                   // get the current window's resolution
                           CDC* pddc = GetDC();
                           int ixd=pddc->GetDeviceCaps(LOGPIXELSX);
                           int iyd=pddc->GetDeviceCaps(LOGPIXELSY);
   
                   // scale rect2 up by the ratio of the resolutions
                           int width=rect2.Width()*(ixp/ixd);
                           int height=rect2.Height()*(ixp/ixd);
                           rect2.right  = rect2.left +
                                       MulDiv(rect2.Width(),ixp, ixd);
                           rect2.bottom = rect2.top +
                                      MulDiv(rect2.Height(), iyp, iyd);
                   }
   
                   pItem->Draw(pDC, rect2);
          if(!pDC->IsPrinting())
          {
                   // draw the tracker over the item
                   CRectTracker tracker;
                   SetupTracker(pItem, &tracker);
                   tracker.Draw(pDC);
                   }
           }
   }
Keywords          : MfcOLE
Technology        : kbMfc kbole
Version           : WINDOWS:1.5,1.51,1.522;Winnet:2.0,2.1,2.2,4.0,4.1,4.2,5.0
Platform          : NT WINDOWS
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 17, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.