打印预览实现流程:
首先调用CFormView::OnFilePrintPreview,再依次调用自己的所重写的虚函数OnPreparePrinting(CPrintInfo* pInfo)、OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)、OnPrint(CDC* pDC, CPrintInfo* pInfo)、CFormView::OnPrint(pDC, pInfo);,当关闭打印预览时调用OnEndPrinting(CDC* pDC, CPrintInfo* pInfo) ,结束打印。
部分代码:
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
BOOL CElectronValveView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
pInfo->SetMinPage(1); //设置打印文件起始页
pInfo->SetMaxPage(1); //设置打印文件终止页
return DoPreparePrinting(pInfo);
}
void CElectronValveView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
m_ChartCtrl.GetBottomAxis()->GetGrid()->SetColor(BlackColor);//设置栅格颜色为黑色,不然打印的时候太淡
m_ChartCtrl.GetLeftAxis()->GetGrid()->SetColor(BlackColor);
}
void CElectronValveView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
m_ChartCtrl.GetBottomAxis()->GetGrid()->SetColor(BlueColor);//恢复颜色,不然控件中也是这个颜色了。
m_ChartCtrl.GetLeftAxis()->GetGrid()->SetColor(BlueColor);
}
void CElectronValveView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: add customized printing code here
CMainFrame* pMain=(CMainFrame *)AfxGetApp()->m_pMainWnd
CDC *pCurrentDC = GetDC(); // will have dimensions of the client area
CSize PaperPixelsPerInch(pDC->GetDeviceCaps(LOGPIXELSX), pDC->GetDeviceCaps(LOGPIXELSY));
//获得打印纸设备每英寸的像素数,应该是一样的。
CSize ScreenPixelsPerInch(pCurrentDC->GetDeviceCaps(LOGPIXELSX), pCurrentDC->GetDeviceCaps(LOGPIXELSY));
//应该是获得屏幕的每英寸的像素数,应该是一样的
CSize m_PaperSize,m_LogicalPageSize;
m_PaperSize = CSize(pDC->GetDeviceCaps(HORZRES), pDC->GetDeviceCaps(VERTRES));
//把纸张所得的像素对应的屏幕大小得到。
m_LogicalPageSize.cx = ScreenPixelsPerInch.cx * m_PaperSize.cx / PaperPixelsPerInch.cx * 3 / 4;
m_LogicalPageSize.cy = ScreenPixelsPerInch.cy * m_PaperSize.cy / PaperPixelsPerInch.cy * 3 / 4;
//这里必须这样设,是为了和ChartCtrl中的打印对应起来。
pDC->SetMapMode(MM_ANISOTROPIC);
pDC->SetWindowExt(m_LogicalPageSize);
pDC->SetViewportExt(m_PaperSize);
pDC->SetWindowOrg(0, 0);
CRect rcClient,rcClient1;
GetClientRect(&rcClient1); //得到的仍然是打印预览显示客户区的大小
rcClient=pInfo->m_rectDraw;//打印纸的客户区
double ratioX,ratioY;
ratioX=(double)rcClient.right/(double)rcClient1.Width();
ratioY=(double)rcClient.bottom/(double)rcClient1.Height();
int nWid,left,right;
CFont *pOldFont;
CFont fnBig,fnBig1;
CPen Pen,*OldPen;
//画主框
Pen.CreatePen(PS_SOLID,2,RGB(0,0,0));
OldPen=pDC->SelectObject(&Pen);
pDC->Rectangle(20,20,m_LogicalPageSize.cx-20,m_LogicalPageSize.cy-20);
pDC->SelectObject(OldPen);
//画标题栏
pDC->MoveTo(20,60);
pDC->LineTo(m_LogicalPageSize.cx-20,60);
pDC->MoveTo(20,85);
pDC->LineTo(m_LogicalPageSize.cx-20,85);
pDC->SelectObject(&Pen);
pDC->MoveTo(20,110);
pDC->LineTo(m_LogicalPageSize.cx-20,110);
pDC->SelectObject(OldPen);
pDC->MoveTo(196,60);
pDC->LineTo(196,110);
pDC->MoveTo(390,60);
pDC->LineTo(390,110);
//画结果栏
pDC->MoveTo(20,430);
pDC->LineTo(m_LogicalPageSize.cx-20,430);
pDC->MoveTo(85,430);
pDC->LineTo(85,470);//测量结果1分界线竖线
pDC->MoveTo((m_LogicalPageSize.cx-85-100)/4+85,430);//开启响应时间分界线
pDC->LineTo((m_LogicalPageSize.cx-85-100)/4+85,470);
pDC->MoveTo((m_LogicalPageSize.cx-85-100)/4*2+85,430);
pDC->LineTo((m_LogicalPageSize.cx-85-100)/4*2+85,470);
pDC->MoveTo((m_LogicalPageSize.cx-85-100)/4*3+85,430);
pDC->LineTo((m_LogicalPageSize.cx-85-100)/4*3+85,470);
pDC->MoveTo((m_LogicalPageSize.cx-85-100)/4*4+85,430);
pDC->LineTo((m_LogicalPageSize.cx-85-100)/4*4+85,470);
pDC->MoveTo(20,450);
pDC->LineTo(m_LogicalPageSize.cx-20,450);
pDC->SelectObject(&Pen);
pDC->MoveTo(20,470);
pDC->LineTo(m_LogicalPageSize.cx-20,470);
pDC->SelectObject(OldPen);
//写标题
fnBig.CreatePointFont(200,"黑体",pDC);
pOldFont=pDC->SelectObject(&fnBig);
nWid=rcClient1.Width();
left=rcClient1.left;
right=rcClient1.right;
pDC->SetTextAlign(TA_CENTER);
pDC->TextOut(m_LogicalPageSize.cx/2,30,"XXX性能测试报告");
fnBig.DeleteObject();
fnBig.CreatePointFont(110,"宋体",pDC);
pOldFont=pDC->SelectObject(pOldFont);
pOldFont=pDC->SelectObject(&fnBig);
pDC->SetTextAlign(TA_LEFT);
pDC->TextOut(25,67,"产品名称:"+pMain->m_strProductName);
pDC->TextOut(25,92,"产品图号:"+pMain->m_strProductPictureNo);
pDC->TextOut(201,67,"检测时间:"+pMain->m_strMeasureTime0);
pDC->TextOut(201,92,"生产厂家:"+pMain->m_strProductFactory);
pDC->TextOut(395,67,"产品编号:"+pMain->m_strProductBianHao);
pDC->TextOut(395,92,"检 验 员:"+pMain->m_strMeasurerName);
CString aa;
aa.Format("%d(ms)",m_iPreviousTime1);
fnBig.DeleteObject();
fnBig.CreatePointFont(85,"宋体",pDC);
pDC->SelectObject(&fnBig);
pDC->TextOut(25,420,"预置电信号时间:"+aa);
aa.Format("%d",m_iCurveNumber1);
pDC->TextOut(150,420,"曲线数量:"+aa);
fnBig1.CreatePointFont(100,"宋体",pDC);
pOldFont=pDC->SelectObject(&fnBig1);
pDC->TextOut(25,455,"测试结果");
pDC->TextOut(90,435,"开启响应时间(ms)");
pDC->TextOut(120,455,m_strOpenResponseTime);
pDC->TextOut((m_LogicalPageSize.cx-85-100)/4+85+5,435,"开启换向时间(ms)");
pDC->TextOut((m_LogicalPageSize.cx-85-100)/4+85+5+30,455,m_strOpenInvertTime);
pDC->TextOut((m_LogicalPageSize.cx-85-100)/4*2+85+5,435,"关闭响应时间(ms)");
pDC->TextOut((m_LogicalPageSize.cx-85-100)/4*2+85+5+30,455,m_strCloseResponseTime);
pDC->TextOut((m_LogicalPageSize.cx-85-100)/4*3+85+5,435,"关闭换向时间(ms)");
pDC->TextOut((m_LogicalPageSize.cx-85-100)/4*3+85+5+30,455,m_strCloseInvertTime);
pDC->TextOut((m_LogicalPageSize.cx-85-100)/4*4+85+10,435,"检测结论");
//最后一栏
fnBig.DeleteObject();
fnBig.CreatePointFont(130,"宋体",pDC);
pDC->SelectObject(&fnBig);
pDC->TextOut(m_LogicalPageSize.cx-180,m_LogicalPageSize.cy-20-20,"审核:");
pDC->SetTextAlign(TA_LEFT);
CPoint offset(16,110);
m_ChartCtrl.Print1(offset,pDC,pInfo,0);//调用chart类来画表格和曲线
fnBig.DeleteObject();
CFormView::OnPrint(pDC, pInfo);
}
本程序利用ChartCtrl来实现绘制表格和曲线。