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(CPrintingDial og)
enum { IDD = AFX_IDD_PRINTDLG };
//}}AFX_DATA
CPrintingDialog::CPrinting Dialog(CWn d* pParent)
{
Create(CPrintingDialog::ID D, pParent); // modeless !
_afxUserAbort = FALSE;
}
virtual BOOL OnInitDialog();
virtual void OnCancel();
protected:
#if 0 // for ClassWizard's use only
//{{AFX_MSG(CPrintingDialo g)
//}}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::OnInitDia log()
{
SetWindowText(AfxGetAppNam e());
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)->OnWindow NewSS ();
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->OnPreparePrinti ng(&printI nfo))
{ // 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.SetDlgItemT ext(AFX_ID C_PRINT_DO CNAME, docTitle);
dlgPrintStatus.SetDlgItemT ext(AFX_ID C_PRINT_PR INTERNAME,
printInfo.m_pPD->GetDevice Name());
CString strPort;
AfxFormatString1(strPort, AFX_IDS_PRINTONPORT,
printInfo.m_pPD->GetPortNa me());
dlgPrintStatus.SetDlgItemT ext(AFX_ID C_PRINT_PO RTNAME, strPort);
dlgPrintStatus.ShowWindow( SW_SHOW);
dcPrint.SetAbortProc(_AfxA bortProc);
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_FAIL ED_TO_STAR T_PRINT);
return;
}
AfxGetMainWnd()->EnableWin dow(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(&dcP rint, &printInfo);
if (!printInfo.m_bContinuePri nting)
break; // reached end of print
// Set up drawing rect to entire page (in logical coordinates)
printInfo.m_rectDraw.SetRe ct(0, 0, dcPrint.GetDeviceCaps(HORZ RES),
dcPrint.GetDeviceCaps(VERT RES));
dcPrint.DPtoLP(&printInfo. m_rectDraw );
CString strFmt;
VERIFY(strFmt.LoadString(A FX_IDS_PRI NTPAGENUM) );
char szBuf[80];
wsprintf(szBuf, strFmt, printInfo.m_nCurPage);
dlgPrintStatus.SetDlgItemT ext(AFX_ID C_PRINT_PA GENUM, szBuf);
VERIFY(dcPrint.StartPage() );
pTheView->OnPrint(&dcPrint , &printInfo);
if (dcPrint.EndPage() < 0)
bError = TRUE;
}
if (!bError)
dcPrint.EndDoc();
AfxGetMainWnd()->EnableWin dow(TRUE); // Enable main window
pTheView->OnEndPrinting(&d cPrint, &printInfo); // Clean up after printing
dlgPrintStatus.DestroyWind ow();
}
////////////////////////// ////////// ////////// ////////// ////////// ////////// ////////// //////
// And remove the view
pTheFrame->DestroyWindow ();
SetFocus (pFocusWnd);
}
// #ifndef _AFXDLL
static BOOL NEAR _afxUserAbort = FALSE;
class CPrintingDialog : public CDialog
{
public:
//{{AFX_DATA(CPrintingDial
enum { IDD = AFX_IDD_PRINTDLG };
//}}AFX_DATA
CPrintingDialog::CPrinting
{
Create(CPrintingDialog::ID
_afxUserAbort = FALSE;
}
virtual BOOL OnInitDialog();
virtual void OnCancel();
protected:
#if 0 // for ClassWizard's use only
//{{AFX_MSG(CPrintingDialo
//}}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::OnInitDia
{
SetWindowText(AfxGetAppNam
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)->OnWindow
ASSERT (pTheFrame);
// Find the view
pTheView = (CSSView *) pTheFrame->GetActiveView ();
ASSERT (pTheView);
// Make a copy of the spreadsheet to print
pTheView->CloneSpreadSheet
//////////////////////////
// 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->OnPreparePrinti
{ // print if OK
ASSERT(printInfo.m_pPD->m_
CDC dcPrint;
dcPrint.Attach(printInfo.m
dcPrint.m_bPrinting = TRUE;
pTheView->OnBeginPrinting(
CPrintingDialog dlgPrintStatus(pTheView);
CString docTitle = AfxGetAppName();
dlgPrintStatus.SetDlgItemT
dlgPrintStatus.SetDlgItemT
printInfo.m_pPD->GetDevice
CString strPort;
AfxFormatString1(strPort, AFX_IDS_PRINTONPORT,
printInfo.m_pPD->GetPortNa
dlgPrintStatus.SetDlgItemT
dlgPrintStatus.ShowWindow(
dcPrint.SetAbortProc(_AfxA
if (docTitle.GetLength() > 31)
docTitle.ReleaseBuffer(31)
DOCINFO docInfo;
docInfo.cbSize = sizeof(DOCINFO);
docInfo.lpszDocName = docTitle;
docInfo.lpszOutput = NULL;
if (dcPrint.StartDoc(&docInfo
{
AfxMessageBox(AFX_IDP_FAIL
return;
}
AfxGetMainWnd()->EnableWin
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(&dcP
if (!printInfo.m_bContinuePri
break; // reached end of print
// Set up drawing rect to entire page (in logical coordinates)
printInfo.m_rectDraw.SetRe
dcPrint.GetDeviceCaps(VERT
dcPrint.DPtoLP(&printInfo.
CString strFmt;
VERIFY(strFmt.LoadString(A
char szBuf[80];
wsprintf(szBuf, strFmt, printInfo.m_nCurPage);
dlgPrintStatus.SetDlgItemT
VERIFY(dcPrint.StartPage()
pTheView->OnPrint(&dcPrint
if (dcPrint.EndPage() < 0)
bError = TRUE;
}
if (!bError)
dcPrint.EndDoc();
AfxGetMainWnd()->EnableWin
pTheView->OnEndPrinting(&d
dlgPrintStatus.DestroyWind
}
//////////////////////////
// And remove the view
pTheFrame->DestroyWindow ();
SetFocus (pFocusWnd);
}
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
What do you mean by "not work"? Have you debugged it?
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.
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("DISP LAY"), NULL, NULL, NULL);
pDC->SetMapMode(MM_ANISOTR OPIC);
pDC->SetWindowExt(ScreenDC .GetDevice Caps(LOGPI XELSX),
ScreenDC.GetDeviceCaps(LOG PIXELSY));
pDC->SetViewportExt(pDC->G etDeviceCa ps(LOGPIXE LSX),
pDC->GetDeviceCaps(LOGPIXE LSY));
pDC->SetWindowOrg(0, 0);
pDC->SetViewportOrg(0, 0);
}
CView::OnPrepareDC(pDC, pInfo);
}
BOOL CSSView::OnPreparePrinting (CPrintInf o* pInfo)
{
pInfo->SetMaxPage(0xffff);
pInfo->m_pPD->m_pd.Flags &= ~PD_NOSELECTION;
pInfo->m_pPD->m_pd.hInstan ce = 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(C DC* pDC, CPrintInfo* pInfo)
{
ASSERT (m_pSS);
m_pSS->OnBeginPrinting (pDC, pInfo); // Initializes pInfo
}
void CSSView::OnEndPrinting(CDC * pDC, CPrintInfo* pInfo)
{
m_pSS->OnGridEndPrinting(p DC, pInfo);
}
//////////////////////////
// CSSView printing
void CSSView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
if (pInfo != NULL)
{ // printing
CDC ScreenDC;
ScreenDC.CreateIC(_T("DISP
pDC->SetMapMode(MM_ANISOTR
pDC->SetWindowExt(ScreenDC
ScreenDC.GetDeviceCaps(LOG
pDC->SetViewportExt(pDC->G
pDC->GetDeviceCaps(LOGPIXE
pDC->SetWindowOrg(0, 0);
pDC->SetViewportOrg(0, 0);
}
CView::OnPrepareDC(pDC, pInfo);
}
BOOL CSSView::OnPreparePrinting
{
pInfo->SetMaxPage(0xffff);
pInfo->m_pPD->m_pd.Flags &= ~PD_NOSELECTION;
pInfo->m_pPD->m_pd.hInstan
// 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(C
{
ASSERT (m_pSS);
m_pSS->OnBeginPrinting (pDC, pInfo); // Initializes pInfo
}
void CSSView::OnEndPrinting(CDC
{
m_pSS->OnGridEndPrinting(p
}
You may put some MessageBeep/MessageBox so that you know whether the code is actually executed.
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.
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.
ASKER
//////////////////////////
// CSSView printing
void CSSView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
if (pInfo != NULL)
{ // printing
CDC ScreenDC;
ScreenDC.CreateIC(_T("DISP
pDC->SetMapMode(MM_ANISOTR
pDC->SetWindowExt(ScreenDC
ScreenDC.GetDeviceCaps(LOG
pDC->SetViewportExt(pDC->G
pDC->GetDeviceCaps(LOGPIXE
pDC->SetWindowOrg(0, 0);
pDC->SetViewportOrg(0, 0);
}
CView::OnPrepareDC(pDC, pInfo);
}