Link to home
Start Free TrialLog in
Avatar of jamdown
jamdown

asked on

Printing tiny in Windows 95

Below is my printing code for a doc-view application with a form view for a main view.  Everything prints OK on Windows NT. However, on Windows 95/98, it prints in really tiny print on the top left of the paper. I need an expert’s advice on how to modify the code to resolve this problem.

// #ifndef _AFXDLL
      static BOOL NEAR _afxUserAbort = FALSE;

class CPrintingDialog : public CDialog
{
public:
   //{{AFX_DATA(CPrintingDialog)
   enum { IDD = AFX_IDD_PRINTDLG };
   //}}AFX_DATA
   CPrintingDialog::CPrintingDialog(CWnd* pParent)
      {
         Create(CPrintingDialog::IDD, pParent);      // modeless !
         _afxUserAbort = FALSE;
      }

   virtual BOOL OnInitDialog();
   virtual void OnCancel();

protected:

#if 0 // for ClassWizard's use only
   //{{AFX_MSG(CPrintingDialog)
   //}}AFX_MSG
   DECLARE_MESSAGE_MAP()
#endif
};

BOOL CALLBACK AFX_EXPORT _AfxAbortProc(HDC, int)
{
   MSG msg;
   while (!_afxUserAbort &&
         ::PeekMessage(&msg, NULL, NULL, NULL, PM_NOREMOVE))
   {
      if (!AfxGetApp()->PumpMessage())
         return FALSE;               // Bag out if WM_QUIT received
   }
   return !_afxUserAbort;
}

BOOL CPrintingDialog::OnInitDialog()
{
   SetWindowText(AfxGetAppName());
   CenterWindow();
   return CDialog::OnInitDialog();
}

void CPrintingDialog::OnCancel()
{
   _afxUserAbort = TRUE;       // flag that user aborted print
   CDialog::OnCancel();
}

void PrintSpreadSheet (CGrid *pSpreadSheet)
{
   HWND pFocusWnd = GetFocus ();

   CFrameWnd *pTheFrame = NULL;
   CSSView *pTheView = NULL;

   // Create a spreadsheet view to print from
   ASSERT (AfxGetApp ());
   ASSERT (AfxGetApp ()->m_pMainWnd);
   pTheFrame = ((CMPFrameWnd *)((CMPApp *) AfxGetApp ())->m_pMainWnd)->OnWindowNewSS ();
   ASSERT (pTheFrame);

   // Find the view
   pTheView = (CSSView *) pTheFrame->GetActiveView ();
   ASSERT (pTheView);

   // Make a copy of the spreadsheet to print
   pTheView->CloneSpreadSheet (pSpreadSheet);

   ////////////////////////////////////////////////////////////////////////////////////////////
   // The following chunk of code is a modified OnFilePrint from VIEWPRNT.CPP in the MFC source
   ////////////////////////////////////////////////////////////////////////////////////////////
   CPrintInfo printInfo;           // Get Default print info
   ASSERT(printInfo.m_pPD != NULL);    // must be set

   if (pTheView->OnPreparePrinting(&printInfo))
      {       // print if OK

      ASSERT(printInfo.m_pPD->m_pd.hDC != NULL);

      CDC dcPrint;
      dcPrint.Attach(printInfo.m_pPD->m_pd.hDC);          // attach printer dc
      dcPrint.m_bPrinting = TRUE;

      pTheView->OnBeginPrinting(&dcPrint, &printInfo);

      CPrintingDialog dlgPrintStatus(pTheView);

      CString docTitle = AfxGetAppName();

      dlgPrintStatus.SetDlgItemText(AFX_IDC_PRINT_DOCNAME, docTitle);
      dlgPrintStatus.SetDlgItemText(AFX_IDC_PRINT_PRINTERNAME,
         printInfo.m_pPD->GetDeviceName());

      CString strPort;
      AfxFormatString1(strPort, AFX_IDS_PRINTONPORT,
         printInfo.m_pPD->GetPortName());
      dlgPrintStatus.SetDlgItemText(AFX_IDC_PRINT_PORTNAME, strPort);
      dlgPrintStatus.ShowWindow(SW_SHOW);

      dcPrint.SetAbortProc(_AfxAbortProc);

      if (docTitle.GetLength() > 31)
         docTitle.ReleaseBuffer(31);

      DOCINFO docInfo;
      docInfo.cbSize = sizeof(DOCINFO);
      docInfo.lpszDocName = docTitle;
      docInfo.lpszOutput = NULL;

      if (dcPrint.StartDoc(&docInfo) == SP_ERROR)
      {
         AfxMessageBox(AFX_IDP_FAILED_TO_START_PRINT);
         return;
      }

      AfxGetMainWnd()->EnableWindow(FALSE);   // Disable main window

      UINT nEndPage = printInfo.GetToPage();
      UINT nStartPage = printInfo.GetFromPage();

      // Guarantee values are in the valid range
      if (nEndPage < printInfo.GetMinPage())
         nEndPage = printInfo.GetMinPage();
      if (nEndPage > printInfo.GetMaxPage())
         nEndPage = printInfo.GetMaxPage();

      if (nStartPage < printInfo.GetMinPage())
         nStartPage = printInfo.GetMinPage();
      if (nStartPage > printInfo.GetMaxPage())
         nStartPage = printInfo.GetMaxPage();

      int nStep = (nEndPage >= nStartPage) ? 1 : -1;
      nEndPage = (nEndPage == 0xffff) ? 0xffff : nEndPage + nStep;

      BOOL bError = FALSE;
      for (printInfo.m_nCurPage = nStartPage;
         !bError && printInfo.m_nCurPage != nEndPage;
         printInfo.m_nCurPage += nStep)
      {
         pTheView->OnPrepareDC(&dcPrint, &printInfo);

         if (!printInfo.m_bContinuePrinting)
            break;          // reached end of print

         // Set up drawing rect to entire page (in logical coordinates)
         printInfo.m_rectDraw.SetRect(0, 0, dcPrint.GetDeviceCaps(HORZRES),
                                    dcPrint.GetDeviceCaps(VERTRES));
         dcPrint.DPtoLP(&printInfo.m_rectDraw);

         CString strFmt;
         VERIFY(strFmt.LoadString(AFX_IDS_PRINTPAGENUM));
         char szBuf[80];
         wsprintf(szBuf, strFmt, printInfo.m_nCurPage);
         dlgPrintStatus.SetDlgItemText(AFX_IDC_PRINT_PAGENUM, szBuf);

         VERIFY(dcPrint.StartPage());
         pTheView->OnPrint(&dcPrint, &printInfo);
         if (dcPrint.EndPage() < 0)
            bError = TRUE;
      }

      if (!bError)
         dcPrint.EndDoc();

      AfxGetMainWnd()->EnableWindow(TRUE);    // Enable main window

      pTheView->OnEndPrinting(&dcPrint, &printInfo);        // Clean up after printing

      dlgPrintStatus.DestroyWindow();
   }
   ////////////////////////////////////////////////////////////////////////////////////////////

   // And remove the view
   pTheFrame->DestroyWindow ();

   SetFocus (pFocusWnd);
}
 
