工控编程吧
标题:
310上位机VC MFC实现基于对话框的打印功能
[打印本页]
作者:
qq263946146
时间:
2016-9-23 22:01
标题:
310上位机VC MFC实现基于对话框的打印功能
(, 下载次数: 1)
上传
点击文件名下载附件
基于对话框的打印功能
功能展示
对话框程序是我们常用的一种框架,但它并没封装打印功能,我们当前例程就来实现基于对话框的打印功能,效果如图,点击<打印下载数据>会弹出一打印对话框,点击对话框的打印,可以将例程界面的数据打印;
要点提示
例程的打印功能的实现 是通过CPrintDialog实现,调用其DoModal()函数就可获得打印对话框,打印机设备上下文件的获取可以通过此类的函数GetPrinterDC()实现
实现功能
1.新建基于对话框的应用程序
2.添加变量及自定义函数并在OnInitDialog()中初始化变量
public:
CString merchandise[4][5];
int m_nScreenX,m_nScreenY;
void DrawReport(CRect rect, CDC *pDC, BOOL isprinted);
merchandise[0][0]= "商品编号";
merchandise[1][0]= "商品名称";
merchandise[2][0]= "数量";
merchandise[3][0]= "单价";
merchandise[0][1]= "XX001";
merchandise[0][2]= "XX002";
merchandise[0][3]= "XX003";
merchandise[0][4]= "XX004";
merchandise[1][1]= "手机";
merchandise[1][2]= "电视";
merchandise[1][3]= "空调";
merchandise[1][4]= "电脑";
merchandise[2][1]= "2000";
merchandise[2][2]= "4000";
merchandise[2][3]= "6000";
merchandise[2][4]= "7000";
merchandise[3][1]= "1200";
merchandise[3][2]= "2500";
merchandise[3][3]= "3200";
merchandise[3][4]= "5000";
3.实现自定义函数的函数体部分
void CGkbc8Dlg:
rawReport(CRect rect, CDC *pDC, BOOL isprinted)
{
CFont titlefont,bodyfont;
titlefont.CreatePointFont(200,"宋体",pDC);
bodyfont.CreatePointFont(120,"宋体",pDC);
if (!isprinted) //预览
{
rect.DeflateRect(0,15,0,0);
pDC->SelectObject(&titlefont);
pDC->DrawText("商品销售排行",rect,DT_CENTER);
pDC->SelectObject(&bodyfont);
CRect m_rect(rect);
CRect temprect(m_rect.left+80,m_rect.top+60,40+(m_rect.Width())/4,m_rect.bottom+100);
CRect itemrect;
int width = temprect.Width();
for (int i = 0;i<4;i++)
{
pDC->DrawText(merchandise
[0],temprect,DT_LEFT);
itemrect.CopyRect(temprect);
for (int y = 1; y< 5;y++)
{
itemrect.DeflateRect(0,50);
pDC->DrawText(merchandise
[y],itemrect,DT_LEFT);
}
temprect.DeflateRect(width,0,0,0);
temprect.InflateRect(0,0,width,0);
}
}
else
{
int printx,printy;
printx = pDC->GetDeviceCaps(LOGPIXELSX);
printy = pDC->GetDeviceCaps(LOGPIXELSY);
double ratex = (double)(printx)/m_nScreenX;
double ratey = (double)(printy)/m_nScreenY;
rect.DeflateRect(0,(int)(ratey*15),0,0);
pDC->SelectObject(&titlefont);
pDC->StartDoc("printinformation");
pDC->DrawText("商品销售排行",rect,DT_CENTER);
pDC->SelectObject(&bodyfont);
CRect m_rect(rect);
CRect temprect(m_rect.left+(int)(80*ratex),m_rect.top+(int)(60*ratey),
(int)(ratey*40)+(m_rect.Width())/4,m_rect.bottom+(int)(ratey*100));
CRect itemrect;
int width = temprect.Width();
for (int i = 0;i<4;i++)
{
pDC->DrawText(merchandise
[0],temprect,DT_LEFT);
itemrect.CopyRect(temprect);
for (int y = 1; y< 5;y++)
{
itemrect.DeflateRect(0,(int)(ratey*50));
pDC->DrawText(merchandise
[y],itemrect,DT_LEFT);
}
temprect.DeflateRect(width,0,0,0);
temprect.InflateRect(0,0,width,0);
}
pDC->EndDoc();
}
titlefont.DeleteObject();
bodyfont.DeleteObject();
}
4.在OnPaint()函数中显示要打印的数据
CRect rect;
GetClientRect(rect);
CDC* pDC = GetDC();
m_nScreenX = pDC->GetDeviceCaps(LOGPIXELSX);
m_nScreenY = pDC->GetDeviceCaps(LOGPIXELSY);
DrawReport(rect,GetDC(),false);
5.添加按钮<打印下面数据>关联点击函数,实现打印功能
void CGkbc8Dlg::OnButton1()
{
CPrintDialog printdlg(false);
if (printdlg.DoModal()==IDOK)
{
CDC dc1;
dc1.Attach(printdlg.GetPrinterDC());
int leftmargin;
leftmargin = dc1.GetDeviceCaps(PHYSICALOFFSETX);
CRect rect(-leftmargin,0,dc1.GetDeviceCaps(PHYSICALWIDTH)-leftmargin,dc1.GetDeviceCaps(PHYSICALHEIGHT)) ;
DrawReport(rect,&dc1,true);
}
}
我们来演示下功能实现的整个过程
[iqiyi]http://player.video.qiyi.com/63f3dd14c1c86e44334ced993e05f4a1/0/0/w_19rstazlsl.swf-albumId=6359666809-tvId=6359666809-isPurchase=0-cnId=12[/iqiyi]
(, 下载次数: 0)
上传
点击文件名下载附件
[note]1[/note]
欢迎光临 工控编程吧 (https://www.gkbc8.com/)
Powered by Discuz! X3.4