Solved

Printing tiny in Windows 95

Posted on 2000-02-27
9
510 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
ID: 2562760
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
ID: 2569697
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
ID: 2571088
What do you mean by "not work"? Have you debugged it?
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:jamdown
ID: 2571144
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
 

Author Comment

by:jamdown
ID: 2571163
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
ID: 2571268
You may put some MessageBeep/MessageBox so that you know whether the code is actually executed.
0
 

Author Comment

by:jamdown
ID: 2574365
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
ID: 2574388
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
ID: 2574417
Right. StartPage resets the attributes of the device context.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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…
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.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

775 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