#include "StdAfx.h" #include "Printkernel.h" /************************************************************************/ /* 函数:GIsHalfHZ[4/25/2016 IT]; /* 描述:判断输入的字符串最后一个字符是否是汉字; /* 参数:; /* [IN] str:输入的字符串; /* 返回:如果是一个完整的汉字,返回FALSE。否则返回TRUE; /************************************************************************/ BOOL GIsHalfHZ(const CString &str) { INT nLength = str.GetLength(); BOOL IsHalf = FALSE; for ( INT i = 0; i < nLength; ) { if ( str.GetAt(i) < 0 ) { i += 2; if ( i > nLength ) IsHalf = TRUE; } else { i++; } } return IsHalf; } CPrinter::CPrinter() { for (int i=0; i<10; i++) { m_iFace[i] = 0; for (int j=0; j<4; j++) m_font[i][j] = new (CFont); } m_nFonts = 0; m_iFont = 0; m_pPrintDlg = NULL; m_Margins.SetRect(0, 0, 0, 0); m_yCur = 0; m_xCur = 0; m_iPage = 1; m_nCopies = 1; } CPrinter::~CPrinter() { for (int i=0; i<10; i++) for (int j=0; j<4; j++) delete (m_font[i][j]); if (m_pPrintDlg) delete (m_pPrintDlg); } int CPrinter::DrawText(TCHAR *str, int iFont, int iFace, int format) { m_DC.TextOut(m_xCur, m_yCur, str); return 1; } int CPrinter::DrawText(TCHAR *str, CRect& m_rt, int iFont, int iFace, int format) { SIZE Size; GetTextExtentPoint32(m_DC.GetSafeHdc(), str, _tcslen(str), &Size); int left, top; if (format & FORMAT_HCENTER) { left = CALCX(m_rt.left) + CALCX(m_rt.Width())/2 - Size.cx/2; if (left < CALCX(m_rt.left)) left = CALCX(m_rt.left); } else if(format & FORMAT_RIGHT) { left = CALCX(m_rt.left) + CALCX(m_rt.Width()) - Size.cx; if (left < CALCX(m_rt.left)) left = CALCX(m_rt.left); } else left = CALCX(m_rt.left); if (format & FORMAT_VCENTER) { top = CALCY(m_rt.top) + CALCY(m_rt.Height())/2 - Size.cy/2; if (top < CALCY(m_rt.top)) top = CALCY(m_rt.top); } else top = CALCY(m_rt.top); m_DC.TextOut(left, top, str); return 1; } /************************************************************************/ /* 函数:[4/25/2016 IT]; /* 描述:; /* 参数:; /* [IN] :; /* [OUT] :; /* [IN/OUT] :; /* 返回:void; /* 注意:LOGFONT需要说明的是成员变量lfHeight和lfWidth均为逻辑坐标值; /* 示例:; /* /* 修改:; /* 日期:; /* 内容:; /************************************************************************/ int CPrinter::DrawTextEx(IN LPCTSTR lpStr, IN CRect& rtStr, IN LOGFONT& lf, IN const int& nAlign) { if ( lpStr == NULL ) { _tprintf_s(_T("字符串指针空\n")); return -1; } if ( rtStr.IsRectEmpty() ) { _tprintf_s(_T("字符串区域空\n")); return -1; } CFont cf; CFont *oldf = NULL; SIZE Size; int nLeft = 0, nTop = 0; cf.CreateFontIndirect(&lf); oldf = m_DC.SelectObject(&cf); // GetTextExtentPoint32返回字符串宽高(按逻辑坐标返回SIZE); if ( !GetTextExtentPoint32(m_DC.GetSafeHdc(), lpStr, _tcslen(lpStr), &Size) ) return -1; // 处理情况; if ( Size.cx <= rtStr.Width() ) { if (nAlign & FORMAT_HCENTER) { nLeft = rtStr.left + rtStr.Width()/2 - Size.cx/2; if ( nLeft < rtStr.left ) nLeft = rtStr.left; } else if(nAlign & FORMAT_RIGHT) { nLeft = rtStr.left + rtStr.Width() - Size.cx; if (nLeft < rtStr.left) nLeft = rtStr.left; } else nLeft = rtStr.left; if (nAlign & FORMAT_VCENTER) { nTop = rtStr.top + rtStr.Height()/2 - Size.cy/2; if (nTop < rtStr.top) nTop = rtStr.top; } else nTop = rtStr.top; // 输出文本,注意TextOut使用的x,y是逻辑坐标值; m_DC.TextOut(nLeft, nTop, lpStr); } else { } m_DC.SelectObject(oldf); return 1; } int CPrinter::DrawHLine(int x_left, int y_left, int x_right, int y_right) { // CDC::MoveTo和CDC::LineTo的x,y坐标使用的是逻辑坐标值; m_DC.MoveTo(CALCX(m_Margins.left + x_left), CALCY(m_Margins.top + y_left)); m_DC.LineTo(CALCX(m_Margins.left + x_right), CALCY(m_Margins.top + y_right)); return 0; } int CPrinter::DrawHLine(int x_left, int y_left, int x_right, int y_right, CPen &newpen) { // CDC::MoveTo和CDC::LineTo的x,y坐标使用的是逻辑坐标值; CPen* oldPen = m_DC.SelectObject(&newpen); m_DC.MoveTo(CALCX(m_Margins.left + x_left), CALCY(m_Margins.top + y_left)); m_DC.LineTo(CALCX(m_Margins.left + x_right), CALCY(m_Margins.top + y_right)); m_DC.SelectObject(oldPen); return 0; } int CPrinter::DrawVLine(int x_up, int y_up, int x_bottom, int y_bottom) { // CDC::MoveTo和CDC::LineTo的x,y坐标使用的是逻辑坐标值; m_DC.MoveTo(CALCX(m_Margins.left + x_up), CALCY(m_Margins.top + y_up)); m_DC.LineTo(CALCX(m_Margins.left + x_bottom), CALCY(m_Margins.top + y_bottom)); return 0; } int CPrinter::DrawVLine(int x_up, int y_up, int x_bottom, int y_bottom, CPen &newpen) { // CDC::MoveTo和CDC::LineTo的x,y坐标使用的是逻辑坐标值; CPen* oldPen = m_DC.SelectObject(&newpen); m_DC.MoveTo(CALCX(m_Margins.left + x_up), CALCY(m_Margins.top + y_up)); m_DC.LineTo(CALCX(m_Margins.left + x_bottom), CALCY(m_Margins.top + y_bottom)); m_DC.SelectObject(oldPen); return 0; } /************************************************************************/ /* 函数:[4/24/2016 IT]; /* 描述:; /* 参数:; /* [IN] :; /* [OUT] :; /* [IN/OUT] :; /* 返回:void; /* 注意:; /* 示例:; /* /* 修改:; /* 日期:; /* 内容:; /************************************************************************/ int CPrinter::DrawBarCode(TCHAR *str, CRect& rcBar) { Barcode128 code; code.SetRatio(1); code.Encode128B(str); COLORREF clrBar = RGB(0,0,0); COLORREF clrSpace = RGB(255,255,255); //rcBar = m_Margins; //P_DrawBarcode(pDC,iL,iT,iB-10,iB,clrBar,clrSpace,iPenW,&code); //m_DC.DPtoLP(rcBar); //m_DC.SetMapMode(MM_TEXT); code.DrawBarcode(m_hPrinter, rcBar.left, rcBar.top, rcBar.right, rcBar.bottom, clrBar, clrSpace, 2); return 0; } int CPrinter::AddFont(CFont &newfont) { return -1; } int CPrinter::AddFont(LOGFONT *lf) { if (m_nFonts == 10) return -1; m_font[m_nFonts][FACE_NORMAL]->CreateFontIndirect(lf); lf->lfWeight = FW_BLACK; m_font[m_nFonts][FACE_NORMALBOLD]->CreateFontIndirect(lf); lf->lfWeight = FW_REGULAR; lf->lfHeight = CALCF(25); m_font[m_nFonts][FACE_BIG]->CreateFontIndirect(lf); lf->lfWeight = FW_BLACK; m_font[m_nFonts][FACE_BIGBOLD]->CreateFontIndirect(lf); m_nFonts ++; return 0; } int CPrinter::SetFontFace(int iFont, int iFace) { ASSERT(iFont < 10); ASSERT(iFace < 4); m_iFont = iFont; m_iFace[m_iFont] = iFace; m_DC.SelectObject(m_font[m_iFont][m_iFace[m_iFont]]); return 0; } /************************************************************************/ /* 函数:[4/24/2016 IT]; /* 描述:; /* 参数:; /* [IN] :; /* [OUT] :; /* [IN/OUT] :; /* 返回:void; /* 注意:; /* 示例:; /* /* 修改:; /* 日期:; /* 内容:; /************************************************************************/ int CPrinter::InitToPrint(TCHAR *PrinterName, int Copies ) { if (m_pPrintDlg == NULL) // delete it in ~CPrinter() m_pPrintDlg = new CPrintDialog(FALSE,PD_DISABLEPRINTTOFILE); ASSERT(m_pPrintDlg != NULL); if (PrinterName == NULL) // which printer ist desired, and how much copies? { if (m_pPrintDlg->DoModal() == IDCANCEL) return -1; // 获取打印机设备上的DC值; m_hPrinter = m_pPrintDlg->GetPrinterDC(); // 获取要打印的份数; m_nCopies = m_pPrintDlg->GetCopies(); } else // a printer is given by name.. { if (m_PrinterDC.CreateDC(NULL, PrinterName, NULL, NULL) == 0) { m_LastErrNo = PRERR_CANTCREATEPRINTERDC; return (-1); // failed, then set the num of Last Error } m_hPrinter = m_PrinterDC; m_nCopies = Copies; } if (m_DC.Attach(m_hPrinter) == 0) return -1; m_DC.m_bPrinting = TRUE; if (m_pPrintDlg) delete (m_pPrintDlg); m_pPrintDlg = NULL; LOGFONT log; log.lfHeight = CALCF(13); // 13号字体; log.lfWidth = 0; log.lfEscapement = 0; log.lfOrientation = 0; log.lfWeight = FW_REGULAR; log.lfItalic = false; log.lfUnderline = false; log.lfStrikeOut = 0; log.lfCharSet = ANSI_CHARSET; log.lfOutPrecision = OUT_DEFAULT_PRECIS; log.lfClipPrecision = CLIP_DEFAULT_PRECIS; log.lfQuality = DEFAULT_QUALITY; log.lfPitchAndFamily = DEFAULT_PITCH || FF_ROMAN; _tcscpy_s(log.lfFaceName,_T("Arial")); AddFont(&log); m_iFont = 0; //set default font is index of 0 return 0; } int CPrinter::StartPrint() { CString strTitle; // 打印文档对象; DOCINFO di; strTitle.LoadString(AFX_IDS_APP_TITLE); ::ZeroMemory(&di, sizeof (DOCINFO)); di.cbSize = sizeof (DOCINFO); // 设置打印文档的名称; di.lpszDocName = strTitle; // 开始打印工作; if FAILED(m_DC.StartDoc(&di) == -1) return (-1); // 获取打印机打印区域的宽度和高度(像素); m_WorkSize.cx = m_DC.GetDeviceCaps(HORZRES); m_WorkSize.cy = m_DC.GetDeviceCaps(VERTRES); return 0; } int CPrinter::EndPrint() { m_DC.EndDoc(); // end a print job m_DC.Detach(); // detach the printer DC return 0; } /************************************************************************/ /* 函数:SetMargins[4/24/2016 IT]; /* 描述:设置页边距; /* 参数:; /* [IN] :; /* [OUT] :; /* [IN/OUT] :; /* 返回:void; /* 注意:; /* 示例:; /************************************************************************/ int CPrinter::SetMargins(int Top, int Bottom, int Left, int Right) { if (Top > 0) m_Margins.top = Top; if (Bottom > 0) m_Margins.bottom = Bottom; if (Left > 0) m_Margins.left = Left; if (Right > 0) m_Margins.right = Right; m_yCur = CALCY(m_Margins.top); m_xCur = CALCX(m_Margins.left); return 0; } void CPrinter::SetDistance(int punkte) { m_Abstand = punkte; } int CPrinter::StartPage() { if (m_DC.StartPage() < 0) { m_LastErrNo = PRERR_STARTPAGEFAILED; return (-1); } m_yCur = CALCY(m_Margins.top); m_xCur = CALCX(m_Margins.left); return 0; } void CPrinter::EndPage() { m_DC.EndPage(); } void CPrinter::NewPage() { m_DC.EndPage(); m_DC.StartPage(); m_yCur = CALCY(m_Margins.top); m_xCur = CALCX(m_Margins.left); } int CPrinter::GetWidth() { return MulDiv(m_WorkSize.cx, 72, GetDeviceCaps(m_DC.m_hDC, LOGPIXELSX)); } int CPrinter::GetStrSize(TCHAR *str, CSize &size) { GetTextExtentPoint32(m_DC.GetSafeHdc(), str, _tcslen(str), &size); size.cx = MulDiv(size.cx, 72, GetDeviceCaps(m_DC.m_hDC, LOGPIXELSX)); size.cy = MulDiv(size.cy, 72, GetDeviceCaps(m_DC.m_hDC, LOGPIXELSY)); return 0; } void CPrinter::MoveTo(int xCur, int yCur) { m_xCur = CALCX(m_Margins.left + xCur); m_yCur = CALCY(m_Margins.right + yCur); }