ASKER CERTIFIED SOLUTION
Avatar of chensu
chensu
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jamdown
jamdown

ASKER

Did not work. Here's where I inserted the code:

/////////////////////////////////////////////////////////////////////////////
// CSSView printing
void CSSView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
     if (pInfo != NULL)
    {   // printing
        CDC ScreenDC;
        ScreenDC.CreateIC(_T("DISPLAY"), NULL, NULL, NULL);

        pDC->SetMapMode(MM_ANISOTROPIC);
        pDC->SetWindowExt(ScreenDC.GetDeviceCaps(LOGPIXELSX),
                          ScreenDC.GetDeviceCaps(LOGPIXELSY));
        pDC->SetViewportExt(pDC->GetDeviceCaps(LOGPIXELSX),
                            pDC->GetDeviceCaps(LOGPIXELSY));
        pDC->SetWindowOrg(0, 0);
        pDC->SetViewportOrg(0, 0);
    }

    CView::OnPrepareDC(pDC, pInfo);

}
What do you mean by "not work"? Have you debugged it?
Avatar of jamdown

ASKER

I'm using a NT workstation running VC++ 5.0. When I applied the changes, it still worked OK on NT. I then ported the .exe to the WIN95 and tested, same results -printed tiny. Unfortunately, the WIN95 machine is not setup as a development box. I am not able to debug on WIN95.
Avatar of jamdown

ASKER

Here's the rest of the code, hope this helps. Thanks.

/////////////////////////////////////////////////////////////////////////////
// CSSView printing
void CSSView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
  if (pInfo != NULL)
    {   // printing
        CDC ScreenDC;
        ScreenDC.CreateIC(_T("DISPLAY"), NULL, NULL, NULL);

        pDC->SetMapMode(MM_ANISOTROPIC);
        pDC->SetWindowExt(ScreenDC.GetDeviceCaps(LOGPIXELSX),
                          ScreenDC.GetDeviceCaps(LOGPIXELSY));
        pDC->SetViewportExt(pDC->GetDeviceCaps(LOGPIXELSX),
                            pDC->GetDeviceCaps(LOGPIXELSY));
        pDC->SetWindowOrg(0, 0);
        pDC->SetViewportOrg(0, 0);
    }

   
     CView::OnPrepareDC(pDC, pInfo);
}

BOOL CSSView::OnPreparePrinting(CPrintInfo* pInfo)
{
 pInfo->SetMaxPage(0xffff);

 pInfo->m_pPD->m_pd.Flags &= ~PD_NOSELECTION;
 pInfo->m_pPD->m_pd.hInstance = AfxGetInstanceHandle();

 // default preparation
 return DoPreparePrinting(pInfo);
}

void CSSView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{                                
   // Print table
   // use objective grid printing routine

   ASSERT (m_pSS);

   m_pSS->OnPrint (pDC, pInfo);  // Prints page specified in pInfo
}

void CSSView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
 ASSERT (m_pSS);

 m_pSS->OnBeginPrinting (pDC, pInfo);   // Initializes pInfo
}

void CSSView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
 m_pSS->OnGridEndPrinting(pDC, pInfo);
}

You may put some MessageBeep/MessageBox so that you know whether the code is actually executed.
Avatar of jamdown

ASKER

Code works. The problem had to do with StartPage(). It appears that you must recall OnPrepareDC on newer versions of Windows after StartPage() because StartPage now resets the device attributes.
Avatar of jamdown

ASKER

Code works. The problem had to do with StartPage(). It appears that you must recall OnPrepareDC on newer versions of Windows after StartPage() because StartPage now resets the device attributes.
Right. StartPage resets the attributes of the device context.