How to implement scaled printing in an MFC/OLE container (138266)



The information in this article applies to:

  • The Microsoft Foundation Classes (MFC), when used with:
    • Microsoft Visual C++ for Windows, 16-bit edition 1.5
    • Microsoft Visual C++ for Windows, 16-bit edition 1.51
    • Microsoft Visual C++ for Windows, 16-bit edition 1.52
    • Microsoft Visual C++, 32-bit Editions 2.0
    • Microsoft Visual C++, 32-bit Editions 2.1
    • Microsoft Visual C++, 32-bit Editions 2.2
    • Microsoft Visual C++, 32-bit Editions 4.0
    • Microsoft Visual C++, 32-bit Editions 4.1
    • Microsoft Visual C++, 32-bit Enterprise Edition 4.2
    • Microsoft Visual C++, 32-bit Professional Edition 4.2
    • Microsoft Visual C++, 32-bit Enterprise Edition 5.0
    • Microsoft Visual C++, 32-bit Professional Edition 5.0
    • Microsoft Visual C++, 32-bit Enterprise Edition 6.0
    • Microsoft Visual C++, 32-bit Professional Edition 6.0
    • Microsoft Visual C++, 32-bit Learning Edition 6.0

This article was previously published under Q138266

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
   */ 
				
In the MFC Container sample, 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 Container and PRINT or PRINT PREVIEW is selected, the embedded object will appear too small.

Using Container 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 CContainerView::OnDraw(CDC* pDC)
{
    CContainerDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    
    // draw the OLE items from the list
    POSITION pos = pDoc->GetStartPosition();
    while (pos != NULL)
    {
        // draw the item
        CContainerItem* pItem = (CContainerItem*)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  = MulDiv(rect2.Width(),ixp, ixd);
            int height = MulDiv(rect2.Height(), iyp, iyd);
            rect2.left = MulDiv(rect2.left, ixp, ixd);
            rect2.top  = MulDiv(rect2.top,  ixp, ixd);
            rect2.right  = rect2.left + width;
            rect2.bottom = rect2.top + height;
        }
        
        pItem->Draw(pDC, rect2);
        if(!pDC->IsPrinting())
        {
            // draw the tracker over the item
            CRectTracker tracker;
            SetupTracker(pItem, &tracker);
            tracker.Draw(pDC);
        }
    }
}
				

Modification Type:MajorLast Reviewed:9/1/2005
Keywords:kbContainer kbGDI kbhowto kbprint KB138266 kbAudDeveloper