PRINTDLG to CPrintInfo
-
Your code merely creates an empty
CPrintInfo
structure. You should check the documentation[^] for full details. -
The Remarks section on the documentation page states:
CPrintInfo is a structure and does not have a base class.
The framework creates an object of CPrintInfo each time the Print or Print Preview command is chosen and destroys it when the command is completed.So this control is only available within those two sections of your application, after either menu item is selected. It is possible that you could force this by sending a Print or Print Preview message to your application.
-
The Remarks section on the documentation page states:
CPrintInfo is a structure and does not have a base class.
The framework creates an object of CPrintInfo each time the Print or Print Preview command is chosen and destroys it when the command is completed.So this control is only available within those two sections of your application, after either menu item is selected. It is possible that you could force this by sending a Print or Print Preview message to your application.
-
Richard, is there a way to force printpreview to application, in such a way that user see nothing about it, just for retrieving CPrintInfo ? Could you tell me how ?
-
Richard, I still have a question: I had tried to create manually an CPrintInfo object, just like that:
CPrintDialog pdlg(FALSE); pdlg.GetDefaults(); HDC hDC = pdlg.CreatePrinterDC(); if(NULL != hDC) { m\_pPreviewInfo = new CPrintInfo; m\_dcPrint.CreateCompatibleDC(CDC::FromHandle(hDC)); m\_pPreviewInfo->m\_rectDraw.left = 0; m\_pPreviewInfo->m\_rectDraw.top = 0; m\_pPreviewInfo->m\_rectDraw.right = m\_dcPrint.GetDeviceCaps(HORZRES); m\_pPreviewInfo->m\_rectDraw.bottom = m\_dcPrint.GetDeviceCaps(VERTRES); m\_pPreviewInfo->m\_bContinuePrinting = TRUE; m\_pPreviewInfo->m\_bDirect = TRUE; m\_pPreviewInfo->m\_bDocObject = FALSE; m\_pPreviewInfo->m\_bPreview = TRUE; m\_pPreviewInfo->m\_lpUserData = NULL; m\_pPreviewInfo->m\_pPD = &pdlg; m\_pPreviewInfo->SetMinPage(1); m\_pPreviewInfo->SetMaxPage(2); m\_nPages = m\_pPreviewInfo->m\_nNumPreviewPages; m\_pPreviewDC = new CPreviewDC; m\_pPreviewDC->SetAttribDC(m\_pPreviewInfo->m\_pPD->m\_pd.hDC); m\_sizePrinterPPI.cx = m\_dcPrint.GetDeviceCaps(LOGPIXELSX); m\_sizePrinterPPI.cy = m\_dcPrint.GetDeviceCaps(LOGPIXELSY); m\_nPages = m\_pPreviewInfo->m\_nNumPreviewPages; if (m\_nPages == 0) m\_nPages = 1; else if (m\_nPages > m\_nMaxPages) m\_nPages = m\_nMaxPages; // Sanity Check! }
where m_pPreviewInfo and m_pPreviewDC is protected members of CMyCustomPrintPreview:
CPreviewDC\* m\_pPreviewDC; CPrintInfo\* m\_pPreviewInfo;
of the first sight, I had do something wrong on the above code ? Because when I am trying to use m_pPreviewInfo object in CMyCustomPrintPreview::OnDraw, is crashing and I get an access violation on:
_AFXEXT_INLINE UINT CPrintInfo::GetMaxPage() const
{ return m_pPD->m_pd.nMaxPage; }Thank you.
-
Richard, I still have a question: I had tried to create manually an CPrintInfo object, just like that:
CPrintDialog pdlg(FALSE); pdlg.GetDefaults(); HDC hDC = pdlg.CreatePrinterDC(); if(NULL != hDC) { m\_pPreviewInfo = new CPrintInfo; m\_dcPrint.CreateCompatibleDC(CDC::FromHandle(hDC)); m\_pPreviewInfo->m\_rectDraw.left = 0; m\_pPreviewInfo->m\_rectDraw.top = 0; m\_pPreviewInfo->m\_rectDraw.right = m\_dcPrint.GetDeviceCaps(HORZRES); m\_pPreviewInfo->m\_rectDraw.bottom = m\_dcPrint.GetDeviceCaps(VERTRES); m\_pPreviewInfo->m\_bContinuePrinting = TRUE; m\_pPreviewInfo->m\_bDirect = TRUE; m\_pPreviewInfo->m\_bDocObject = FALSE; m\_pPreviewInfo->m\_bPreview = TRUE; m\_pPreviewInfo->m\_lpUserData = NULL; m\_pPreviewInfo->m\_pPD = &pdlg; m\_pPreviewInfo->SetMinPage(1); m\_pPreviewInfo->SetMaxPage(2); m\_nPages = m\_pPreviewInfo->m\_nNumPreviewPages; m\_pPreviewDC = new CPreviewDC; m\_pPreviewDC->SetAttribDC(m\_pPreviewInfo->m\_pPD->m\_pd.hDC); m\_sizePrinterPPI.cx = m\_dcPrint.GetDeviceCaps(LOGPIXELSX); m\_sizePrinterPPI.cy = m\_dcPrint.GetDeviceCaps(LOGPIXELSY); m\_nPages = m\_pPreviewInfo->m\_nNumPreviewPages; if (m\_nPages == 0) m\_nPages = 1; else if (m\_nPages > m\_nMaxPages) m\_nPages = m\_nMaxPages; // Sanity Check! }
where m_pPreviewInfo and m_pPreviewDC is protected members of CMyCustomPrintPreview:
CPreviewDC\* m\_pPreviewDC; CPrintInfo\* m\_pPreviewInfo;
of the first sight, I had do something wrong on the above code ? Because when I am trying to use m_pPreviewInfo object in CMyCustomPrintPreview::OnDraw, is crashing and I get an access violation on:
_AFXEXT_INLINE UINT CPrintInfo::GetMaxPage() const
{ return m_pPD->m_pd.nMaxPage; }Thank you.
-
I suspect that your
CPrintDialog
object is going out of scope thus causing this error. You should usenew
to create it before adding it to yourCPrintInfo
. -
I suspect that your
CPrintDialog
object is going out of scope thus causing this error. You should usenew
to create it before adding it to yourCPrintInfo
. -
Yes, I had created dinamically (with new), but when I am trying to clean up in my class destructor, it crashing ...
CPrintPreview::~CPrintPreview
{
if(NULL != m_pPrintDlg)
delete m_pPrintDlg; // crash ! <----
}weird ...
-
It should be enough, but if it has somehow been corrupted, or already deleted then you will get a crash. The only way to check is via the debugger.