BOOL CPosDlg::PrintListCtrl(CListCtrl &list) { CDC dc; //把打印设备环境附加到DC对象 CPrintDialog pdlg(FALSE,PD_NOPAGENUMS|PD_NOSELECTION,this); BOOL bFindPrinter=pdlg.GetDefaults(); if(!bFindPrinter) return FALSE; dc.Attach(pdlg.GetPrinterDC()); //取打印机的横方向和纵方向的分辨率 //即每英寸点数 short cxInch = dc.GetDeviceCaps(LOGPIXELSX); short cyInch = dc.GetDeviceCaps(LOGPIXELSY); //字体 CFont font; VERIFY(font.CreatePointFont(96, "宋体", &dc));//为DC创建字体 CFont* def_font = dc.SelectObject(&font);//保存现在的字体 //根据字体宽度、高度计算每行最大字数及每页最大行数 //取打印纸张高度和宽度 int nPageHeight, nPageWidth; nPageHeight = dc.GetDeviceCaps(VERTRES); nPageWidth = dc.GetDeviceCaps(HORZRES); TEXTMETRIC TextM; dc.GetTextMetrics(&TextM); //字体高度 //字体平均宽度 int nCharHeight = (unsigned short)TextM.tmHeight; int nCharWidth=(unsigned short)TextM.tmAveCharWidth; //每行最大字数 //每页最大行数 int m_MaxLineChar = nPageWidth / nCharWidth - 8; int m_LinesPerPage = nPageHeight/ nCharHeight; //页边距 int nXMargin = 2; int nYMargin = 2; //获得行数 int nLineCount = list.GetItemCount(); int i; //设置所需打印纸张数目 DOCINFO di; di.cbSize = sizeof(DOCINFO); di.lpszDocName = "Mypos printing"; di.lpszOutput = (LPTSTR) NULL; di.lpszDatatype = (LPTSTR) NULL; di.fwType = 0; dc.StartDoc(&di); dc.StartPage(); //输出公司名称 CString stitle,sftitle,space=""; stitle=GetCompanyFloor(TRUE); int n=(36-stitle.GetLength())/2; for(i=0;i<n;i++) space=space+" "; sftitle=space+stitle; dc.TextOut(2, nYMargin+nCharHeight,sftitle, strlen(sftitle)); //输出单号 CString sbillid; int iOption = m_oListbill.GetCurSel(); m_oListbill.GetText(iOption, sbillid);//得到选中项的文本 sbillid="单号:"+sbillid; dc.TextOut( 2, nYMargin+nCharHeight*2, sbillid, strlen(sbillid)); //输出桌号、开台时间、人数、结帐时间、收银、付款方式 CString stable,speople,sbegin,ssale,stable1,speople1,send,spaymode; m_oTableno.GetWindowText(stable); stable1.Format("%-8s",stable);//Table No. m_oPeasons.GetWindowText(speople); speople1.Format("%-8s",speople);//People quantity. m_oBegintime.GetWindowText(sbegin); m_oCheck.GetWindowText(ssale);//Checkman. spaymode.Format("%-15s",theApp.spaymodep); if(ssale=="无") { ssale.Format("%-8s",theApp.name); spaymode=""; } m_oEndtime.GetWindowText(send);//End time. if(send=="无") send=theApp.snowtimep; stable="开台:"+sbegin+" 桌号:"+stable1; speople="结账:"+send+" 人数:"+speople1; ssale="付款方式:"+spaymode+" 收银:"+ssale; dc.TextOut( 2, nYMargin+nCharHeight*3, stable, strlen(stable)); dc.TextOut( 2, nYMargin+nCharHeight*4, speople, strlen(speople)); dc.TextOut( 2, nYMargin+nCharHeight*5, ssale, strlen(ssale)); //输出列表控件的列标题 CString slisttitle; slisttitle="名称 数量 金额"; dc.TextOut( 2, nYMargin+nCharHeight*7,slisttitle, strlen(slisttitle)); int nMaxLinePerPage = nPageHeight/nCharHeight -3;//每页最大行数 int nCurPage =1; //输出各列的数据 CString subitem,sfsub,sfsub1,sfsub2; for(i =0; i<nLineCount; i++) { if(nLineCount+7-(nCurPage-1)*nMaxLinePerPage > nMaxLinePerPage) { //新的一页 dc.EndPage(); dc.StartPage(); nCurPage ++; } subitem= list.GetItemText(i, 1); sfsub.Format("%-20s",subitem); subitem= list.GetItemText(i, 2); sfsub1.Format("%6s",subitem); subitem= list.GetItemText(i, 3); sfsub2.Format("%10s",subitem); sfsub=sfsub+sfsub1+sfsub2; dc.TextOut( 2, nYMargin+(i+8-(nCurPage-1)*nMaxLinePerPage)*nCharHeight, sfsub, strlen(sfsub)); } stitle="------------------------------------"; dc.TextOut( 2, nYMargin+(nLineCount+8-(nCurPage-1)*nMaxLinePerPage)*nCharHeight, stitle, strlen(stitle)); //输出总金额 CString stotal,sacttotal,sdiscount; float fdiscount; stotal.Format("%27s",theApp.sconsume); sacttotal.Format("%27s",theApp.sactsum); fdiscount=atof(stotal)-atof(sacttotal); sdiscount.Format("%31.2f",fdiscount); stotal="消费金额:"+stotal; sdiscount="折扣:"+sdiscount; sacttotal="应收金额:"+sacttotal; dc.TextOut( 2, nYMargin+(nLineCount+9-(nCurPage-1)*nMaxLinePerPage)*nCharHeight, stotal, strlen(stotal)); dc.TextOut( 2, nYMargin+(nLineCount+10-(nCurPage-1)*nMaxLinePerPage)*nCharHeight, sdiscount, strlen(sdiscount)); dc.TextOut( 2, nYMargin+(nLineCount+11-(nCurPage-1)*nMaxLinePerPage)*nCharHeight, sacttotal, strlen(sacttotal)); stitle="------------------------------------"; dc.TextOut( 2, nYMargin+(nLineCount+12-(nCurPage-1)*nMaxLinePerPage)*nCharHeight, stitle, strlen(stitle)); sacttotal="谢谢惠顾,欢迎再次光临!"; dc.TextOut( 2, nYMargin+(nLineCount+13-(nCurPage-1)*nMaxLinePerPage)*nCharHeight, sacttotal, strlen(sacttotal)); dc.EndPage(); dc.EndDoc(); //打印结束 //最后不要忘记将字体还原,这一句是必需的 dc.SelectObject(def_font); //恢复原来的字体 font.DeleteObject(); DeleteDC(dc.Detach()); return TRUE; }
CString CPosDlg::GetCompanyFloor(BOOL bCompany) { _RecordsetPtr m_pRecordset; //Must define it in function!!!! CString sql,scompany,sfloor; sql="Select * from COMPANY"; try { m_pRecordset.CreateInstance("ADODB.Recordset"); m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText); if(!m_pRecordset->adoEOF) { scompany=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("COMPANYNAME"); sfloor=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("FLOORNUMBER"); } m_pRecordset->Close(); } catch(_com_error e)///捕捉异常 { CString stemp; if(!bCompany) stemp.Format("[初始化]读取楼层号出错:%s",e.ErrorMessage()); if(bCompany) stemp.Format("[打印]读取公司名出错:%s",e.ErrorMessage()); AfxMessageBox(stemp); } if(!bCompany) return sfloor; if(bCompany) return scompany; }