Solved

Printing tiny in Windows 95

Posted on 2000-02-27
9
502 Views
Last Modified: 2013-11-20
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);
}
 
0
Comment
Question by:jamdown
  • 5
  • 4
9 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 50 total points
Comment Utility
You need to set the mapping mode for the printer DC.

void CMyView::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);
}
0
 

Author Comment

by:jamdown
Comment Utility
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);

}
0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
What do you mean by "not work"? Have you debugged it?
0
 

Author Comment

by:jamdown
Comment Utility
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.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:jamdown
Comment Utility
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);
}

0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
You may put some MessageBeep/MessageBox so that you know whether the code is actually executed.
0
 

Author Comment

by:jamdown
Comment Utility
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.
0
 

Author Comment

by:jamdown
Comment Utility
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.
0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
Right. StartPage resets the attributes of the device context.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
xyBalance chalenge 58 88
Strange date stored 22 100
modThree challenge 4 64
unix example issues 18 44
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now