home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2002 March / PCWMAR02.iso / software / turbocad / v8trial / TurboCADv8ProfessionalNoReg.exe / Data.Cab / F39412_PrintDr.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-16  |  5.1 KB  |  208 lines

  1. /******************************************************************/
  2. /*                                                                */
  3. /*                      TurboCAD for Windows                      */
  4. /*                   Copyright (c) 1993 - 2001                    */
  5. /*             International Microcomputer Software, Inc.         */
  6. /*                            (IMSI)                              */
  7. /*                      All rights reserved.                      */
  8. /*                                                                */
  9. /******************************************************************/
  10.  
  11. // PrintDr.cpp : Implementation of CPrintDr
  12. #include "stdafx.h"
  13. #include "PrintCAD.h"
  14. #include "PrintDr.h"
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CPrintDr
  18.  
  19. COleVariant varMissing((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
  20. COleVariant varTrue(1L);
  21. COleVariant varFalse(0L);
  22.  
  23. #define RC_REQURE    RC_BITBLT | RC_BITMAP64 /*| RC_STRETCHBLT */
  24.  
  25.  
  26. STDMETHODIMP CPrintDr::PrintAll(LPDISPATCH WhichDrawing, long pHDC)
  27. {
  28.     AFX_MANAGE_STATE(AfxGetStaticModuleState())
  29.     // TODO: Add your implementation code here
  30.     IDrawing* pIDrawing = NULL;
  31.     pIDrawing = (IDrawing*) WhichDrawing;
  32.     m_sDrawMode = DM_BLACK;
  33.     m_pView = NULL;
  34.     
  35.     if (pIDrawing == NULL)
  36.         return FALSE;
  37.  
  38.  
  39.     HDC hPrintDC = (HDC) pHDC;
  40.     if (hPrintDC != NULL)
  41.     {
  42.         int caps = ::GetDeviceCaps(hPrintDC, RASTERCAPS);
  43.  
  44.         if ((caps & (RC_REQURE)) != (RC_REQURE))
  45.         {
  46.             CString cstrMessage;
  47.             cstrMessage.Format(_T("Sorry, can't use your printer.\n \
  48.                                 Printer raster capabilities is %x"), caps);
  49.             AfxMessageBox(cstrMessage);
  50.             return FALSE;
  51.         }
  52.  
  53.         BOOL bMonochrom = FALSE;
  54.         // Setup printer DC
  55.         CDC *pPrintDC = new CDC;
  56.         pPrintDC->Attach(hPrintDC);
  57.  
  58.         pPrintDC->m_bPrinting = TRUE;
  59.  
  60.         HDC hScreenIC = CreateIC(_T("Display"), NULL, NULL, NULL);
  61.         int logpxScreen = ::GetDeviceCaps(hScreenIC, LOGPIXELSX);
  62.         int logpyScreen = ::GetDeviceCaps(hScreenIC, LOGPIXELSY);
  63.         DeleteDC(hScreenIC);
  64.  
  65.         int iOldMode = pPrintDC->SetMapMode(MM_TEXT);
  66.  
  67.         int logpxPage = pPrintDC->GetDeviceCaps(LOGPIXELSX);
  68.         int logpyPage = pPrintDC->GetDeviceCaps(LOGPIXELSY);
  69.  
  70.         CSize cszOldPage = pPrintDC->ScaleWindowExt(logpxPage, logpxScreen,
  71.                                                     logpyPage, logpyScreen);
  72.  
  73.         int cxPage = (int)(pPrintDC->GetDeviceCaps(HORZRES) * 0.9 + 0.5);
  74.         int cyPage = (int)(pPrintDC->GetDeviceCaps(VERTRES) * 0.9 + 0.5);
  75.  
  76.         CRect rcPage(0, 0, cxPage, cyPage);
  77.  
  78.         CSize cszOldViewport = pPrintDC->SetViewportExt(cxPage, -cyPage);
  79.  
  80.         if (hPrintDC != NULL)
  81.         {
  82.  
  83.             Views *pViews = NULL;
  84.             View *pView = NULL;
  85.  
  86.             short sOldDrawMode = m_sDrawMode;
  87.  
  88.             HRESULT hRes = S_OK;
  89.  
  90.             DOCINFO docInfo;
  91.             memset(&docInfo, 0, sizeof(DOCINFO));
  92.             docInfo.cbSize = sizeof(DOCINFO);
  93.             docInfo.lpszDocName = _T("TurboCAD drawing");
  94.  
  95.             pPrintDC->StartDoc(&docInfo);
  96.             pPrintDC->StartPage();
  97.             // ABT 052097
  98.             // According to MS( see mfc\src\viewprnt.cpp)
  99.             // must call OnPrepareDC on newer versions of Windows because
  100.             // StartPage now resets the device attributes.
  101.             ASSERT_VALID(pPrintDC);
  102.             UNUSED(pPrintDC); // unused in release builds
  103.  
  104.             try
  105.             {
  106.                 hRes = pIDrawing->get_Views(&pViews);
  107.                 CHECK_HRESULT(hRes)
  108.  
  109.                 COleVariant varHwnd(varMissing);
  110.                 COleVariant varDC((const long)hPrintDC);
  111.  
  112.                 hRes = pViews->Add(&varHwnd, &varDC, &pView);
  113.                 CHECK_HRESULT(hRes)
  114.  
  115.                 hRes = pView->put_Update(FALSE);
  116.                 CHECK_HRESULT(hRes)
  117.  
  118.                 hRes = pView->put_CenterOnExtents(TRUE);
  119.                 CHECK_HRESULT(hRes)
  120.  
  121.                 hRes = pView->put_MappingMode(MM_TEXT);
  122.                 CHECK_HRESULT(hRes)
  123.  
  124.                 hRes = pView->put_Margins(FALSE);
  125.                 CHECK_HRESULT(hRes)
  126.  
  127.                 hRes = pView->put_FixedAspectRatio(TRUE);
  128.                 CHECK_HRESULT(hRes)
  129.  
  130.                 hRes = pView->put_ScreenLeft(0);
  131.                 CHECK_HRESULT(hRes)
  132.  
  133.                 hRes = pView->put_ScreenTop(0);
  134.                 CHECK_HRESULT(hRes)
  135.  
  136.                 hRes = pView->put_ScreenWidth(cxPage);
  137.                 CHECK_HRESULT(hRes)
  138.  
  139.                 hRes = pView->put_ScreenHeight(cyPage);
  140.                 CHECK_HRESULT(hRes)
  141.  
  142.                 SetDrawModeEx(DM_WHITE | DM_INVERT);
  143.  
  144.                 hRes = pView->ZoomToExtents();
  145.                 CHECK_HRESULT(hRes)
  146.  
  147.             }
  148.             catch (...)
  149.             {
  150.             }
  151.  
  152.             SetDrawModeEx(sOldDrawMode);
  153.  
  154.             if (pView != NULL)
  155.                 pView->Release();
  156.  
  157.             if (pViews != NULL)
  158.                 pViews->Release();
  159.  
  160.             
  161.         }
  162.  
  163.         pPrintDC->EndPage();
  164.         pPrintDC->SetViewportExt(cszOldViewport);
  165.         pPrintDC->SetWindowExt(cszOldPage);
  166.         pPrintDC->SetMapMode(iOldMode);
  167.         int nResult = pPrintDC->EndDoc();
  168.         ASSERT(nResult >= 0);
  169.         pPrintDC->Detach();
  170.  
  171.         delete pPrintDC;
  172.  
  173.     }
  174.  
  175.     // TODO: Add your implementation code here
  176.  
  177.     return S_OK;
  178. }
  179.  
  180.  
  181.  
  182. void CPrintDr::SetDrawModeEx(short nNewValue)
  183. {
  184.     m_sDrawMode = nNewValue;
  185.     ASSERT((m_sDrawMode & DM_CHECKBITMAP) != DM_CHECKBITMAP);
  186.  
  187.     switch (m_sDrawMode)
  188.     {
  189.         case DM_WHITE:
  190.             m_clrBackGround = RGB(255, 255, 255);
  191.         break;
  192.         case DM_INVERT | DM_WHITE:
  193.             m_clrBackGround = RGB(0, 0, 0);
  194.         break;
  195.         case DM_INVERT | DM_BLACK:
  196.             m_clrBackGround = RGB(255, 255, 255);
  197.         break;
  198.         case DM_BLACK:
  199.         default:
  200.             m_clrBackGround = RGB(0, 0, 0);
  201.         break;
  202.     }
  203.  
  204. }
  205.  
  206.  
  207.  
  208.