Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Printing tiny in Windows 95

Posted on 2000-02-27
9
Medium Priority
?
529 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 150 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

636 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