1.The base architecture:
CDC dc(this);
CPrintDialog dlg(False);
//dlg.GetDefault();
//if(dlg.DoModal()==IDOK)
dc.Attach(dlg.GetPrintDC())
DOCINFO di;
::ZeroMemory(&di,sizeof(DOCINFO));
di.cbSize=sizeof(DOCINFO)
di.lpszDocName=_T("ddddddddd");
if(dc.StatDoc(&di))
int nContinue=true;
for(int i=0;i<nPage&&nContinue;i++)
{
dc.StartPage();
//
if(dc.EndPage()<0)
nContinue=false;
}
if(nContinue)
dc.EndDoc();
else
dc.Abort();
2.Abort processure:[CView]
int CALLBACK AbortProc(HDC hDC,UNIT nCode)
{
MSG msg;
while(!bUserAbort&&::PeekMessage(&msg,0,0,PM_NOREMOVE)
AfxGetThread()->PumpMessage();
return !bUserAbort;
}
3.MFC print Architecture
OnPreparePrinting(CDC *pDC,CPrintInfo *pf)
{
pf->SetMaxPage(3);
return DoPreparePrinting(pDC,pf);
}
OnBeginPrinting(CDC *pDC,CPrintInfo *pf)
{
int nPageHeight=pf->GetDeviceCaps(VERTRES);
int nDocLength=pf->GetDocLength();
int nMaxPage=max(1,(nDocLength+nPageHeight-1)/nPageHeight);
pf->SetMaxPage(nMaxPage);
}
If it's impossible (or simply inconvenient) to determine the document length before printing begins, you can perform print-time pagination by overriding OnPrepareDC and setting CPrintInfo::m_bContinuePrinting to TRUE or FALSE each time OnPrepareDC is called. An m_bContinuePrinting value equal to FALSE terminates the print job. If you don't call SetMaxPage, the framework assumes the document is only one page long. Therefore, you must override OnPrepareDC and set m_bContinuePrinting to print documents that are more than one page long if you don't set the maximum page number with SetMaxPage.
OnPrepareDC(CDC *pDC,CPrintInfo *pf) //you need to override OnPrepareDC only if you use OnDraw to draw to both the screen and the printed page.
{
CView::OnPrepareDC(pDC,pf);
if(pDC->isPrinting())
{
//.........
int y=(pf->m_nCurPage-1)/nPageHeight;
pDC->SetViewOrg(0,-y);
}
}
OnPrint(CDC *pDC,CPrintInfo *pf) //It's usually more practical to put printer output logic in OnPrint and screen logic in OnDraw.
{
PrintHeader(pDC);
PrintPageNumber(pDC,pf->m_nCurPage);
// Set the viewport origin and/or clipping region before
OnDraw(pDC);
}
OnEndPrinting(CDC *pDC,CPrintInfo *pf)
4.PrintPreview
CView::OnFilePrintPreview ID=ID_FILE_PRINT_PREVIEW
void CMyView::OnEndPrintPreview (CDC* pDC, CPrintInfo* pInfo,
POINT point, CPreviewView* pView)
{
UINT nPage = pInfo->m_nCurPage;
POINT pt;
// Convert nPage into a scroll position in pt.
ScrollToPosition (pt);
CScrollView::OnEndPrintPreview (pDC, pInfo, point, pView);
}
本文详细介绍了MFC框架中的打印功能实现方式,包括打印的基本架构、打印预览、打印过程中的取消操作处理以及如何通过覆盖特定函数来实现复杂的打印逻辑。对于希望在MFC应用程序中集成打印功能的开发者来说,本文提供了实用的指导。

被折叠的 条评论
为什么被折叠?



