MFC打印技术CPrintDlg
Posted htj10
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MFC打印技术CPrintDlg相关的知识,希望对你有一定的参考价值。
例如,打印超市小票
// 打印小票 void COnTheStageDlg::Print(CString sYje, CString sSje, CString sZl) { CTime time = CTime::GetCurrentTime(); CString sql, ph, xsph; //判断今天的票号,并递增 sql.Format(_T("select XSPH from TB_SELL where TO_CHAR(XSSJ,‘MMDDYYYY‘)=‘%s‘ order by XSPH"), time.Format("%m%d%Y")); AdoConn adoConn; adoConn.InitConnect(); adoConn.m_pRs = adoConn.GetRecordSet((_bstr_t)sql); if (!adoConn.m_pRs->adoEOF) { adoConn.m_pRs->MoveLast(); ph = (TCHAR*)(_bstr_t)adoConn.m_pRs->GetCollect("XSPH"); xsph.Format(_T("XS%s%04d"), time.Format("%y%m%d"), _ttoi(ph.Right(4)) + 1); } else xsph.Format(_T("XS%s0001"), time.Format("%y%m%d")); //插入记录到数据库 表 tb_sell CString spbh, spmc, spjm, splb, txm, xssl, xsdj, xssj; for (int i = 0, n = m_Grid.GetItemCount(); i<n; i++) { spbh = m_Grid.GetItemText(i, 0); spmc = m_Grid.GetItemText(i, 1); spjm = m_Grid.GetItemText(i, 2); splb = m_Grid.GetItemText(i, 3); txm = m_Grid.GetItemText(i, 4); xssl = m_Grid.GetItemText(i, 5); xsdj = m_Grid.GetItemText(i, 6); sql.Format(_T("insert into TB_SELL(XSPH,SPBH,SPMC,SPJM,SPLB,TXM,XSSL,SPDJ,XSSJ) values(‘%s‘,‘%s‘,‘%s‘,‘%s‘,‘%s‘,‘%s‘,%d,%f,TO_DATE(‘%s‘,‘MMDDYYYY HH24:MI‘))"), xsph, spbh, spmc, spjm, splb, txm, _ttoi(xssl), _ttof(xsdj), time.Format("%m%d%Y %H:%M")); adoConn.ExecuteSql((_bstr_t)sql); } adoConn.ExitConnect(); const int MAX_SIZE_SP = 100;//所能打印的商品最大数量 const int MAX_SIZE_COL = 5; //最大列数 const int HEIGHT_ROW = 20;//每行的标准高度 const int MAX_SIZE_ROW = 40;//每页的最大行数 const int LEFT_LOC = 5;//打印的左边位置 const int RIGHT_LOC = 220;//打印的最右边的坐标位置 const int TOP_LOC = 10;//打印的最上边的坐标位置 const int BOTTOM_LOC = TOP_LOC + MAX_SIZE_ROW * HEIGHT_ROW;//打印的最下边的坐标位置,超过了换下一页(40行换页) CDC* pDC = GetDC(); int screenx = pDC->GetDeviceCaps(LOGPIXELSX);//沿屏幕宽度每逻辑英寸的像素数,在多显示器系统中,该值对所显示器相同; int screeny = pDC->GetDeviceCaps(LOGPIXELSY);//沿屏幕高度每逻辑英寸的像素数,在多显示器系统中,该值对所显示器相同; CPrintDialog m_printdlg(FALSE); if (m_printdlg.DoModal() == IDOK) { CDC dc; dc.Attach(m_printdlg.GetPrinterDC()); //查询打印设备的信息,每逻辑英寸的像素数 int printx = dc.GetDeviceCaps(LOGPIXELSX); int printy = dc.GetDeviceCaps(LOGPIXELSY); double ratex = (double)(printx) / screenx; double ratey = (double)(printy) / screeny; CString str[MAX_SIZE_SP + 1][MAX_SIZE_COL]; int i = 0; str[i][0] = _T("商品编号"); str[i][1] = _T("商品名称"); str[i][2] = _T("销售数量"); str[i][3] = _T("销售单价"); str[i][4] = _T("销售金额"); ++i; double sum = 0; CString strsum; for (int nCount = m_Grid.GetItemCount(); i <= (MAX_SIZE_SP < nCount ? MAX_SIZE_SP : nCount); i++) { str[i][0] = m_Grid.GetItemText(i - 1, 0);//商品编号 str[i][1] = m_Grid.GetItemText(i - 1, 1);//商品名称 str[i][2] = m_Grid.GetItemText(i - 1, 5);//销售数量 str[i][3] = m_Grid.GetItemText(i - 1, 6);//销售单价 str[i][4].Format(_T("%0.2f"), _ttoi(str[i][2])*_ttof(str[i][3]));//数量*单价 } dc.StartDoc(_T("printstart")); CFont pfont; pfont.CreatePointFont(110, L"宋体", &dc); int x, y; x = LEFT_LOC; y = TOP_LOC; dc.TextOut((int)ratex * x, (int)ratey * y, _T("XX超市")); y = y + HEIGHT_ROW; dc.TextOut((int)ratex * x, (int)ratey * y, _T("销售票号:") + xsph); y = y + HEIGHT_ROW; dc.TextOut((int)ratex * x, (int)ratey * y, _T("销售时间:") + time.Format("%Y-%m-%d %H:%M:%S")); y += HEIGHT_ROW; //画两根线 y += 2; dc.MoveTo((int)ratex * LEFT_LOC, (int)ratey * y); dc.LineTo((int)ratex * RIGHT_LOC, (int)ratey * y); y += 2; dc.MoveTo((int)ratex * LEFT_LOC, (int)ratey * y); dc.LineTo((int)ratex * RIGHT_LOC, (int)ratey * y); y += 2; //打印所有商品信息 for (int k = 0; k < i; ++k) { if (y>BOTTOM_LOC) { y = TOP_LOC; dc.EndPage(); dc.StartPage(); } for (int j = 0; j < MAX_SIZE_COL; j++) { if (j < 2) { dc.TextOut((int)ratex*(LEFT_LOC + j * 80), (int)ratey * y, str[k][j]); } else { dc.TextOut((int)ratex*(LEFT_LOC + (j - 2) * 80), (int)ratey * (y + HEIGHT_ROW), str[k][j]); } } y += 2 * HEIGHT_ROW; if (0 == k) {//画一条红色的虚线 CPen pen(PS_DOT, 1, RGB(200, 0, 0)); CPen* pOld = dc.SelectObject(&pen); y += 1; dc.MoveTo((int)ratex * LEFT_LOC, (int)ratey * y); dc.LineTo((int)ratex * RIGHT_LOC, (int)ratey * y); dc.SelectObject(pOld); y += 1; } } //画两根线 y += 2; dc.MoveTo((int)ratex * LEFT_LOC, (int)ratey * y); dc.LineTo((int)ratex * RIGHT_LOC, (int)ratey * y); y += 2; dc.MoveTo((int)ratex * LEFT_LOC, (int)ratey * y); dc.LineTo((int)ratex * RIGHT_LOC, (int)ratey * y); y += 2; //打印金额 CRect rc((int)ratex * LEFT_LOC, (int)ratey * y, (int)ratex * RIGHT_LOC, (int)ratey * (y + HEIGHT_ROW)); dc.DrawText(_T("应收金额:") + sYje, rc, DT_RIGHT); CFont font2; font2.CreatePointFont(1200, _T("楷体")); CFont* pOld = dc.SelectObject(&font2); y += HEIGHT_ROW; CString strTemp = _T("实收:") + sSje; CSize sz = dc.GetTextExtent(strTemp); rc.SetRect((int)ratex * LEFT_LOC, (int)ratey * y, (int)ratex * RIGHT_LOC, (int)ratey * y + sz.cy); dc.DrawText(strTemp, rc, DT_RIGHT); dc.SelectObject(pOld); rc.SetRect((int)ratex * LEFT_LOC, (int)ratey * y + sz.cy + 2, (int)ratex * RIGHT_LOC, (int)ratey * (y + HEIGHT_ROW) + sz.cy + 2); dc.DrawText(_T("找零:") + sZl, rc, DT_RIGHT); //谢谢惠顾,欢迎下次光临 int loc = (int)ratey * (y + HEIGHT_ROW) + sz.cy + 2; rc.SetRect((int)ratex * LEFT_LOC, loc, (int)ratex * RIGHT_LOC, loc + (int)ratey * HEIGHT_ROW); dc.DrawText(_T("谢谢惠顾,欢迎下次光临"), rc, DT_CENTER); dc.EndDoc(); pfont.DeleteObject(); } ReleaseDC(pDC); }
********************
以上是关于MFC打印技术CPrintDlg的主要内容,如果未能解决你的问题,请参考以下文章